The Stytch Go library makes it easy to use the Stytch user infrastructure API in Go applications.
It pairs well with the Stytch Web SDK or your own custom authentication flow.
$ go get github.com/stytchauth/stytch-go/v15
You can find your API credentials in the Stytch Dashboard.
This client library supports all Stytch's live products:
- Email Magic Links
- Embeddable Magic Links
- OAuth logins
- SMS passcodes
- WhatsApp passcodes
- Email passcodes
- Session Management
- WebAuthn
- Time-based one-time passcodes (TOTPs)
- Crypto wallets
- Passwords
- M2M
- Organizations
- Members
- Email Magic Links
- OAuth logins
- Session Management
- Single-Sign On
- Discovery
- Passwords
Create an API client:
import (
"context"
"github.com/stytchauth/stytch-go/v15/stytch"
"github.com/stytchauth/stytch-go/v15/stytch/consumer/stytchapi"
)
stytchAPIClient, err := stytchapi.NewClient(
"project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
"secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
)
if err != nil {
panic(err)
}
Send a magic link by email:
res, err := stytchAPIClient.MagicLinks.Email.Send(
context.Background(),
&stytch.MagicLinksEmailSendParams{
Email: "sandbox@stytch.com",
Attributes: stytch.Attributes{
IPAddress: "10.0.0.0",
},
},
)
Authenticate the token from the magic link:
res, err := stytchAPIClient.MagicLinks.Authenticate(
context.Background(),
&stytch.MagicLinksAuthenticateParams{
Token: "DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=",
Options: stytch.Options{IPMatchRequired: true},
Attributes: stytch.Attributes{IPAddress: "10.0.0.0"},
})
Get all users
res, err := stytchAPIClient.Users.Search(
context.Background(),
&stytch.UsersSearchParams{
Limit: 1000
})
Search users
res, err := stytchAPIClient.Users.Search(
context.Background(),
&stytch.UsersSearchParams{
Limit: 1000,
Query: stytch.UsersSearchQuery{
Operator: stytch.UserSearchOperatorOR,
Operands: []json.Marshaler{
stytch.UsersSearchQueryPhoneVerifiedFilter{true},
stytch.UsersSearchQueryEmailVerifiedFilter{true},
stytch.UsersSearchQueryWebAuthnRegistrationVerifiedFilter{true},
}
}
})
Iterate over all pages of users for a search query
var users []stytch.User
iter := stytchAPIClient.Users.SearchAll(&stytch.UsersSearchParams{})
for iter.HasNext() {
res, err := iter.Next(context.Background())
if err != nil {
fmt.Println(err)
return nil, err
}
users = append(users, res...)
}
Create an API client:
import (
"context"
"github.com/stytchauth/stytch-go/v15/stytch"
"github.com/stytchauth/stytch-go/v15/stytch/b2b/b2bstytchapi"
"github.com/stytchauth/stytch-go/v15/stytch/b2b/organizations"
)
stytchClient, err := b2bstytchapi.NewClient(
"project-test-uuid",
"secret-test-uuid",
)
if err != nil {
panic(err)
}
Create an organization
params := &organizations.CreateParams{
OrganizationName: "Example Org Inc.",
OrganizationSlug: "example-org",
}
resp, err := client.Organizations.Create(context.Background(), params)
if err != nil {
log.Fatalf("error in method call: %v", err)
}
Log the first user into the organization
import (
"context"
"log"
"github.com/stytchauth/stytch-go/v15/stytch/b2b/b2bstytchapi"
"github.com/stytchauth/stytch-go/v15/stytch/b2b/magiclinks/email"
)
params := &email.LoginOrSignupParams{
OrganizationID: "organization-test-07971b06-ac8b-4cdb-9c15-63b17e653931",
EmailAddress: "sandbox@stytch.com",
}
resp, err := client.MagicLinks.Email.LoginOrSignup(context.Background(), params)
if err != nil {
log.Fatalf("error in method call: %v", err)
}
When possible Stytch returns an error prepended with Stytch Error
.
Additionally, the error should include a type that can be used to distinguish errors.
Learn more about errors in the docs.
See example requests and responses for all the endpoints in the Stytch API Reference.
Follow one of the integration guides or start with one of our example apps.
If you've found a bug, open an issue!
If you have questions or want help troubleshooting, join us in Slack or email support@stytch.com.
If you've found a security vulnerability, please follow our responsible disclosure instructions.
See DEVELOPMENT.md
Everyone interacting in the Stytch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.