Skip to main content
The Courier Java SDK provides typed access to the Courier REST API from applications written in Java 8+. It uses builder-pattern request construction, OkHttp for transport, and returns strongly typed response objects. Available on GitHub and Maven Central.

Installation

implementation("com.courier:courier-java:4.9.1")
Requires Java 8+.

Quick Start

import com.courier.client.CourierClient;
import com.courier.client.okhttp.CourierOkHttpClient;
import com.courier.core.JsonValue;
import com.courier.models.UserRecipient;
import com.courier.models.send.SendMessageParams;

CourierClient client = CourierOkHttpClient.fromEnv();

SendMessageParams params = SendMessageParams.builder()
    .message(SendMessageParams.Message.builder()
        .to(UserRecipient.builder().userId("your_user_id").build())
        .template("your_template_id")
        .data(SendMessageParams.Message.Data.builder()
            .putAdditionalProperty("foo", JsonValue.from("bar"))
            .build())
        .build())
    .build();

var response = client.send().message(params);
System.out.println(response.requestId());
CourierOkHttpClient.fromEnv() reads COURIER_API_KEY from your environment (or the courier.apiKey system property). You can also configure it with the builder: .apiKey("your-key").

Authentication

Get your API key from Settings > API Keys in the Courier dashboard. Set it as an environment variable:
export COURIER_API_KEY="your-api-key"
Or pass it via the builder:
CourierClient client = CourierOkHttpClient.builder()
    .apiKey("your-api-key")
    .build();
SetterSystem propertyEnvironment variableDefault
apiKeycourier.apiKeyCOURIER_API_KEY
baseUrlcourier.baseUrlCOURIER_BASE_URLhttps://api.courier.com

Sending Notifications

With a template

var params = SendMessageParams.builder()
    .message(SendMessageParams.Message.builder()
        .to(UserRecipient.builder().userId("user_123").build())
        .template("my-template-id")
        .data(SendMessageParams.Message.Data.builder()
            .putAdditionalProperty("orderNumber", JsonValue.from("10042"))
            .build())
        .build())
    .build();

var response = client.send().message(params);

With inline content

var params = SendMessageParams.builder()
    .message(SendMessageParams.Message.builder()
        .to(UserRecipient.builder().email("jane@example.com").build())
        .content(SendMessageParams.Message.Content.builder()
            .title("Order shipped")
            .body("Your order {{orderNumber}} has shipped!")
            .build())
        .data(SendMessageParams.Message.Data.builder()
            .putAdditionalProperty("orderNumber", JsonValue.from("10042"))
            .build())
        .build())
    .build();

var response = client.send().message(params);

Available Resources

The SDK covers the full Courier API. Every method returns strongly typed response objects.
ResourceNamespaceDescription
Sendclient.send()Send messages to one or more recipients
Messagesclient.messages()Retrieve status, history, and content of sent messages
Profilesclient.profiles()Create, update, and retrieve user profiles
Usersclient.users()Manage preferences, tenants, and push tokens per user
Authclient.auth()Issue JWT tokens for client-side SDK authentication
Bulkclient.bulk()Send messages to large recipient lists via jobs
Listsclient.lists()Manage subscription lists and their subscribers
Audiencesclient.audiences()Define and query audience segments
Tenantsclient.tenants()Manage tenants for multi-tenant setups
Automationsclient.automations()Invoke multi-step automation workflows
Brandsclient.brands()Manage brand settings (logos, colors, templates)
Notificationsclient.notifications()List and inspect notification templates
Translationsclient.translations()Manage localized content

Common Operations

Checking Message Status

var message = client.messages().retrieve("your-message-id");
System.out.println(message.status());
System.out.println(message.delivered());

Managing User Profiles

client.profiles().create("user_123", ProfileCreateParams.builder()
    .profile(Map.of(
        "email", "jane@example.com",
        "name", "Jane Doe"
    ))
    .build());

var profile = client.profiles().retrieve("user_123");

Issuing JWT Tokens

var result = client.auth().issueToken(AuthIssueTokenParams.builder()
    .scope("user_id:user_123 inbox:read:messages inbox:write:events")
    .expiresIn("2 days")
    .build());

System.out.println(result.token());
ScopePermission
user_id:<id>Which user the token is for (required)
inbox:read:messagesRead inbox messages
inbox:write:eventsMark messages as read/archived
read:preferencesRead notification preferences
write:preferencesUpdate notification preferences
write:user-tokensRegister push notification tokens

Configuration

Error Handling

When the API returns a non-success status code, the SDK throws a CourierServiceException:
try {
    client.send().message(params);
} catch (CourierServiceException e) {
    System.out.println(e.statusCode());
    System.out.println(e.message());
}

Retries

The SDK automatically retries failed requests up to 2 times with exponential backoff. Configure globally:
CourierClient client = CourierOkHttpClient.builder()
    .maxRetries(0)
    .build();

Timeouts

Requests time out after 60 seconds by default. Configure globally:
CourierClient client = CourierOkHttpClient.builder()
    .timeout(Duration.ofSeconds(20))
    .build();
Or override per-request using withOptions:
client.withOptions(options -> options.timeout(Duration.ofSeconds(5)))
    .send().message(params);

More Operations

The SDK covers the full Courier REST API. Here are a few more resources beyond what’s documented above:
ResourceMethodUse case
User preferencesclient.users().preferences().retrieve(userId)Fetch a user’s notification preferences for your preference center
Cancel a messageclient.messages().cancel(messageId)Cancel a delayed or queued message before delivery
Push tokensclient.users().tokens().addSingle(token, params)Register a device push token for iOS, Android, or React Native
Automationsclient.automations().invoke().invokeAdHoc(params)Run a multi-step workflow via Automations