Java Quickstart

  1. Make sure you have the latest version of Java installed.
  2. Sign up for Courier to get access to your API key.
  3. Add the channel(s) you wish to send your message to.
  4. Copy the following Courier Java SDK dependency snippet and add to Maven pom.xml file
<dependency>
  <groupId>com.courier</groupId>
  <artifactId>courier-java</artifactId>
  <version>X.X.X</version>
  <scope>compile</scope>
</dependency>
  1. Ask Maven to download the dependencies by running:
mvn compile
  1. Add the following code to import dependencies:
import services.Courier;
import services.SendService;
import models.SendEnhancedRequestBody;
import models.SendEnhancedResponseBody;
import models.SendRequestMessage;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
  1. Add the following code to send the message:
public class App {
  public static void main(String[] args) {
    Courier.init("<auth_token>");

    SendEnhancedRequestBody request = new SendEnhancedRequestBody();
    SendRequestMessage message = new SendRequestMessage();

    HashMap<String, String> to = new HashMap<String, String>();
    to.put("email", "email@example.com");
    message.setTo(to);

    HashMap<String, Object> content = new HashMap<String, Object>();
    content.put("title", "Welcome!");
    content.put("body", "Thanks for signing up, {{name}}");
    message.setContent(content);

    HashMap<String, Object> data = new HashMap<String, Object>();
    data.put("name", "Peter Parker");
    message.setData(data);

    HashMap<String, Object> routing = new HashMap<String, Object>();
    routing.put("method", "single");
    routing.put("channels", ["email"]);
    message.setRouting(routing);

    request.setMessage(message);
    try {
        SendEnhancedResponseBody response = new SendService().sendEnhancedMessage(request);
        System.out.println(response);
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}
  1. Replace the variables to match the type and content of notification you want to send. There are 4 main properties within this example notification:

    • to: provide information used by Courier to identify the recipient of the notification. Here you can include details such as the recipient’s email address for email, phone number for SMS.
    • content: add your notification’s title and body.
    • data: include any data you want to pass to a message template. The data will populate the corresponding template variables.
    • routing: customize which channel(s) Courier will potentially deliver the message. If no routing key is specified, Courier will use the default routing configured in the Courier Studio UI.

    Explore how to customize your notification with more properties >

  2. Run your code. Upon running this API call, only a requestId will be returned.

{ "requestId": "87e7c05b-4f46-fda24e356e23" }
  1. Monitor the status of your notification (once sent) in the logs: https://app.courier.com/logs

FAQs

Questions?

Join our developer community on Discord and ask questions in the #ask-support channel.