2016-05-09 11:00:22 +01:00
<h1> API documentation</h1>
2016-04-15 10:56:47 +01:00
2016-05-09 09:51:59 +01:00
This document is for central government developers and technical architects who want to use the GOV.UK Notify platform to send notifications to users of their digital service.
2016-04-04 13:42:41 +01:00
2016-05-09 11:00:22 +01:00
* [Integrate the GOV.UK Notify API into your service ](#integrate_Notify )
2016-05-09 11:14:00 +01:00
* [Authenticate requests ](#AuthRequests )
2016-05-09 11:00:22 +01:00
* [JSON Web Tokens: claims ](#JWT_claims )
* [API client libraries ](#client_libraries )
2016-07-11 15:00:03 +01:00
* [Test your integration with GOV.UK Notify ](#test_integ )
2016-07-11 14:55:11 +01:00
* [API keys ](#API_keys )
2016-05-09 11:00:22 +01:00
* [API endpoints ](#API_endpoints )
2016-05-09 11:50:52 +01:00
* [Send notifications: POST ](#sendnotifications )
* [Retrieve notifications: GET ](#getnotifications )
2016-05-09 11:00:22 +01:00
* [Authorisation error messages ](#autherror_code )
* [Other error messages ](#othererror_code )
* [GOV.UK Notify API code ](#Notify_code )
2016-05-09 10:17:26 +01:00
2016-05-09 11:00:22 +01:00
2016-04-15 10:56:47 +01:00
2016-04-04 14:37:35 +01:00
2016-05-09 11:00:22 +01:00
<h2 id="integrate_Notify">Integrate the GOV.UK Notify API into your service</h2>
2016-04-04 13:42:41 +01:00
2016-04-27 11:16:11 +01:00
There are 2 ways to integrate the API into your service:
2016-04-15 10:56:47 +01:00
2016-05-23 10:32:31 +01:00
* use one of the client libraries provided by GOV.UK Notify:
2016-07-12 09:08:42 +01:00
2016-05-23 10:34:39 +01:00
* [Python library ](https://github.com/alphagov/notifications-python-client/blob/master/README.md#usage )
* [PHP library ](https://github.com/alphagov/notifications-php-client/blob/master/README.md#usage )
2016-05-12 15:37:42 +01:00
* [Java library ](https://github.com/alphagov/notifications-java-client )
2016-05-23 10:34:39 +01:00
2016-04-25 15:07:05 +01:00
* develop your own integration to produce requests in the correct format
2016-04-11 09:31:24 +01:00
2016-05-09 11:00:22 +01:00
<h3 id="AuthRequests">Authenticate requests</h3>
2016-05-04 15:45:38 +01:00
2016-05-12 15:37:42 +01:00
GOV.UK Notify uses [JSON Web Tokens (JWT) ](https://jwt.io/introduction/ ) for authentication and identification. The GOV.UK Notify client libraries encode and decode JSON Web Tokens when making requests to the GOV.UK Notify API. If you don’ t use one of these libraries, you must manually create tokens yourself.
2016-04-12 16:37:10 +01:00
2016-05-19 10:55:39 +01:00
For examples of how to encode and decode JSON Web Tokens, see [authentication.py ](https://github.com/alphagov/notifications-python-client/blob/master/notifications_python_client/authentication.py ) in the GOV.UK Notify Python client library, or the appropriate [PHP ](https://github.com/alphagov/notifications-php-client ) or [Java ](https://github.com/alphagov/notifications-java-client ) client library.
2016-04-06 15:44:02 +01:00
2016-05-05 11:32:40 +01:00
To create JSON Web Tokens you need:
2016-04-15 10:56:47 +01:00
2016-05-05 11:38:26 +01:00
* your Service ID – identifies your service
2016-04-15 10:56:47 +01:00
* your API key (in JSON Web Token terms this is called the client ID) – used to sign tokens during requests for API resources
2016-04-06 15:44:02 +01:00
2016-08-04 09:05:00 +01:00
To find your Service ID and create or revoke API keys, click on **API integration ** in the [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) web application.
2016-04-06 15:44:02 +01:00
2016-05-09 11:00:22 +01:00
<h3 id="JWT_claims">JSON Web Tokens: claims</h3>
2016-04-11 16:27:23 +01:00
2016-04-14 09:51:46 +01:00
JSON Web Tokens have a series of standard and application-specific claims.
2016-04-11 16:27:23 +01:00
2016-05-05 09:06:33 +01:00
JSON Web Token standard claims (these form the JSON Web Token header):
2016-04-11 16:27:23 +01:00
```
{
2016-04-14 16:40:30 +01:00
"alg": "HS256",
"typ": "JWT"
2016-04-11 16:27:23 +01:00
}
```
2016-05-05 09:06:33 +01:00
GOV.UK Notify application-specific claims (these form the JSON Web Token payload):
2016-04-11 16:27:23 +01:00
```
{
2016-06-16 11:58:45 +01:00
iss: "string", // Service ID
2016-05-05 11:32:40 +01:00
iat: 0, // creation time in epoch seconds (UTC)
2016-04-11 16:27:23 +01:00
}
```
2016-05-12 15:37:42 +01:00
The header and payload are Base64Url encoded.
2016-05-04 15:45:38 +01:00
2016-05-05 11:13:21 +01:00
The verify signature is created using the HMAC SHA256 hashing algorithm.
2016-04-11 16:27:23 +01:00
2016-05-09 11:00:22 +01:00
<h3 id="client_libraries">API client libraries</h3>
2016-04-12 16:29:05 +01:00
2016-04-25 09:56:22 +01:00
GOV.UK Notify supports the following client libraries:
2016-04-12 16:29:05 +01:00
2016-05-12 15:37:42 +01:00
* [GOV.UK Notify Python library ](https://github.com/alphagov/notifications-python-client )
2016-05-19 10:55:39 +01:00
* [GOV.UK Notify PHP library ](https://github.com/alphagov/notifications-php-client )
* [GOV.UK Notify Java library ](https://github.com/alphagov/notifications-java-client )
2016-04-12 16:29:05 +01:00
2016-05-05 09:06:33 +01:00
These provide example code for calling the API and for creating API tokens.
2016-04-12 16:29:05 +01:00
2016-07-11 15:00:03 +01:00
<h2 id="test_integ">Test your integration with GOV.UK Notify</h2>
2016-07-11 14:41:20 +01:00
Service teams should do all their testing within the GOV.UK Notify production environment (https://api.notifications.service.gov.uk).
You don’ t need different service accounts or environments. Instead, there are 3 types of API key that let you do functional and performance integration testing.
<h3 id="API_keys">API keys</h3>
2016-07-11 14:55:11 +01:00
The 3 types of API key that you can create within GOV.UK Notify are:
2016-07-11 14:41:20 +01:00
2016-07-12 09:08:42 +01:00
* [normal keys ](#normal_keys )
2016-07-11 15:04:21 +01:00
* [team keys ](#team_keys )
2016-07-12 09:08:42 +01:00
* [test keys ](#test_keys )
2016-07-11 14:41:20 +01:00
2016-07-11 15:07:55 +01:00
Type of key | Sends real messages? | Appears in activity and statistics? | Daily service limit
--- | --- | --- | ---
2016-07-11 15:43:30 +01:00
Normal key | Yes | Yes | 50 (trial), unlimited (live)
Team key | Yes (only to team members) | Yes | 50 (trial), unlimited (live)
2016-07-11 15:07:55 +01:00
Test key | No | No | Unlimited
2016-07-12 09:08:42 +01:00
<h4 id="normal_keys">Normal keys</h4>
2016-07-11 14:41:20 +01:00
Normal keys have the same permissions as the service:
2016-07-11 15:00:03 +01:00
* when the service is in trial mode, you can send only to members of your team and you are restricted to 50 messages per day
2016-07-11 14:41:20 +01:00
* when the service is live, you can use the key to send messages to anyone
2016-07-12 09:08:42 +01:00
* three
2016-07-11 14:41:20 +01:00
Messages sent with a normal key show up on your dashboard and count against your text message and email allowances.
There is no need to generate a new key when the service moves from trial to live.
Don’ t use your normal key for automated testing.
2016-07-12 09:08:42 +01:00
<h4 id="team_keys">Team keys</h4>
2016-07-11 14:41:20 +01:00
2016-07-11 14:55:11 +01:00
Use team keys for end-to-end functional testing.
2016-07-11 14:41:20 +01:00
A team key lets you send real messages to members of your team. You get an error if you try to send messages to anyone else.
Messages sent with a team key show up on your dashboard and count against your text message and email allowances.
2016-07-12 09:08:42 +01:00
<h4 id="test_keys">Test keys</h4>
2016-07-11 14:55:11 +01:00
Use test keys to test the performance of your service and its integration with GOV.UK Notify under load.
2016-07-11 15:46:42 +01:00
Test keys don’ t send real messages but they do generate realistic responses. There’ s no restriction on who you can send to or how many messages you can send per day.
2016-07-11 14:55:11 +01:00
Messages sent using a test key don’ t show up on your dashboard or count against your text message and email allowances.
2016-05-09 11:00:22 +01:00
<h2 id="API_endpoints">API endpoints</h2>
2016-04-04 13:42:41 +01:00
2016-04-12 13:21:14 +01:00
You can use the GOV.UK Notify API to:
2016-04-15 10:56:47 +01:00
2016-05-12 15:37:42 +01:00
* send a [text ](#sendtext ) or [email ](#sendemail ) notification
* [retrieve the status of one notification ](#get_single_notif )
2016-05-05 11:32:40 +01:00
* [retrieve the status of all notifications ](#get_all_notif )
2016-04-04 14:37:35 +01:00
2016-05-09 11:50:52 +01:00
<h3 id="sendnotifications">Send notifications: POST</h3>
2016-05-05 09:25:52 +01:00
2016-05-09 11:00:22 +01:00
<a name="sendtext"></a>
To send a text notification:
2016-04-04 14:37:35 +01:00
```
POST /notifications/sms
```
```
{
2016-06-16 11:58:45 +01:00
"to": "+447700987345",
"template": "f6895ff7-86e0-4d38-80ab-c9525856c3ff",
"personalisation": {
2016-06-22 14:56:24 +01:00
"name": "Bill",
"date": "3 January 2016"
2016-04-06 15:44:02 +01:00
}
2016-04-04 14:37:35 +01:00
}
```
2016-05-09 11:42:47 +01:00
See [below ](#fieldsforPOST ) for explanations of the fields.
2016-05-04 16:34:51 +01:00
2016-04-26 10:41:07 +01:00
<a name="sendemail"></a>
2016-04-06 15:44:02 +01:00
To send an email notification:
```
POST /notifications/email
```
```
{
2016-06-16 11:58:45 +01:00
"to": "email@gov .uk",
"template": "f6895ff7-86e0-4d38-80ab-c9525856c3ff",
"personalisation": {
2016-06-22 14:11:17 +01:00
"name": "Bill",
"date": "3 January 2016"
2016-04-06 15:44:02 +01:00
}
}
```
2016-05-09 11:42:47 +01:00
<a name="fieldsforPOST"></a>
2016-04-11 16:16:38 +01:00
where:
2016-04-15 10:56:47 +01:00
2016-05-03 15:31:01 +01:00
* `to` is a required string that indicates the recipient's phone number or email address
2016-05-05 11:50:33 +01:00
* `template` is a required string that indicates the Template ID to use
2016-05-12 15:37:42 +01:00
2016-05-05 11:50:33 +01:00
**Note: ** To access the Template ID from the [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) web application, go to **Text message templates ** or **Email templates ** and click on **API info ** .
2016-04-25 15:29:58 +01:00
2016-05-05 11:13:21 +01:00
* `personalisation` is an optional array that specifies the placeholders and values in your templates
2016-04-26 10:16:13 +01:00
2016-05-09 12:48:26 +01:00
**Note: ** You must provide all placeholders set up in your template. See [how to create placeholders in a template ](#beforestart ).
2016-04-14 10:57:37 +01:00
2016-04-12 13:21:14 +01:00
<a id="coderesponse"></a>
2016-05-04 11:19:09 +01:00
The response (status code 201) will be:
2016-04-11 16:16:38 +01:00
```
{
2016-06-16 11:58:45 +01:00
"data":{
"notification": {
"id":1
},
2016-06-22 14:11:17 +01:00
"body": "Dear Bill, your licence is due for renewal on 3 January 2016.",
2016-06-16 11:58:45 +01:00
"template_version": 1,
"subject": "Licence renewal"
2016-05-12 15:37:42 +01:00
}
2016-04-11 16:16:38 +01:00
}
```
2016-06-16 11:58:45 +01:00
where:
* `id` is the unique identifier for the notification – you'll use this ID to retrieve the status of a notification
* `body` is the content of the message
* `template_version` is the version number of the template used
* `subject` is the subject of the email (present for email notifications only)
2016-04-14 09:51:46 +01:00
2016-05-09 11:50:52 +01:00
<h3 id="getnotifications">Retrieve notifications: GET</h3>
2016-05-05 09:25:52 +01:00
2016-04-26 10:41:07 +01:00
<a name="get_single_notif"></a>
2016-05-09 11:02:18 +01:00
To retrieve the status of a single text or email notification:
2016-04-04 14:37:35 +01:00
```
GET /notifications/{id}
```
2016-05-04 11:19:09 +01:00
The response (status code 200) will be:
2016-04-04 14:37:35 +01:00
2016-05-12 14:22:49 +01:00
```
{
2016-06-16 11:58:45 +01:00
"notification": {
"status": "delivered",
"to": "07515 987 456",
"template": {
"id": "5e427b42-4e98-46f3-a047-32c4a87d26bb",
"name": "First template",
"template_type": "sms"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"created_at": "2016-04-26T15:29:36.891512+00:00",
"updated_at": "2016-04-26T15:29:38.724808+00:00",
"sent_at": "2016-04-26T15:29:37.230976+00:00",
"job": {
"id": "f9043884-acac-46db-b2ea-f08cd8ec6d67",
"original_file_name": "Test run"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"sent_at": "2016-04-26T15:29:37.230976+00:00",
"id": "f163deaf-2d3f-4ec6-98fc-f23fa511518f",
"content_char_count": 490,
"service": "5cf87313-fddd-4482-a2ea-48e37320efd1",
"reference": None,
2016-06-22 14:11:17 +01:00
"sent_by": "mmg",
2016-06-22 14:12:57 +01:00
"body": "Dear Bill, your licence is due for renewal on 3 January 2016."
2016-06-22 14:11:17 +01:00
"date": "3 January 2016"
2016-05-12 15:37:42 +01:00
}
2016-04-04 14:37:35 +01:00
}
```
2016-05-09 11:44:46 +01:00
See [below ](#fieldsforGET ) for explanations of the fields.
2016-04-06 15:44:02 +01:00
2016-04-26 10:41:07 +01:00
<a name="get_all_notif"></a>
2016-05-12 15:37:42 +01:00
To retrieve the status of all notifications:
2016-04-06 15:44:02 +01:00
```
GET /notifications
```
2016-05-04 11:19:09 +01:00
The response (status code 200) will be:
2016-04-06 15:44:02 +01:00
```
2016-06-16 11:58:45 +01:00
{"notifications":
2016-05-12 15:37:42 +01:00
[{
2016-06-16 11:58:45 +01:00
"status": "delivered",
"to": "07515 987 456",
"template": {
"id": "5e427b42-4e98-46f3-a047-32c4a87d26bb",
"name": "First template",
"template_type": "sms"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"job": {
"id": "5cc9d7ae-ceb7-4565-8345-4931d71f8c2e",
"original_file_name": "Test run"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"created_at": "2016-04-26T15:30:49.968969+00:00",
"updated_at": "2016-04-26T15:30:50.853844+00:00",
"sent_at": "2016-04-26T15:30:50.383634+00:00",
"id": "04ae9bdc-92aa-4d6c-a0da-48587c03d4c7",
"content_char_count": 446,
"service": "5cf87313-fddd-4482-a2ea-48e37320efd1",
"reference": None,
"sent_by": "mmg"
2016-05-12 15:37:42 +01:00
},
{
2016-06-16 11:58:45 +01:00
"status": "delivered",
"to": "07515 987 456",
"template": {
"id": "5e427b42-4e98-46f3-a047-32c4a87d26bb",
"name": "First template",
"template_type": "sms"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"job": {
"id": "f9043884-acac-46db-b2ea-f08cd8ec6d67",
"original_file_name": "Test run"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"created_at": "2016-04-26T15:29:36.891512+00:00",
"updated_at": "2016-04-26T15:29:38.724808+00:00",
"sent_at": "2016-04-26T15:29:37.230976+00:00",
"id": "f163deaf-2d3f-4ec6-98fc-f23fa511518f",
"content_char_count": 490,
"service": "5cf87313-fddd-4482-a2ea-48e37320efd1",
"reference": None,
"sent_by": "mmg"
2016-05-12 15:37:42 +01:00
},
…
],
2016-06-16 11:58:45 +01:00
"links": {
"last": "/notifications?page=3&template_type=sms&status=delivered",
"next": "/notifications?page=2&template_type=sms&status=delivered"
2016-05-12 15:37:42 +01:00
},
2016-06-16 11:58:45 +01:00
"total": 162,
"page_size": 50
2016-05-12 15:37:42 +01:00
}
2016-04-06 15:44:02 +01:00
```
2016-05-09 11:44:46 +01:00
<a name="fieldsforGET"></a>
2016-05-04 15:45:38 +01:00
where:
2016-05-25 12:11:00 +01:00
* `status` is the status of the notification; this can be `sending` , `delivered` , `permanently failed` , `temporarily failed` , or `technical failure` (see [Delivery and failure ](https://www.notifications.service.gov.uk/delivery-and-failure ))
2016-05-04 16:34:51 +01:00
* `to` is the recipient's phone number or email address
2016-05-05 09:18:21 +01:00
* `template` :
2016-05-05 11:50:33 +01:00
* `id` is the Template ID
2016-05-05 09:18:21 +01:00
* `name` is the name of the template used
* `template_type` is `sms` or `email`
2016-05-05 11:13:21 +01:00
* `created_at` is the full timestamp, in Coordinated Universal Time (UTC), at which GOV.UK Notify created the notification
* `updated_at` is the full timestamp, in Coordinated Universal Time (UTC), at which the notification was updated
2016-05-05 11:32:40 +01:00
* `sent_at` is the full timestamp, in Coordinated Universal Time (UTC), at which GOV.UK Notify sent the notification
* `job` is empty if you are using the API to send notifications:
2016-05-05 11:13:21 +01:00
* `id` is the job ID
2016-05-19 10:55:39 +01:00
* `original_file_name` is the name of the .csv file, if used
2016-05-05 09:57:11 +01:00
* `id` is the unique identifier for the process of sending and retrieving one or more notifications
2016-05-09 14:39:58 +01:00
* `content_char_count` indicates the full character count of the text notification, including placeholders (populated for text notifications only)
2016-05-05 11:38:26 +01:00
* `service` is your Service ID
2016-05-05 09:45:58 +01:00
* `reference` is used in the Notifications API so you can ignore it (populated for email notifications only)
2016-05-04 16:34:51 +01:00
* `sent_by` is the name of the provider
2016-05-12 15:37:42 +01:00
* `links` :
2016-05-19 14:44:09 +01:00
* `last` is the URL of the last page of notifications
* `next` is the URL of the next page of notifications
2016-05-05 10:26:44 +01:00
* `total` is the total number of notifications sent by the service using the given template type
2016-05-05 11:38:26 +01:00
* `page_size` is an optional integer indicating the number of notifications per page; if not provided, defaults to 50
2016-05-04 15:45:38 +01:00
2016-05-23 10:48:34 +01:00
The ``GET /notifications` ` request accepts the following query string parameters:
2016-05-24 13:45:21 +01:00
2016-05-23 10:40:44 +01:00
* `template_type` - `sms` or `email` (you can enter `template_type` twice)
2016-05-25 12:11:00 +01:00
* `status` - `sending` , `delivered` , `permanently failed` , `temporarily failed` , or `technical failure` (see [Delivery and failure ](https://www.notifications.service.gov.uk/delivery-and-failure ))
2016-05-23 10:40:44 +01:00
* `page` - page number
* `page_size` - number of notifications per page; defaults to 50
* `limit_days` - number of days; defaults to 7
2016-05-04 15:45:38 +01:00
2016-05-23 10:40:44 +01:00
For example, to scroll through the pages in the list, run:
2016-04-04 14:37:35 +01:00
2016-04-06 15:44:02 +01:00
```
2016-05-23 10:40:44 +01:00
GET /notifications?page=2
2016-04-06 15:44:02 +01:00
```
2016-04-04 14:37:35 +01:00
2016-05-23 10:40:44 +01:00
2016-05-09 11:00:22 +01:00
<h3 id="autherror_code">Authorisation error messages</h3>
2016-05-04 15:45:38 +01:00
Error code | Body | Meaning
--- | --- | ---
2016-06-20 13:26:04 +01:00
401 | {"result": "error", <br> "message": {"token": ["Invalid token: Unauthorized, authentication token must be provided"]}} | Authorisation header is missing from request
401 | {"result": "error", <br> "message": {"token": ["Invalid token: Unauthorized, authentication bearer scheme must be used"]}} | Authorisation header is missing bearer
403 | {"result": "error", <br> "message": {"token": ["Invalid token: Invalid token: signature"]}} | Unable to decode the JSON Web Token signature, due to missing claims
403 | {"result": "error", <br> "message": {"token": ["Invalid token: Invalid credentials"]}} | Service ID in the `iss` claim is incorrect, or no valid API key for Service ID
403 | {"result": "error", <br> "message": {"token": ["Invalid token: no api keys for service"]}} | No valid API key for Service ID
403 | {"result": "error", <br> "message": {"token": ["Invalid token: expired"]}} | Token is expired; there is a 30 second time limit
2016-05-04 15:45:38 +01:00
2016-05-09 11:00:22 +01:00
<h3 id="othererror_code">Other error messages</h3>
2016-05-05 09:06:33 +01:00
2016-05-04 15:45:38 +01:00
Error code | Body | Meaning
--- | --- | ---
2016-05-09 14:47:06 +01:00
429 | {"result": "error", <br> "message": "Exceeded send limits (50) for today"} | You have reached the maximum number of messages you can send per day
2016-05-05 12:03:06 +01:00
400 | {"result": "error", <br> "message": "id: required field"} | Post body is badly formed: missing `id` field
2016-06-16 11:58:45 +01:00
400 | {"result":"error", <br> "message":{"template": ["Missing personalisation: {template_placeholder_name}"]} | Post body is badly formed: missing personalisation data
400 | {"result":"error", <br> "message"={"to": ["Invalid {notification_type} for restricted service")]} | Service is in trial mode; you cannot send messages to email addresses or phone numbers not belonging to team members
2016-05-04 15:45:38 +01:00
2016-05-09 11:13:06 +01:00
<h2 id="Notify_code">GOV.UK Notify API code</h2>
2016-04-12 09:58:09 +01:00
2016-04-12 10:19:25 +01:00
The GOV.UK Notify API code is open sourced at:
2016-04-12 09:58:09 +01:00
2016-05-12 15:37:42 +01:00
[GOV.UK Notify API ](https://github.com/alphagov/notifications-api )