com.stytch.java:sdk

Stytch Java SDK


License
Abstyles

Documentation

Stytch Java Library

The Stytch Java library makes it easy to use the Stytch user infrastructure API in Java, Kotlin and Scala applications.

It pairs well with the Stytch Web SDK or your own custom authentication flow.

Check out our Java example app here.

Install

implementation("com.stytch.java:sdk:1.1.0")

Usage

You can find your API credentials in the Stytch Dashboard.

This client library supports all of Stytch's live products:

B2C

B2B

Example B2C usage

Create an API client:

Kotlin:

import com.stytch.java.consumer.StytchClient
StytchClient.configure(
    projectId = "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
    secret = "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I="
)

Java:

import com.stytch.java.consumer.StytchClient;
StytchClient.configure(
        "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
        "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I="
        );

Send a magic link by email:

Kotlin:

when (val result = StytchClient.magicLinks.email.loginOrCreate(
    LoginOrCreateRequest(
        email = "email@address.com",
        loginMagicLinkURL = "https://example.com/authenticate",
        signupMagicLinkURL = "https://example.com/authenticate",
    ),
)) {
    is StytchResult.Success -> println(result.value)
    is StytchResult.Error -> println(result.exception)
}

Java:

LoginOrCreateRequest request = new LoginOrCreateRequest(
    "email@address.com",
    "https://example.com/authenticate",
    "https://example.com/authenticate",
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null
);
StytchResult<LoginOrCreateResponse> response = StytchClient.magicLinks.getEmail().loginOrCreateCompletable(request).get();
if (response instanceof StytchResult.Error) {
    var exception = ((StytchResult.Error) response).getException();
    System.out.println(exception.getReason());
} else {
    System.out.println(((StytchResult.Success<?>) response).getValue());
}

Authenticate the token from the magic link:

Kotlin:

when (val result = StytchClient.magicLinks.authenticate(
    AuthenticateRequest(token = "DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=")
)) {
    is StytchResult.Success -> println(result.value)
    is StytchResult.Error -> println(result.exception)
}

Java:

AuthenticateRequest request = new AuthenticateRequest("DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=");
StytchResult<AuthenticateResponse> response = StytchClient.magicLinks.authenticateCompletable(request).get();
if (response instanceof StytchResult.Error) {
    var exception = ((StytchResult.Error) response).getException();
    System.out.println(exception.getReason());
} else {
    System.out.println(((StytchResult.Success<?>) response).getValue());
}

Example B2B usage

Create an API client:

Kotlin:

import com.stytch.java.b2b.StytchB2BClient
...
StytchB2BClient.configure(
    projectId = "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
    secret = "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I="
)

Java:

import com.stytch.java.b2b.StytchB2BClient;
...
        StytchB2BClient.configure(
        "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
        "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I="
        );

Create an organization

Kotlin:

when (val result = StytchB2BClient.organizations.create(CreateRequest(
    organizationName = "Acme Co",
    organizationSlug = "acme-co",
    emailAllowedDomains = listOf("acme.co"),
))) {
    is StytchResult.Success -> println(result.value)
    is StytchResult.Error -> println(result.exception)
}

Java:

var emailAllowedDomains = new ArrayList<String>();
emailAllowedDomains.add("acme.co");
CreateRequest request = new CreateRequest(
        "Acme Co",
        "acme-co",
        null,
        null,
        null,
        emailAllowedDomains
);
StytchResult<CreateResponse> response = StytchB2BClient.organizations.createCompletable(request).get();
if (response instanceof StytchResult.Error) {
    var exception = ((StytchResult.Error) response).getException();
    System.out.println(exception.getReason());
} else {
    System.out.println(((StytchResult.Success<?>) response).getValue());
}

Log the first user into the organization

Kotlin:

when (val result = StytchB2BClient.magicLinks.email.loginOrSignup(LoginOrSignupRequest(
    organizationId = "organization-id-from-create-response-...",
    emailAddress = "admin@acme.co",
    loginRedirectURL = "https://example.com/authenticate",
    signupRedirectURL = "https://example.com/authenticate",
)) {
    is StytchResult.Success -> println(result.value)
    is StytchResult.Error -> println(result.exception)
}

Java:

LoginOrSignupRequest request = new LoginOrSignupRequest(
    "organization-id-from-create-response-...",
    "admin@acme.co",
    "https://example.com/authenticate",
    "https://example.com/authenticate"
);
StytchResult<LoginOrSignupResponse> response = StytchB2BClient.magicLinks.getEmail().loginOrSignup(request).get();
if (response instanceof StytchResult.Error) {
    var exception = ((StytchResult.Error) response).getException();
    System.out.println(exception.getReason());
} else {
    System.out.println(((StytchResult.Success<?>) response).getValue());
}

Handling Errors

When possible Stytch returns an error wrapped in a StytchResult.Error class. 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.