2016-04-15 10:56:47 +01:00
# API documenation
## 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-04-15 10:56:47 +01:00
## About GOV.UK Notify
2016-04-14 10:57:37 +01:00
GOV.UK Notify is a cross-government platform, currently in beta, that lets government services send notifications by sms or email.
2016-04-04 13:42:41 +01:00
There are two ways to send notifications:
2016-04-05 10:17:18 +01:00
* use the [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) web application
2016-04-14 10:57:37 +01:00
* [integrate your web applications or back office systems ](#integrateNotify ) 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-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-04-15 10:56:47 +01:00
1. Register for a [GOV.UK Notify ](https://www.notifications.service.gov.uk/ ) account. You will 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-15 10:56:47 +01:00
At first your service will be in trial mode. In trial mode you will only be able to send test sms and email notifications to your own mobile number or email address. 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-04-25 11:45:39 +01:00
You can provide all your developers with keys so they can experiment in the Sandbox environment. But keep the number of keys for real integrations to a minimum number of people on your team.
2016-04-04 14:37:35 +01:00
2016-04-12 13:26:47 +01:00
<a name="integrateNotify"></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-04-15 10:56:47 +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-11 09:31:24 +01:00
There are two ways to integrate the API into your service:
2016-04-15 10:56:47 +01:00
2016-04-14 16:40:30 +01:00
* use one of the client libraries provided by GOV.UK Notify:
2016-04-25 10:20:55 +01:00
2016-04-25 11:45:39 +01:00
* [Python library ](https://github.com/alphagov/notifications-python-client )
2016-04-14 16:40:30 +01:00
* [PHP library] (https://github.com/alphagov/notifications-php-client)
* [Java library] (https://github.com/alphagov/notifications-java-client)
2016-04-25 10:20:55 +01:00
2016-04-11 15:33:56 +01:00
* develop your own integration to produce requests in the correct format
2016-04-11 09:31:24 +01:00
2016-04-25 09:56:22 +01:00
GOV.UK Notify uses [JSON Web Tokens (JWT) ](https://jwt.io/ ) 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 11:45: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-04-14 09:51:46 +01:00
A JSON Web Token contains, in encrypted format:
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-04-12 16:35:10 +01:00
**Important:** API keys are secret, so save them somewhere safe. Do not commit API keys to public source code repositories.
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-04-14 09:51:46 +01:00
JSON Web Token standard claims:
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
}
```
GOV.UK Notify application-specific claims:
```
{
iss: 'string', // service id
iat: 0, // creation time in epoch seconds (UTC)
}
```
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-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-04-25 09:56:22 +01:00
These provide example code for calling the API and for constructing the API tokens.
2016-04-12 16:29:05 +01:00
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-06 15:44:02 +01:00
* send a notification
* retrieve one notification
2016-04-04 14:37:35 +01:00
* retrieve all notifications
2016-04-05 10:17:18 +01:00
To send a text 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-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-04-25 13:11:55 +01:00
* `to` is the phone number or email address (required)
2016-04-12 13:31:56 +01:00
* `template` is the template ID to send (required)
2016-04-14 16:40:30 +01:00
2016-04-25 10:49:47 +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-14 16:40:30 +01:00
2016-04-14 10:57:37 +01:00
* `personalisation` (optional) specifies the values for the placeholders in your templates
2016-04-25 10:49:47 +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-04-11 16:16:38 +01:00
The response will be:
```
{
'data':{
'notification': {
'id':1
}
}
}
```
2016-04-14 16:40:30 +01:00
where `id` is the unique identifier for the notification - you will use this id to retrieve the status of a notification.
2016-04-14 09:51:46 +01:00
2016-04-06 15:44:02 +01:00
To retrieve the status of a single text or email notification:
2016-04-04 14:37:35 +01:00
```
GET /notifications/{id}
```
```
{
'data':{
'notification': {
2016-04-12 16:29:05 +01:00
'status':'delivered',
'created_at':'2016-01-01T09:00:00.999999Z',
2016-04-04 14:37:35 +01:00
'to':'+447827992607',
2016-04-12 16:29:05 +01:00
'template_type':'sms',
'sent_at':'2016-01-01T09:01:00.999999Z',
2016-04-04 14:37:35 +01:00
'id':1,
'message':'...',
2016-04-11 15:33:56 +01:00
'job_id':1,
2016-04-04 14:37:35 +01:00
'sender':'sms-partner'
}
}
}
```
2016-04-12 13:21:14 +01:00
where:
2016-04-15 10:56:47 +01:00
2016-04-14 09:51:46 +01:00
* `status` is the the status of the notification; this can be `sending` , `delivered` , or `failed`
2016-04-25 13:11:55 +01:00
* `to` is the phone number or email address
2016-04-12 16:29:05 +01:00
* `template_type` is `sms` or `email`
2016-04-25 13:11:55 +01:00
* `sent_at` is the full timestamp, in Coordinated Universal Time (UTC), at which the notification was sent
2016-04-12 13:31:56 +01:00
* `message` is the content of message
2016-04-25 13:11:55 +01:00
* `job_id` is the unique identifier for the process of sending and retreiving one or more notifications
2016-04-14 16:40:30 +01:00
* `sender` may be the provider
2016-04-06 15:44:02 +01:00
2016-04-15 10:56:47 +01:00
The above fields are populated once the message has been processed; initially you get back the [response ](#coderesponse ) indicated above.
2016-04-06 15:44:02 +01:00
To get the status of all notifications:
```
GET /notifications
```
```
{
'data':[{
'notification': {
2016-04-12 16:29:05 +01:00
'status':'delivered',
'created_at':'2016-01-01T09:00:00.999999Z',
2016-04-06 15:44:02 +01:00
'to':'+447827992607',
2016-04-12 16:29:05 +01:00
'template_type':'sms',
'sent_at':'2016-01-01T09:01:00.999999Z',
2016-04-06 15:44:02 +01:00
'id':1,
'message':'...',
2016-04-25 13:11:55 +01:00
'job_id':1,
2016-04-06 15:44:02 +01:00
'sender':'sms-partner'
}
},
{
'notification': {
2016-04-12 16:29:05 +01:00
'status':'delivered',
'created_at':'2016-01-01T09:00:00.999999Z',
2016-04-06 15:44:02 +01:00
'to':'+447827992607',
2016-04-12 16:29:05 +01:00
'template_type':'email',
'sent_at':'2016-01-01T09:01:00.999999Z',
2016-04-06 15:44:02 +01:00
'id':1,
'message':'...',
2016-04-12 16:29:05 +01:00
'job_id':1,
2016-04-06 15:44:02 +01:00
'sender':'email-partner'
}
2016-04-15 10:56:47 +01:00
}…]
2016-04-06 15:44:02 +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-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