This documentation is for developers interested in using this Java client to integrate their government service with GOV.UK Notify.
- Installation
- Getting started
- Send messages
- Get the status of one message
- Get the status of all messages
- Get a template by ID
- Get a template by ID and version
- Get all templates
- Generate a preview template
- Get all received text messages
The notifications-java-client is deployed to Bintray. Add this snippet to your Maven settings.xml
file.
Click here to expand for more information.
<?xml version='1.0' encoding='UTF-8'?>
<settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd' xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-gov-uk-notify-maven</id>
<name>bintray</name>
<url>http://dl.bintray.com/gov-uk-notify/maven</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-gov-uk-notify-maven</id>
<name>bintray-plugins</name>
<url>http://dl.bintray.com/gov-uk-notify/maven</url>
</pluginRepository>
</pluginRepositories>
<id>bintray</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>bintray</activeProfile>
</activeProfiles>
</settings>
Then add the Maven dependency to your project.
<dependency>
<groupId>uk.gov.service.notify</groupId>
<artifactId>notifications-java-client</artifactId>
<version>3.8.0-RELEASE</version>
</dependency>
Click here to expand for more information.
repositories {
jcenter()
}
dependencies {
compile('uk.gov.service.notify:notifications-java-client:3.8.0-RELEASE')
}
Click 'set me up!' on https://bintray.com/gov-uk-notify/maven/notifications-java-client for instructions.
import uk.gov.service.notify.NotificationClient;
import uk.gov.service.notify.Notification;
import uk.gov.service.notify.NotificationList;
import uk.gov.service.notify.SendEmailResponse;
import uk.gov.service.notify.SendSmsResponse;
NotificationClient client = new NotificationClient(apiKey);
Generate an API key by signing in to GOV.UK Notify and going to the API integration page.
Click here to expand for more information.
Map<String, String> personalisation = new HashMap<>();
personalisation.put("name", "Jo");
personalisation.put("reference_number", "13566");
SendSmsResponse response = client.sendSms(templateId, mobileNumber, personalisation, "yourReferenceString", emailReplyToId);
If the request is successful, the SendSmsResponse is returned from the client. Attributes of the SendSmsResponse are listed below.
Click here to expand for more information.
UUID notificationId;
Optional<String> reference;
UUID templateId;
int templateVersion;
String templateUri;
String body;
Optional<String> fromNumber;
Otherwise the client will raise a NotificationClientException
:
status code | error message |
---|---|
429 |
429 { "errors": [{ "error": "RateLimitError", "message": "Exceeded rate limit for key type live of 60 requests per second" }] } |
429 |
429 { "errors": [{ "error": "TooManyRequestsError", "message": "Exceeded send limits (50) for today" }] } |
400 |
400 { "errors": [{ "error": "BadRequestError", "message": "Can"t send to this recipient using a team-only API key" }] } |
400 |
400 { "errors": [{ "error": "BadRequestError", "message": ""Can"t send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"" }] } |
Click here to expand for more information.
The mobile number the SMS notification is sent to.
The template id is visible on the template page in the application.
An optional unique identifier for the notification or an identifier for a batch of notifications. reference
can be an empty string or null.
If a template has placeholders, you need to provide their values, for example:
Map<String, String> personalisation = new HashMap<>();
personalisation.put("name", "Jo");
personalisation.put("reference_number", "13566");
Optional. Specifies the identifier of the text message sender to set for the notification. The identifiers are found in your service Settings, when you 'Manage' your 'Text message senders'. If you omit this argument your default text message sender will be for the notification.
Click here to expand for more information.
HashMap<String, String> personalisation = new HashMap<>();
personalisation.put("name", "Jo");
personalisation.put("reference_number", "13566");
SendEmailResponse response = client.sendEmail(templateId, emailAddress, personalisation, reference, emailReplyToId);
If the request is successful, the SendEmailResponse is returned from the client. Attributes of the SendEmailResponse are listed below.
Click here to expand for more information.
UUID notificationId;
Optional<String> reference;
UUID templateId;
int templateVersion;
String templateUri;
String body;
String subject;
Optional<String> fromEmail;
Otherwise the client will raise a NotificationClientException
:
error.status_code |
error.message |
---|---|
429 |
[{ "error": "RateLimitError", "message": "Exceeded rate limit for key type TEAM of 10 requests per 10 seconds" }] |
429 |
[{ "error": "TooManyRequestsError", "message": "Exceeded send limits (50) for today" }] |
400 |
[{ "error": "BadRequestError", "message": "Can"t send to this recipient using a team-only API key" ]} |
400 |
[{ "error": "BadRequestError", "message": "Can"t send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode" }] |
Click here for more information
The email address the email notification is sent to.
The template id is visible on the template page in the application.
If a template has placeholders, you need to provide their values, for example:
Map<String, String> personalisation = new HashMap<>();
personalisation.put("name", "Jo");
personalisation.put("reference_number", "13566");
An optional identifier you generate. The reference can be used as a unique reference for the notification. Because Notify does not require this reference to be unique you could also use this reference to identify a batch or group of notifications.
You can omit this argument if you do not require a reference for the notification.
Optional. Specifies the identifier of the email reply-to address to set for the notification. The identifiers are found in your service Settings, when you 'Manage' your 'Email reply to addresses'. If you omit this argument your default email reply-to address will be set for the notification.
The letter must contain:
- mandatory address fields
- optional address fields if applicable
- fields from template
Click here to expand for more information.
HashMap<String, String> personalisation = new HashMap<>();
personalisation.put("address_line_1", "The Occupier"); // mandatory address field
personalisation.put("address_line_2", "Flat 2"); // mandatory address field
personalisation.put("address_line_3", "123 High Street"); // optional address field
personalisation.put("address_line_4", "Richmond upon Thames"); // optional address field
personalisation.put("address_line_5", "London"); // optional address field
personalisation.put("address_line_6", "Middlesex"); // optional address field
personalisation.put("postcode", "SW14 6BH"); // mandatory address field
personalisation.put("application_id", "1234"); // field from template
personalisation.put("application_date", "2017-01-01"); // field from template
SendLetterResponse response = client.sendLetter(templateId, personalisation, "yourReferenceString");
If the request is successful, the SendLetterResponse is returned from the client. Attributes of the SendLetterResponse are listed below.
Click here to expand for more information.
UUID notificationId;
Optional<String> reference;
UUID templateId;
int templateVersion;
String templateUri;
String body;
String subject;
Otherwise the client will raise a NotificationClientException
:
error.status_code |
error.message |
---|---|
429 |
[{ "error": "RateLimitError", "message": "Exceeded rate limit for key type live of 10 requests per 20 seconds" }] |
429 |
[{ "error": "TooManyRequestsError", "message": "Exceeded send limits (50) for today" }] |
400 |
[{ "error": "BadRequestError", "message": "Cannot send letters with a team api key" ]} |
400 |
[{ "error": "BadRequestError", "message": "Cannot send letters when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode" }] |
400 |
[{ "error": "ValidationError", "message": "personalisation address_line_1 is a required property" }] |
Click here to expand for more information.
The template id is visible on the template page in the application.
The letter must contain:
- mandatory address fields
- optional address fields if applicable
- fields from template
If you are sending a letter, you will need to provide the address fields in the format "address_line_#"
, numbered from 1 to 6, and also the "postcode"
field
The fields "address_line_1"
, "address_line_2"
and "postcode"
are required.
An optional unique identifier for the notification or an identifier for a batch of notifications. reference
can be an empty string or null.
Click here to expand for more information.
Notification notification = client.getNotificationById(notificationId);
If successful a notification
is returned. Below is a list of attributes in a notification
.
Click here to expand for more information.
UUID id;
Optional<String> reference;
Optional<String> emailAddress;
Optional<String> phoneNumber;
Optional<String> line1;
Optional<String> line2;
Optional<String> line3;
Optional<String> line4;
Optional<String> line5;
Optional<String> line6;
Optional<String> postcode;
String notificationType;
String status;
UUID templateId;
int templateVersion;
String templateUri;
String body;
Optional<String subject;
DateTime createdAt;
Optional<DateTime> sentAt;
Optional<DateTime> completedAt;
Optional<DateTime> estimatedDelivery;
Otherwise the client will raise a NotificationClientException
.
error.status_code |
error.message |
---|---|
404 |
[{ "error": "NoResultFound", "message": "No result found" }] |
400 |
[{ "error": "ValidationError", "message": "id is not a valid UUID" }] |
Click here to expand for more information.
NotificationList notification = client.getNotifications(status, notificationType, reference, olderThanId);
If successful a NotificationList
is returned. Below is a list of attributes in aNotificationList
.
Click here to expand for more information.
List<Notification> notifications;
String currentPageLink;
Optional<String> nextPageLink;
Otherwise the client will raise a NotificationClientException
.
error.status_code |
error.message |
---|---|
404 |
[{ "error": "ValidationError", "message": "bad status is not one of [created, sending, sent, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, accepted, received]" }] |
400 |
[{ "error": "ValidationError", "message": "Apple is not one of [sms, email, letter]" }] |
Click here to expand for more information.
You can filter by:
created
- the message is queued to be sent to the provider. The notification typically only remains in this state for a few seconds.sending
- the message has been passed on to our providers to send to the recipient, and we are waiting for delivery information.delivered
- the message was successfully delivered.failed
- this will return all failure statusespermanent-failure
,temporary-failure
andtechnical-failure
.permanent-failure
- the provider was unable to deliver message, email does not exist; remove this recipient from your list.temporary-failure
- the provider was unable to deliver message, email box was full; you can try to send the message again.technical-failure
- Notify had a technical failure; you can try to send the message again.
You can omit this argument to ignore this filter.
text message
You can filter by:
created
- the message is queued to be sent to our providers. The notification typically only remains in this state for a few seconds.sending
- the message has been passed on to our providers to send to the recipient, and we are waiting for delivery information.delivered
- the message was successfully delivered.sent
- the message was delivered internationally - we may not receive additional status updates depending on the recipient's country and telecoms provider.failed
- this will return all failure statusespermanent-failure
,temporary-failure
andtechnical-failure
.permanent-failure
- the provider was unable to deliver message, phone number does not exist; remove this recipient from your list.temporary-failure
- the provider was unable to deliver message, the phone was turned off; you can try to send the message again.technical-failure
- Notify had a technical failure; you can try to send the message again.
You can omit this argument to ignore this filter.
letter
You can filter by:
accepted
- Notify is in the process of printing and posting the lettertechnical-failure
- Notify had an unexpected error while sending to our printing provider
You can omit this argument to ignore this filter.
This is the reference
you gave at the time of sending the notification. The reference
can be a unique identifier for the notification or an identifier for a batch of notifications.
You can get the notifications older than a given Notification.notificationId.
- sms
- letter
This will return the latest version of the template. Use getTemplateVersion to retrieve a specific template version.
Click here to expand for more information.
Template template = client.getTemplateById(templateId);
Click here to expand for more information.
UUID id;
String templateType;
DateTime createdAt;
Optional<DateTime> updatedAt;
String createdBy;
int version;
String body;
Optional<String> subject;
Optional<Map<String, Object>> personalisation;
Otherwise the client will raise a NotificationClientException
.
error.status_code |
error.message |
---|---|
404 |
[{ "error": "NoResultFound", "message": "No Result Found" }] |
400 |
[{ "error": "ValidationError", "message": "id is not a valid UUID" }] |
Click here to expand for more information.
The template id is visible on the template page in the application.
This will return the template for the given id and version.
Click here to expand for more information.
Template template = client.getTemplateVersion(templateId, version);
Click here to expand for more information.
UUID id;
String templateType;
DateTime createdAt;
Optional<DateTime> updatedAt;
String createdBy;
int version;
String body;
Optional<String> subject;
Optional<Map<String, Object>> personalisation;
Otherwise the client will raise a NotificationClientException
:
error.status_code |
error.message |
---|---|
404 |
[{ "error": "NoResultFound", "message": "No Result Found" }] |
400 |
[{ "error": "ValidationError", "message": "id is not a valid UUID" }] |
Click here to expand for more information.
The template id is visible on the template page in the application.
A history of the template is kept. There is a link to See previous versions
on the template page in the application.
This will return the latest version for each template for your service.
Click here to expand for more information.
TemplateList templates = client.getAllTemplates(templateType);
Click here to expand for more information.
List<Template> templates;
If the response is successful, a TemplateList is returned.
If no templates exist for a template type or there no templates for a service, the templates list will be empty.
Click here to expand for more information.
You can filter the templates by the following options:
email
sms
letter
You can also pass in an empty string or null to ignore the filter.
This will return the contents of a template with the placeholders replaced with the given personalisation.
Click here to expand for more information.
TemplatePreview templatePreview = client.getTemplatePreview(templateId, personalisation)
Click here to expand for more information.
UUID id;
String templateType;
int version;
String body;
Optional<String> subject;
Otherwise the client will raise a NotificationClientException
:
error.status_code |
error.message |
---|---|
400 |
[{ "error": "NoResultFound", "message": "No result found" }] |
400 |
[{ "error": "ValidationError", "message": "id is not a valid UUID" }] |
Click here to expand for more information.
The template id is visible on the template page in the application.
If a template has placeholders, you need to provide their values. personalisation
can be an empty or null in which case no placeholders are provided for the notification.
Map<String, String> personalisation = new HashMap<>();
personalisation.put("name", "Jo");
personalisation.put("reference_number", "13566");
ReceivedTextMessageList response = client.getReceivedTextMessages(olderThanId);
Click here to expand for more information.
If the request is successful, response
will be a ReceivedTextMessageList
:
private List<ReceivedTextMessage> receivedTextMessages;
private String currentPageLink;
private String nextPageLink;
A ReceivedTextMessage
will have the following properties -
private UUID id;
private String notifyNumber;
private String userNumber;
private UUID serviceId;
private String content;
private DateTime createdAt;