2016-04-15 10:56:47 +01:00
# API documenation
2016-05-03 10:28:49 +01:00
<a name="about_doc"></a>
2016-04-15 10:56:47 +01:00
## About this document
2016-04-14 10:57:37 +01:00
This document is for central government developers, technical architects, and service managers 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-03 10:28:49 +01:00
<a name="about_Notify"></a>
2016-04-15 10:56:47 +01:00
## About GOV.UK Notify
2016-05-03 13:32:51 +01:00
GOV.UK Notify is a cross-government platform that lets government services send notifications by sms or email. It's currently in beta.
2016-04-04 13:42:41 +01:00
2016-04-27 11:16:11 +01:00
There are 2 ways to send notifications:
2016-04-04 13:42:41 +01:00
2016-04-05 10:17:18 +01:00
* use the [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) web application
2016-05-03 10:28:49 +01:00
* [integrate your web applications or back office systems ](#integrate_Notify ) with the GOV.UK Notify API
2016-04-04 13:42:41 +01:00
To find out more about GOV.UK Notify, see the [Government as a Platform ](https://governmentasaplatform.blog.gov.uk/ ) blog.
2016-05-03 10:28:49 +01:00
<a name="before_start"></a>
2016-04-15 10:56:47 +01:00
## Before you start
2016-04-04 13:42:41 +01:00
To use GOV.UK Notify, you need:
2016-04-05 10:17:18 +01:00
2016-04-04 13:42:41 +01:00
* an email address from a local or central government organisation
* a mobile number for 2-factor authentication
2016-04-14 10:57:37 +01:00
<a name="quickstart"></a>
2016-04-15 10:56:47 +01:00
## Quick start guide to GOV.UK Notify
2016-04-04 13:42:41 +01:00
2016-04-05 10:17:18 +01:00
To get started:
2016-04-04 13:42:41 +01:00
2016-05-03 13:32:51 +01:00
1. Register for a [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) account. You'll need your mobile phone for 2-factor authentication.
2016-04-04 13:42:41 +01:00
2016-04-15 10:56:47 +01:00
2. Add a new service.
2016-04-04 13:42:41 +01:00
2016-04-25 13:30:41 +01:00
At first your service will be in trial mode. In trial mode, you can send up to 50 test notifications per day to yourself and your team members. When you’ re fully integrated and ready to go live, send a request to the GOV.UK Notify team.
2016-04-14 10:57:37 +01:00
2016-04-15 10:56:47 +01:00
3. Add a template so you can send sms and email notifications.
2016-04-25 13:11:55 +01:00
**Note: ** A template is required even if you send notifications with the GOV.UK Notify API.
2016-04-14 10:57:37 +01:00
2016-04-15 10:56:47 +01:00
You can personalise the template using double brackets for placeholders. For example:
> Dear ((name)),
>
> Your ((item)) is due for renewal on ((date)).
2016-04-04 13:42:41 +01:00
2016-04-15 10:56:47 +01:00
4. You can upload a csv file containing a header row matching the placeholders in your template, and data rows with values to use for the placeholders.
2016-04-04 13:42:41 +01:00
2016-04-15 10:56:47 +01:00
5. Send an sms or email notification.
2016-04-04 13:42:41 +01:00
2016-04-15 10:56:47 +01:00
6. If you intend to use the GOV.UK Notify API, create a new API key. This will be used to connect to the GOV.UK Notify API.
2016-04-04 14:37:35 +01:00
2016-05-03 11:36:01 +01:00
Each service can have multiple API keys. This allows you to integrate several systems, each with its own key. You can also have separate keys for your development and test environments.
2016-04-25 14:35:46 +01:00
2016-05-03 13:32:51 +01:00
**Important: ** API keys are secret, so save them somewhere safe. Don't commit API keys to public source code repositories.
2016-04-04 14:37:35 +01:00
2016-05-03 10:28:49 +01:00
<a name="integrate_Notify"></a>
2016-04-15 10:56:47 +01:00
## Integrate the GOV.UK Notify API into your service
2016-04-04 13:42:41 +01:00
2016-04-12 13:31:56 +01:00
GOV.UK Notify provides an API that allows you to create text and email notifications and get the status of notifications you have sent.
2016-04-11 15:33:56 +01:00
2016-05-03 10:28:49 +01:00
<a name="API_integration"></a>
2016-05-04 15:45:38 +01:00
## API integration
2016-04-04 14:37:35 +01:00
2016-04-15 12:11:07 +01:00

2016-04-11 12:15:38 +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-05 09:06:33 +01:00
* use one of the client libraries provided by GOV.UK Notify (see the Usage section in the Readme files):
* [Python library ](https://github.com/alphagov/notifications-python-client )
* [PHP library] (https://github.com/alphagov/notifications-php-client)
* [Java library] (https://github.com/alphagov/notifications-java-client)
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-04 15:45:38 +01:00
### Authenticating requests
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-04-25 13:39:31 +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-04 16:03:40 +01:00
To use a JSON Web Token you need:
2016-04-15 10:56:47 +01:00
* your service ID – identifies your service
* 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-04-11 15:33:56 +01:00
Use the [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) web application to find your service ID and create API keys.
2016-04-06 15:44:02 +01:00
2016-05-03 10:28:49 +01:00
<a name="JWT_claims"></a>
2016-04-15 10:56:47 +01:00
### JSON Web Tokens: claims
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
```
{
iss: 'string', // service id
iat: 0, // creation time in epoch seconds (UTC)
}
```
2016-05-04 15:45:38 +01:00
The header and the payload are Base64Url encoded.
2016-04-12 16:29:05 +01:00
The signing algorithm is the HMAC signature, using the provided key SHA256 hashing algorithm.
2016-04-11 16:27:23 +01:00
2016-05-03 10:28:49 +01:00
<a name="client_libraries"></a>
2016-04-15 10:56:47 +01:00
### API client libraries
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-04-25 09:56:22 +01:00
* [GOV.UK Notify Python library ](https://github.com/alphagov/notifications-python-client )
* [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-05-03 10:28:49 +01:00
<a name="API_endpoints"></a>
2016-04-15 10:56:47 +01:00
### API endpoints
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-04-26 10:41:07 +01:00
* send an [sms ](#sendsms ) or [email ](#sendemail ) notification
* [retrieve the status of one notification ](#get_single_notif )
* [retrieve the status of all notifications ](#get_all_notif )
2016-04-04 14:37:35 +01:00
2016-04-26 10:41:07 +01:00
<a name="sendsms"></a>
To send an sms notification:
2016-04-04 14:37:35 +01:00
```
POST /notifications/sms
```
```
{
'to': '+447700900404',
2016-04-06 15:44:02 +01:00
'template': 1,
'personalisation': {
'name': 'myname',
'date': '2016'
}
2016-04-04 14:37:35 +01:00
}
```
2016-05-04 16:34:51 +01:00
See below for explanations of the fields.
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
```
```
{
'to': 'email@gov .uk',
'template': 1,
'personalisation': {
'name': 'myname',
'date': '2016'
}
}
```
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
* `template` is a required string that indicates the template ID to send
2016-04-25 15:29:58 +01:00
2016-04-26 10:16:13 +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-03 15:31:01 +01:00
* `personalisation` is an optional array that specifies the values for the placeholders in your templates
2016-04-26 10:16:13 +01:00
**Note: ** You must provide all placeholders set up in your template. See [how to create placeholders in a template ](#quickstart ).
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
```
{
'data':{
'notification': {
'id':1
}
}
}
```
2016-05-03 13:32:51 +01:00
where `id` is the unique identifier for the notification – you'll use this id to retrieve the status of a notification.
2016-04-14 09:51:46 +01:00
2016-04-26 10:41:07 +01:00
<a name="get_single_notif"></a>
To retrieve the status of a single sms 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-04 15:45:38 +01:00
'notification':
{
'status': 'delivered',
'to': '07515 349 060',
'template': {'id': '5e427b42-4e98-46f3-a047-32c4a87d26bb',
'name': 'First template',
'template_type': 'sms'},
'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'},
'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-04-04 14:37:35 +01:00
}
```
2016-05-04 16:34:51 +01:00
See below 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>
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-05-04 15:45:38 +01:00
{'notifications':
[{
'status': 'delivered',
'to': '07515 349 060',
'template': {'id': '5e427b42-4e98-46f3-a047-32c4a87d26bb',
'name': 'First template',
'template_type': 'sms'},
'job': {'id': '5cc9d7ae-ceb7-4565-8345-4931d71f8c2e',
'original_file_name': 'Test run'},
'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'
},
{
'status': 'delivered',
'to': '07515 349 060',
'template': {'id': '5e427b42-4e98-46f3-a047-32c4a87d26bb',
'name': 'First template',
'template_type': 'sms'},
'job': {'id': 'f9043884-acac-46db-b2ea-f08cd8ec6d67',
'original_file_name': 'Test run'},
'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'
},
...
]
'links': {'last': '/notifications?page=3&template_type=sms&status=delivered',
'next': '/notifications?page=2&template_type=sms&status=delivered'},
'total': 162,
'page_size': 50
}
2016-04-06 15:44:02 +01:00
```
2016-05-04 15:45:38 +01:00
where:
2016-05-04 16:34:51 +01:00
* `status` is the the status of the notification; this can be `sending` , `delivered` , or `failed`
* `to` is the recipient's phone number or email address
* `name` is the name of the template used
* `template_type` is `sms` or `email`
* `created_at` is the full timestamp, in Coordinated Universal Time (UTC), at which GOV.UK Notify created the data object
* `updated_at` is the full timestamp, in Coordinated Universal Time (UTC), at which the data object was updated
* `sent_at` is the full timestamp, in Coordinated Universal Time (UTC), at which the notification was sent
2016-05-05 09:06:33 +01:00
* `job` is empty if you are using the API:
* `id` is the ???
* `original_file_name` is the name of the CSV file, if used
2016-05-04 16:34:51 +01:00
* `id` is the unique identifier for the process of sending and retreiving one or more notifications
2016-05-05 09:06:33 +01:00
* `content_char_count` is populated only for sms notifications; indicates the full character count, including placeholders
* `service` is the service ID ???
* `reference` is populated only for email notifications; used in the Notifications API so you can ignore it.
2016-05-04 16:34:51 +01:00
* `sent_by` is the name of the provider
2016-05-04 15:45:38 +01:00
* `links` :
2016-05-04 16:34:51 +01:00
* `last` is the url of the last page of notifications
2016-05-04 15:45:38 +01:00
* `next` is the url of the next page of notifications
2016-05-04 16:34:51 +01:00
* `total` is total number of notifications for the service of the given template type
* `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-04 16:34:51 +01:00
The above fields are populated once the message has been processed; initially you get back the [response ](#coderesponse ) indicated above.
2016-05-04 15:45:38 +01:00
2016-04-11 16:16:38 +01:00
This list is split into pages. To scroll through the pages run:
2016-04-04 14:37:35 +01:00
2016-04-06 15:44:02 +01:00
```
GET /notifications?&page=2
```
2016-04-04 14:37:35 +01:00
2016-05-04 15:45:38 +01:00
<a name="autherror_code"></a>
## Authorisation error messages
Error code | Body | Meaning
--- | --- | ---
2016-05-04 16:03:40 +01:00
401 | {"result": "error", "message": "Unauthorized, authentication token must be provided"} | Authorisation header is missing from request
401 | {"result": "error", "message": "Unauthorized, authentication bearer scheme must be used"} | Authorisation header is missing bearer
403 | {"result": "error", "message": "Invalid token: signature"} | Unable to decode the JSON Web Token signature, due to missing claims
403 | {"result": "error", "message": "Invalid credentials"} | Unable to find Service ID that was sent in the iss claim, no valid API for Service ID
403 | {"result": "error", "message": "Invalid token: expired"} | Token is expired; there is a 30 second time limit
2016-05-04 15:45:38 +01:00
## Other error messages
2016-05-05 09:06:33 +01:00
2016-05-04 15:45:38 +01:00
Error code | Body | Meaning
--- | --- | ---
2016-05-04 16:03:40 +01:00
429 | {"result": "error", "message": "Exceeded send limits (50) for today"} | Exceeded number of messages you can send per day
400 | {"result": "error", "message": "id: required field"} | Post body is badly formed: missing id field
400 | {"result":"error", "message":{'template': ['Missing personalisation: {tempalte_placeholder_name}']} | Post body is badly formed: missing personalisation data
400 | {"result":"error", "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-03 10:28:49 +01:00
<a name="Notify_code"></a>
2016-04-15 10:56:47 +01:00
### GOV.UK Notify API code
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
[GOV.UK Notify API ](https://github.com/alphagov/notifications-api )
2016-04-04 13:42:41 +01:00