Skip to content

Latest commit

 

History

History
1278 lines (923 loc) · 52.1 KB

File metadata and controls

1278 lines (923 loc) · 52.1 KB

Node.js client documentation

This documentation is for developers interested in using the GOV.UK Notify Node.js client to send emails, text messages or letters.

Set up the client

Install the client

Run the following in the command line:

npm install --save notifications-node-client

Create a new instance of the client

Add this code to your application:

var NotifyClient = require('notifications-node-client').NotifyClient

var notifyClient = new NotifyClient(apiKey)

To get an API key, sign in to GOV.UK Notify and go to the API integration page. You can find more information in the API keys section of this documentation.

Connect through a proxy (optional)

Add this code to your application:

proxyConfig = {
  host: proxyHost,
  port: proxyPort
}

notifyClient.setProxy(proxyConfig)

where the proxyConfig should be an object supported by axios.

Send a message

You can use GOV.UK Notify to send text messages, emails (including documents) and letters.

Send a text message

Method

notifyClient
  .sendSms(templateId, phoneNumber, {
    personalisation: personalisation,
    reference: reference,
    smsSenderId: smsSenderId
  })
  .then(response => console.log(response))
  .catch(err => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

templateId (required)

To find the template ID:

  1. Sign in to GOV.UK Notify.
  2. Go to the Templates page and select the relevant template.
  3. Select Copy template ID to clipboard.

For example:

'f33517ff-2a88-4f6e-b855-c550268ce08a'
phoneNumber (required)

The phone number of the recipient of the text message. This number can be a UK or international number. For example:

'+447900900123'
personalisation (required)

If a template has placeholder fields for personalised information such as name or reference number, you must provide their values in an object. For example:

{
  'first_name': 'Amala',
  'reference_number': '300241'
}
reference (required)

A unique identifier you create. This reference identifies a single unique notification or a batch of notifications. It must not contain any personal information such as name or postal address. If you do not have a reference, you must pass in an empty string or null. For example:

'your_reference_here'
smsSenderId (optional)

A unique identifier of the sender of the text message notification. For example:

'8e222534-7f05-4972-86e3-17c5d9f894e2'

To find the text message sender:

  1. Sign in to GOV.UK Notify.
  2. Go to the Settings page.
  3. In the Text Messages section, select Manage on the Text Message sender row.

In this screen, you can either:

  • copy the sender ID that you want to use and paste it into the method
  • select Change to change the default sender that the service will use, and select Save

You can leave out this argument if your service only has one text message sender, or if you want to use the default sender.

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': 'bfb50d92-100d-4b8b-b559-14fa3b091cda',
  'reference': null,
  'content': {
    'body': 'Some words',
    'from_number': '40604'
  },
  'uri': 'https://api.notifications.service.gov.uk/v2/notifications/ceb50d92-100d-4b8b-b559-14fa3b091cd',
  'template': {
    'id': 'ceb50d92-100d-4b8b-b559-14fa3b091cda',
    'version': 1,
    'uri': 'https://api.notifications.service.gov.uk/v2/templates/bfb50d92-100d-4b8b-b559-14fa3b091cda'
  }
}

If you are using the test API key, all your messages come back with a delivered status.

All messages sent using the team and guest list or live keys appear on your dashboard.

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "BadRequestError",
"message": "Can't send to this recipient using a team-only API key"
}]
Use the correct type of 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"
}]
Your service cannot send this notification in trial mode
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"
}]
Refer to API rate limits for more information
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (LIMIT NUMBER) for today"
}]
Refer to service limits for the limit number
500 [{
"error": "Exception",
"message": "Internal server error"
}]
Notify was unable to process the request, resend your notification

Send an email

Method

notifyClient
  .sendEmail(templateId, emailAddress, {
    personalisation: personalisation,
    reference: reference,
    emailReplyToId: emailReplyToId
  })
  .then(response => console.log(response))
  .catch(err => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

templateId (required)

To find the template ID:

  1. Sign in to GOV.UK Notify.
  2. Go to the Templates page and select the relevant template.
  3. Select Copy template ID to clipboard.

For example:

"f33517ff-2a88-4f6e-b855-c550268ce08a"
emailAddress (required)

The email address of the recipient. For example:

"sender@something.com"
personalisation (required)

If a template has placeholder fields for personalised information such as name or application date, you must provide their values in an object. For example:

{
  personalisation: {
    'first_name': 'Amala',
    'application_number': '300241'
  }
}
reference (required)

A unique identifier you create. This reference identifies a single unique notification or a batch of notifications. It must not contain any personal information such as name or postal address. If you do not have a reference, you must pass in an empty string or null. For example:

"your_reference_here"
emailReplyToId (optional)

This is an email address specified by you to receive replies from your users. You must add at least one reply-to email address before your service can go live.

To add a reply-to email address:

  1. Sign in to GOV.UK Notify.
  2. Go to the Settings page.
  3. In the Email section, select Manage on the Reply-to email addresses row.
  4. Select Add reply-to address.
  5. Enter the email address you want to use, and select Add.
emailReplyToId='8e222534-7f05-4972-86e3-17c5d9f894e2'

You can leave out this argument if your service only has one reply-to email address, or you want to use the default email address.

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': 'bfb50d92-100d-4b8b-b559-14fa3b091cda',
  'reference': null,
  'content': {
    'subject': 'Licence renewal',
    'body': 'Dear Bill, your licence is due for renewal on 3 January 2016.',
    'from_email': 'the_service@gov.uk'
  },
  'uri': 'https://api.notifications.service.gov.uk/v2/notifications/ceb50d92-100d-4b8b-b559-14fa3b091cd',
  'template': {
    'id': 'ceb50d92-100d-4b8b-b559-14fa3b091cda',
    'version': 1,
    'uri': 'https://api.notificaitons.service.gov.uk/service/your_service_id/templates/bfb50d92-100d-4b8b-b559-14fa3b091cda'
  }
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "BadRequestError",
"message": "Can't send to this recipient using a team-only API key"
}]
Use the correct type of 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"
}]
Your service cannot send this notification in trial mode
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"
}]
Refer to API rate limits for more information
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (LIMIT NUMBER) for today"
}]
Refer to service limits for the limit number
500 [{
"error": "Exception",
"message": "Internal server error"
}]
Notify was unable to process the request, resend your notification

Send a file by email

To send a file by email, add a placeholder to the template then upload a file. The placeholder will contain a secure link to download the file.

The links are unique and unguessable. GOV.UK Notify cannot access or decrypt your file.

Your file will be available to download for a default period of 26 weeks (6 months).

To help protect your files you can also:

  • ask recipients to confirm their email address before downloading
  • choose the length of time that a file is available to download

Add contact details to the file download page

  1. Sign in to GOV.UK Notify.
  2. Go to the Settings page.
  3. In the Email section, select Manage on the Send files by email row.
  4. Enter the contact details you want to use, and select Save.

Add a placeholder to the template

  1. Sign in to GOV.UK Notify.
  2. Go to the Templates page and select the relevant email template.
  3. Select Edit.
  4. Add a placeholder to the email template using double brackets. For example: "Download your file at: ((link_to_file))"

Your email should also tell recipients how long the file will be available to download.

Upload your file

You can upload PDF, CSV, .odt, .txt, .rtf, .xlsx and MS Word Document files. Your file must be smaller than 2MB. Contact the GOV.UK Notify team if you need to send other file types.

Pass the file object as a value into the personalisation argument. For example:

var fs = require('fs')

fs.readFile('path/to/document.pdf', function (err, pdfFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: 'Amala',
      application_date: '2018-01-01',
      link_to_file: notifyClient.prepareUpload(pdfFile)
    }
  }).then(response => console.log(response)).catch(err => console.error(err))
})

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful
CSV Files

Uploads for CSV files should use the isCsv option on the prepareUpload() function. For example:

var fs = require('fs')

fs.readFile('path/to/document.csv', function (err, csvFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: 'Amala',
      application_date: '2018-01-01',
      link_to_file: notifyClient.prepareUpload(csvFile, { isCsv: true })
    }
  }).then(response => console.log(response)).catch(err => console.error(err))
})

Ask recipients to confirm their email address before they can download the file

When a recipient clicks the link in the email you’ve sent them, they have to enter their email address. Only someone who knows the recipient’s email address can download the file.

This security feature is turned on by default.

Turn off email address check (not recommended)

If you do not want to use this feature, you can turn it off on a file-by-file basis.

To do this you will need version 5.2.0 of the Node.js client library, or a more recent version.

You should not turn this feature off if you send files that contain:

  • personally identifiable information
  • commercially sensitive information
  • information classified as ‘OFFICIAL’ or ‘OFFICIAL-SENSITIVE’ under the Government Security Classifications policy

To let the recipient download the file without confirming their email address, set the confirmEmailBeforeDownload option to false.

var fs = require('fs')

fs.readFile('path/to/document.pdf', function (err, pdfFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: 'Amala',
      application_date: '2018-01-01',
      link_to_file: notifyClient.prepareUpload(pdfFile, { confirmEmailBeforeDownload: false })
    }
  }).then(response => console.log(response)).catch(err => console.error(err))
})

Choose the length of time that a file is available to download

Set the number of weeks you want the file to be available using the retentionPeriod option.

You can choose any value between 1 week and 78 weeks.

To use this feature will need version 5.2.0 of the Node.js client library, or a more recent version.

If you do not choose a value, the file will be available for the default period of 26 weeks (6 months).

Files sent before 12 April 2023 had a longer default period of 78 weeks (18 months).

var fs = require('fs')

fs.readFile('path/to/document.pdf', function (err, pdfFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: 'Amala',
      application_date: '2018-01-01',
      link_to_file: notifyClient.prepareUpload(pdfFile, { retentionPeriod: '52 weeks' })
    }
  }).then(response => console.log(response)).catch(err => console.error(err))
})

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': '740e5834-3a29-46b4-9a6f-16142fde533a',
  'reference': 'your_reference_here',
  'content': {
    'subject': 'SUBJECT TEXT',
    'body': 'MESSAGE TEXT',
    'from_email': 'SENDER EMAIL'
  },
  'uri': 'https://api.notifications.service.gov.uk/v2/notifications/740e5834-3a29-46b4-9a6f-16142fde533a',
  'template': {
    'id': 'f33517ff-2a88-4f6e-b855-c550268ce08a',
    'version': INTEGER,
    'uri': 'https://api.notifications.service.gov.uk/v2/template/f33517ff-2a88-4f6e-b855-c550268ce08a'
  }
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "BadRequestError",
"message": "Can't send to this recipient using a team-only API key"
}]
Use the correct type of 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"
}]
Your service cannot send this notification in trial mode
400 [{
"error": "BadRequestError",
"message": "Unsupported file type '(FILE TYPE)'. Supported types are: '(ALLOWED TYPES)'"
}]
Wrong file type. You can only upload .pdf, .csv, .txt, .doc, .docx, .xlsx, .rtf or .odt files
400 [{
"error": "BadRequestError",
"message": "Unsupported value for retention_period '(PERIOD)'. Supported periods are from 1 to 78 weeks."
}]
Choose a period between 1 and 78 weeks
400 [{
"error": "BadRequestError",
"message": "Unsupported value for confirm_email_before_download: '(VALUE)'. Use a boolean true or false value."
}]
Use either true or false
400 [{
"error": "BadRequestError",
"message": "File did not pass the virus scan"
}]
The file contains a virus
400 [{
"error": "BadRequestError",
"message": "Send files by email has not been set up - add contact details for your service at https://www.notifications.service.gov.uk/services/(SERVICE ID)/service-settings/send-files-by-email"
}]
See how to add contact details to the file download page
400 [{
"error": "BadRequestError",
"message": "Can only send a file by email"
}]
Make sure you are using an email template
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"
}]
Refer to API rate limits for more information
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (LIMIT NUMBER) for today"
}]
Refer to service limits for the limit number
500 [{
"error": "Exception",
"message": "Internal server error"
}]
Notify was unable to process the request, resend your notification.
N/A File is larger than 2MB The file is too big. Files must be smaller than 2MB

Send a letter

When you add a new service it will start in trial mode. You can only send letters when your service is live.

To send Notify a request to go live:

  1. Sign in to GOV.UK Notify.
  2. Go to the Settings page.
  3. In the Your service is in trial mode section, select request to go live.

Method

notifyClient
  .sendLetter(templateId, {
    personalisation: personalisation,
    reference: reference
  })
  .then(response => console.log(response))
  .catch(err => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

templateId (required)

To find the template ID:

  1. Sign in to GOV.UK Notify.
  2. Go to the Templates page and select the relevant template.
  3. Select Copy template ID to clipboard.

For example:

"f33517ff-2a88-4f6e-b855-c550268ce08a"
personalisation (required)

The personalisation argument always contains the following parameters for the letter recipient’s address:

  • address_line_1
  • address_line_2
  • address_line_3
  • address_line_4
  • address_line_5
  • address_line_6
  • address_line_7

The address must have at least 3 lines.

The last line needs to be a real UK postcode or the name of a country outside the UK.

Notify checks for international addresses and will automatically charge you the correct postage.

The postcode personalisation argument has been replaced. If your template still uses postcode, Notify will treat it as the last line of the address.

Any other placeholder fields included in the letter template also count as required parameters. You must provide their values in an object. For example:

{
  personalisation: {
    'address_line_1': 'The Occupier',
    'address_line_2': '123 High Street',
    'address_line_3': 'SW14 6BH',
    'application_date': '2018-01-01'
  }
}
reference (optional)

A unique identifier you can create if required. This reference identifies a single unique notification or a batch of notifications. It must not contain any personal information such as name or postal address. For example:

"your_reference_here"

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': '740e5834-3a29-46b4-9a6f-16142fde533a',
  'reference': null,
  'content': {
    'subject': 'Licence renewal',
    'body': 'Dear Bill, your licence is due for renewal on 3 January 2016.'
  },
  'uri': 'https://api.notifications.service.gov.uk/v2/notifications/740e5834-3a29-46b4-9a6f-16142fde533a',
  'template': {
    'id': 'f33517ff-2a88-4f6e-b855-c550268ce08a',
    'version': 1,
    'uri': 'https://api.notifications.service.gov.uk/v2/template/f33517ff-2a88-4f6e-b855-c550268ce08a'
  },
  'scheduled_for': null
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "BadRequestError",
"message": "Cannot send letters with a team api key"
}]
Use the correct type of API key.
400 [{
"error": "BadRequestError",
"message": "Cannot send letters when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"
}]
Your service cannot send this notification in trial mode.
400 [{
"error": "ValidationError",
"message": "personalisation address_line_1 is a required property"
}]
Ensure that your template has a field for the first line of the address, refer to personalisation for more information.
400 [{
"error": "ValidationError",
"message": "Must be a real UK postcode"
}]
Ensure that the value for the last line of the address is a real UK postcode.
400 [{
"error": "ValidationError",
"message": "Last line of address must be a real UK postcode or another country"
}]
Ensure that the value for the last line of the address is a real UK postcode or the name of a country outside the UK.
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock.
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information.
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"
}]
Refer to API rate limits for more information.
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (LIMIT NUMBER) for today"
}]
Refer to service limits for the limit number.
500 [{
"error": "Exception",
"message": "Internal server error"
}]
Notify was unable to process the request, resend your notification.

Send a precompiled letter

Method

var response = notifyClient.sendPrecompiledLetter(
  reference,
  pdfFile,
  postage
)

Arguments

reference (required)

A unique identifier you create. This reference identifies a single unique notification or a batch of notifications. It must not contain any personal information such as name or postal address. For example:

"your_reference_here"
pdfFile

The precompiled letter must be a PDF file which meets the GOV.UK Notify letter specification. For example:

var fs = require('fs')

fs.readFile('path/to/document.pdf', function (err, pdfFile) {
  if (err) {
    console.error(err)
  }
  var notification = notifyClient.sendPrecompiledLetter(
    'your reference', pdfFile
  )
})
postage (optional)

You can choose first or second class postage for your precompiled letter. Set the value to first for first class, or second for second class. If you do not pass in this argument, the postage will default to second class.

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': '740e5834-3a29-46b4-9a6f-16142fde533a',
  'reference': 'your-letter-reference',
  'postage': 'second'
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "BadRequestError",
"message": "Cannot send letters with a team api key"
}]
Use the correct type of API key
400 [{
"error": "BadRequestError",
"message": "Cannot send letters when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"
}]
Your service cannot send this notification in trial mode
400 [{
"error": "ValidationError",
"message": "personalisation address_line_1 is a required property"
}]
Send a valid PDF file
400 [{
"error": "ValidationError",
"message": "postage invalid. It must be either first or second."
}]
Change the value of postage argument in the method call to either 'first' or 'second'
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"
}]
Refer to API rate limits for more information
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (LIMIT NUMBER) for today"
}]
Refer to service limits for the limit number
N/A "message":"precompiledPDF must be a valid PDF file" Send a valid PDF file
N/A "message":"reference cannot be null or empty" Populate the reference parameter
N/A "message":"precompiledPDF cannot be null or empty" Send a PDF file with data in it

Get message status

Get the status of one message

You can only get the status of messages sent within the retention period. The default retention period is 7 days.

Method

notifyClient
  .getNotificationById(notificationId)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

notificationId (required)

The ID of the notification. To find the notification ID, you can either:

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': 'notify_id',
  'body': 'Hello Foo',
  'subject': 'null|email_subject',
  'reference': 'client reference',
  'email_address': 'email address',
  'phone_number': 'phone number',
  'line_1': 'full name of a person or company',
  'line_2': '123 The Street',
  'line_3': 'Some Area',
  'line_4': 'Some Town',
  'line_5': 'Some county',
  'line_6': 'Something else',
  'line_7': 'UK postcode or country',
  'postage': 'first or second',
  'type': 'sms|letter|email',
  'status': 'current status',
  'template': {
    'version': 1,
    'id': 1,
    'uri': '/template/{id}/{version}'
  },
  'created_by_name': 'name of the person who sent the notification if sent manually',
  'created_at': 'created at',
  'sent_at': 'sent to provider at'
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "ValidationError",
"message": "id is not a valid UUID"
}]
Check the notification ID
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
404 [{
"error": "NoResultFound",
"message": "No result found"
}]
Check the notification ID

Get the status of multiple messages

This API call returns one page of up to 250 messages and statuses. You can get either the most recent messages, or get older messages by specifying a particular notification ID in the olderThan argument.

You can only get the status of messages that are 7 days old or newer.

Method

notifyClient
  .getNotifications(templateType, status, reference, olderThan)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

To get the most recent messages, you must pass in an empty olderThan argument or null.

To get older messages, pass the ID of an older notification into the olderThan argument. This returns the next oldest messages from the specified notification ID.

Arguments

You can pass in empty arguments or null to ignore these filters.

status (optional)

You can filter by each:

You can leave out this argument to ignore this filter.

notificationType (optional)

You can filter by:

  • email
  • sms
  • letter
reference (optional)

A unique identifier you create if necessary. This reference identifies a single unique notification or a batch of notifications. It must not contain any personal information such as name or postal address. For example:

"your_reference_here"
olderThan (optional)

Input the ID of a notification into this argument. If you use this argument, the client returns the next 250 received notifications older than the given ID. For example:

"8e222534-7f05-4972-86e3-17c5d9f894e2"

If you pass in an empty argument or null, the client returns the most recent 250 notifications.

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'notifications': [
    {
      'id': 'notify_id',
      'reference': 'client reference',
      'email_address': 'email address',
      'phone_number': 'phone number',
      'line_1': 'full name of a person or company',
      'line_2': '123 The Street',
      'line_3': 'Some Area',
      'line_4': 'Some Town',
      'line_5': 'Some county',
      'line_6': 'Something else',
      'postcode': 'postcode',
      'postage': 'first or second',
      'type': 'sms | letter | email',
      'status': 'sending | delivered | permanent-failure | temporary-failure | technical-failure',
      'template': {
        'version': 1,
        'id': 1,
        'uri': '/template/{id}/{version}'
      },
      'created_by_name': 'name of the person who sent the notification if sent manually',
      'created_at': 'created at',
      'sent_at': 'sent to provider at'
    }
    // …
  ],
  'links': {
    'current': '/notifications?template_type=sms&status=delivered',
    'next': '/notifications?other_than=last_id_in_list&template_type=sms&status=delivered'
  }
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "ValidationError",
"message": "bad status is not one of [created, sending, sent, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, accepted, received]"
}]
Contact the GOV.UK Notify team
400 [{
"error": "ValidationError",
"message": "Applet is not one of [sms, email, letter]"
}]
Contact the GOV.UK Notify team
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information

Email status descriptions

Status Description
#created GOV.UK Notify has placed the message in a queue, ready to be sent to the provider. It should only remain in this state for a few seconds.
#sending GOV.UK Notify has sent the message to the provider. The provider will try to deliver the message to the recipient for up to 72 hours. GOV.UK Notify is waiting for delivery information.
#delivered The message was successfully delivered.
#permanent-failure The provider could not deliver the message because the email address was wrong. You should remove these email addresses from your database.
#temporary-failure The provider could not deliver the message. This can happen when the recipient’s inbox is full or their anti-spam filter rejects your email. Check your content does not look like spam before you try to send the message again.
#technical-failure Your message was not sent because there was a problem between Notify and the provider.
You’ll have to try sending your messages again.

Text message status descriptions

Status Description
#created GOV.UK Notify has placed the message in a queue, ready to be sent to the provider. It should only remain in this state for a few seconds.
#sending GOV.UK Notify has sent the message to the provider. The provider will try to deliver the message to the recipient for up to 72 hours. GOV.UK Notify is waiting for delivery information.
#pending GOV.UK Notify is waiting for more delivery information.
GOV.UK Notify received a callback from the provider but the recipient’s device has not yet responded. Another callback from the provider determines the final status of the text message.
#sent The message was sent to an international number. The mobile networks in some countries do not provide any more delivery information. The GOV.UK Notify website displays this status as 'Sent to an international number'.
#delivered The message was successfully delivered.
#permanent-failure The provider could not deliver the message. This can happen if the phone number was wrong or if the network operator rejects the message. If you’re sure that these phone numbers are correct, you should contact GOV.UK Notify support. If not, you should remove them from your database. You’ll still be charged for text messages that cannot be delivered.
#temporary-failure The provider could not deliver the message. This can happen when the recipient’s phone is off, has no signal, or their text message inbox is full. You can try to send the message again. You’ll still be charged for text messages to phones that are not accepting messages.
#technical-failure Your message was not sent because there was a problem between Notify and the provider.
You’ll have to try sending your messages again. You will not be charged for text messages that are affected by a technical failure.

Letter status descriptions

Status Description
#accepted GOV.UK Notify has sent the letter to the provider to be printed.
#received The provider has printed and dispatched the letter.
#cancelled Sending cancelled. The letter will not be printed or dispatched.
#technical-failure GOV.UK Notify had an unexpected error while sending the letter to our printing provider.
#permanent-failure The provider cannot print the letter. Your letter will not be dispatched.

Precompiled letter status descriptions

Status Description
#accepted GOV.UK Notify has sent the letter to the provider to be printed.
#received The provider has printed and dispatched the letter.
#cancelled Sending cancelled. The letter will not be printed or dispatched.
#pending-virus-check GOV.UK Notify has not completed a virus scan of the precompiled letter file.
#virus-scan-failed GOV.UK Notify found a potential virus in the precompiled letter file.
#validation-failed Content in the precompiled letter file is outside the printable area. See the GOV.UK Notify letter specification for more information.
#technical-failure GOV.UK Notify had an unexpected error while sending the letter to our printing provider.
#permanent-failure The provider cannot print the letter. Your letter will not be dispatched.

Get a PDF for a letter notification

Method

notifyClient
  .getPdfForLetterNotification(notificationId)
  .then(function (fileBuffer) {
    return fileBuffer
  }) /* the response is a file buffer, an instance of Buffer */
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with fileBuffer if successful
  • fail with an err if unsuccessful

Arguments

notificationId (required)

The ID of the notification. To find the notification ID, you can either:

Response

If the request to the client is successful, the client will return an instance of the Buffer class containing the raw PDF data.

Error codes

If the request is not successful, the client will return an HTTPError containing the relevant error code:

error.status_code error.message How to fix
400 [{
"error": "ValidationError",
"message": "id is not a valid UUID"
}]
Check the notification ID
400 [{
"error": "PDFNotReadyError",
"message": "PDF not available yet, try again later"
}]
Wait for the notification to finish processing. This usually takes a few seconds
400 [{
"error": "BadRequestError",
"message": "File did not pass the virus scan"
}]
You cannot retrieve the contents of a letter notification that contains a virus
400 [{
"error": "BadRequestError",
"message": "PDF not available for letters in technical-failure"
}]
You cannot retrieve the contents of a letter notification in technical-failure
400 [{
"error": "ValidationError",
"message": "Notification is not a letter"
}]
Check that you are looking up the correct notification
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
404 [{
"error": "NoResultFound",
"message": "No result found"
}]
Check the notification ID

Get a template

Get a template by ID

Method

This returns the latest version of the template.

notifyClient
  .getTemplateById(templateId)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

templateId (required)

The ID of the template. Sign in to GOV.UK Notify and go to the Templates page to find it. For example:

"f33517ff-2a88-4f6e-b855-c550268ce08a"

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': 'template_id',
  'name': 'template name',
  'type': 'sms|email|letter',
  'created_at': 'created at',
  'updated_at': 'updated at',
  'version': 'version',
  'created_by': 'someone@example.com',
  'body': 'body',
  'subject': 'null|email_subject',
  'letter_contact_block': 'null|letter_contact_block'
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
404 [{
"error": "NoResultFound",
"message": "No Result Found"
}]
Check your template ID

Get a template by ID and version

Method

notifyClient
  .getTemplateByIdAndVersion(templateId, version)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

templateId (required)

The ID of the template. Sign in to GOV.UK Notify and go to the Templates page to find it. For example:

"f33517ff-2a88-4f6e-b855-c550268ce08a"
version (required)

The version number of the template.

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': 'template_id',
  'name': 'template name',
  'type': 'sms|email|letter',
  'created_at': 'created at',
  'updated_at': 'updated at',
  'version': 'version',
  'created_by': 'someone@example.com',
  'body': 'body',
  'subject': 'null|email_subject',
  'letter_contact_block': 'null|letter_contact_block'
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "ValidationError",
"message": "id is not a valid UUID"
}]
Check the notification ID
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information
404 [{
"error": "NoResultFound",
"message": "No Result Found"
}]
Check your template ID and version

Get all templates

Method

This returns the latest version of all templates.

notifyClient
  .getAllTemplates(templateType)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

Arguments

templateType (optional)

If you do not use templateType, the client returns all templates. Otherwise you can filter by:

  • email
  • sms
  • letter

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'templates': [
    {
      'id': 'template_id',
      'name': 'template name',
      'type': 'sms|email|letter',
      'created_at': 'created at',
      'updated_at': 'updated at',
      'version': 'version',
      'created_by': 'someone@example.com',
      'body': 'body',
      'subject': 'null|email_subject',
      'letter_contact_block': 'null|letter_contact_block'
    },
    {
      // … another template
    }
  ]
}

If no templates exist for a template type or there no templates for a service, the object is empty.

Generate a preview template

Method

This generates a preview version of a template.

personalisation = { 'foo': 'bar' }
notifyClient
  .previewTemplateById(templateId, personalisation)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

The parameters in the personalisation argument must match the placeholder fields in the actual template. The API notification client ignores any extra fields in the method.

Arguments

templateId (required)

The ID of the template. Sign in to GOV.UK Notify and go to the Templates page to find it. For example:

"f33517ff-2a88-4f6e-b855-c550268ce08a"
personalisation (optional)

If a template has placeholder fields for personalised information such as name or application date, you must provide their values in an object. For example:

{
  personalisation: {
    'first_name': 'Amala',
    'reference_number': '300241'
  }
}

You can leave out this argument if a template does not have any placeholder fields for personalised information.

Response

If the request is successful, the promise resolves with a response object. For example, the response.data property will look like this:

{
  'id': 'notify_id',
  'type': 'sms|email|letter',
  'version': 'version',
  'body': 'Hello bar', // with substitution values
  'subject': 'null|email_subject',
  'html': '<p>Example</p>' // Returns the rendered body (email templates only)
}

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
400 [{
"error": "BadRequestError",
"message": "Missing personalisation: [PERSONALISATION FIELD]"
}]
Check that the personalisation arguments in the method match the placeholder fields in the template
400 [{
"error": "NoResultFound",
"message": "No result found"
}]
Check the template ID
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information

Get received text messages

This API call returns one page of up to 250 received text messages. You can get either the most recent messages, or get older messages by specifying a particular notification ID in the olderThan argument.

You can only get messages that are 7 days old or newer.

You can also set up callbacks for received text messages.

Enable received text messages

Contact the GOV.UK Notify team using the support page or chat to us on Slack to request a unique number for text message replies.

Get a page of received text messages

Method

notifyClient
  .getReceivedTexts(olderThan)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))

The method returns a promise [external link]. The promise will either:

  • resolve with a response if successful
  • fail with an err if unsuccessful

To get the most recent messages, you must pass in an empty argument or null.

To get older messages, pass the ID of an older notification into the olderThan argument. This returns the next oldest messages from the specified notification ID.

Arguments

olderThan (optional)

Input the ID of a received text message into this argument. If you use this argument, the client returns the next 250 received text messages older than the given ID. For example:

8e222534-7f05-4972-86e3-17c5d9f894e2"

If you pass in an empty argument or null, the client returns the most recent 250 text messages.

Response

If the request to the client is successful, the promise resolves with an object containing all received texts.

{
  'received_text_messages':
        [
          {
            'id': 'notify_id', // required
            'user_number': 'user number', // required user number
            'notify_number': 'notify number', // receiving number
            'created_at': 'created at', // required
            'service_id': 'service id', // required service id
            'content': 'text content' // required text content
          }
          // …
        ],
  'links': {
    'current': '/received-test-messages',
    'next': '/received-text-messages?older_than=last_id_in_list'
  }
}

If the notification specified in the olderThan argument is older than 7 days, the promise resolves an empty response.

Error codes

If the request is not successful, the promise fails with an err.

err.response.data.status_code err.response.data.errors How to fix
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: API key not found"
}]
Use the correct API key. Refer to API keys for more information