Stytch Go Library
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.
Install
$ go get github.com/stytchauth/stytch-go/v12
Usage
You can find your API credentials in the Stytch Dashboard.
This client library supports all Stytch's live products:
B2C
B2B
Example B2C usage
Create an API client:
import (
"context"
"github.com/stytchauth/stytch-go/v12/stytch"
"github.com/stytchauth/stytch-go/v12/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...)
}
Example B2B usage
Create an API client:
import (
"context"
"github.com/stytchauth/stytch-go/v13/stytch"
"github.com/stytchauth/stytch-go/v13/stytch/b2b/b2bstytchapi"
"github.com/stytchauth/stytch-go/v13/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/v13/stytch/b2b/b2bstytchapi"
"github.com/stytchauth/stytch-go/v13/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)
}
Handling Errors
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.
Documentation
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.
Support
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.
Development
See DEVELOPMENT.md
Code of Conduct
Everyone interacting in the Stytch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.