# Node JS client documentation

## Set up the client

### Install the client

Run the following in the command line:

```shellscript
npm install --save notifications-node-client
```

### Create a new instance of the client

Add this code to your application:

```javascript
let NotifyClient = require("notifications-node-client").NotifyClient;

let notifyClient = new NotifyClient("https://api.notifynl.nl/", apiKey);
```

**Arguments**

**api\_key (required)**

To get an API key, [sign in to NotifyNL](https://admin.notifynl.nl/sign-in) and go to the **API integration** page. You can find more information in the [API keys](/misc/api-keys.md) section of this documentation.

**Connect through a proxy (optional)**

Add this code to your application:

```javascript
const proxyConfig = {
  host: proxyHost,
  port: proxyPort
};

notifyClient.setProxy(proxyConfig);
```

where the `proxyConfig` should be an object supported by [axios](https://github.com/axios/axios).

**Provide your own underlying Axios client (optional)**

You can provide your own Axios client to the Notify client. This is useful if you want to use a custom Axios client with specific settings, like custom interceptors.

Add this code to your application:

```javascript
notifyClient.setClient(customAxiosClient);
```

where `customAxiosClient` is an instance of Axios.

## Send a message

You can use NotifyNL to send emails, text messages and letters.

### Bulk sending

You cannot use a single API call to send messages in bulk.

To send a batch of messages, use the API to loop over your recipient list, sending one message at a time to each recipient.

Make sure you do not exceed our [rate limits](/misc/limits.md).

### Send a text message

**Method**

```javascript
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). The promise will either:

* resolve with a `response` if successful
* fail with an `err` if unsuccessful

**Arguments**

**phoneNumber (required)**

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

For example:

```javascript
let phoneNumber = "+447900900123";
```

**templateId (required)**

To find the template ID:

1. [Sign in to NotifyNL](https://admin.notifynl.nl/sign-in).
2. Go to the **Templates** page and select the relevant template.
3. Select **Copy template ID to clipboard**.

For example:

```javascript
let templateId = "f33517ff-2a88-4f6e-b855-c550268ce08a"; // required UUID string
```

**personalisation (optional)**

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:

```javascript
let personalisation = {
    first_name: "Amala",
    appointment_date: "1 January 2018 at 1:00pm"
};
```

**reference (optional)**

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

```javascript
let reference = "your reference"; // optional string - identifies notification(s)
```

**smsSenderId (optional)**

A unique identifier of the sender of the text message.

To find the text message sender:

1. [Sign in to NotifyNL](https://admin.notifynl.nl/sign-in).
2. Go to the **Settings** page.
3. In the **Text Messages** section, select **Manage** on the **Text Message sender** row.

You can then 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**

```javascript
let smsSenderId = "8e222534-7f05-4972-86e3-17c5d9f894e2";
```

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:

```json
{
    "id": "740e5834-3a29-46b4-9a6f-16142fde533a", // required string - notification ID
    "reference": "your reference", // optional string - reference you provided when sending the message
    "content": {
        "body": "Hi Amala, your appointment is on 1 January 2018 at 1:00pm", // required string - message content
        "from_number": "GOVUK" // required string - sender ID / phone number
    },
    "uri": "https://api.notifications.service.gov.uk/v2/notifications/740e5834-3a29-46b4-9a6f-16142fde533a", // required string
    "template": {
        "id": "f33517ff-2a88-4f6e-b855-c550268ce08a", // required string - template ID
        "version": 3, // required integer
        "uri": "https://api.notifications.service.gov.uk/v2/template/f33517ff-2a88-4f6e-b855-c550268ce08a" // required string
    }
};
```

If you are using the [test API key](/misc/api-keys.md#test), all your messages will come back with a `delivered` status.

All messages sent using the [team and guest list](/misc/api-keys.md#team-and-guest-list) or [live](/misc/api-keys.md#live) keys will appear on your dashboard.

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

<table><thead><tr><th width="440">Error message</th><th>How to fix</th></tr></thead><tbody><tr><td><strong>ValidationError (status code 400)</strong></td><td></td></tr><tr><td><code>phone_number Too many digits</code></td><td>Provide a valid recipient phone number.</td></tr><tr><td><code>phone_number Not enough digits</code></td><td>Provide a valid recipient phone number.</td></tr><tr><td><code>phone_number Not a NL mobile number</code></td><td>Provide a valid Dutch recipient phone number.</td></tr><tr><td><code>phone_number Must not contain letters or symbols</code></td><td>Provide a valid recipient phone number.</td></tr><tr><td><code>phone_number Not a valid country prefix</code></td><td>Provide a valid recipient phone number.</td></tr><tr><td><strong>BadRequestError (status code 400)</strong></td><td></td></tr><tr><td><code>sms_sender_id &#x3C;sms_sender_id> does not exist in database for service id</code></td><td>Go to your service Settings and copy a valid sms_sender_id. Check that the API key you are using and the sms_sender_id belong to the same service.</td></tr><tr><td><code>Cannot send to this recipient when service is in trial mode - see https://admin.notifynl.nl/trial-mode</code></td><td>Your service cannot send this text message in <a href="/pages/0i4hFdFw5kQZdzjm8eBk">trial mode</a>. To fix, you need to request for your service to go live.</td></tr><tr><td><code>Cannot send to international mobile numbers</code></td><td>Sending to international mobile numbers is turned off for your service. You can change this in your service Settings.</td></tr></tbody></table>

### Send an email

**Method**

```javascript
let options = {
    personalisation: {
        first_name: "Amala",
        appointment_date: "1 January 2018 at 1:00pm",
        required_documents: ["passport", "utility bill", "other id"]
    },
    reference: "your reference",
    oneClickUnsubscribeURL: "https://example.com/unsubscribe.html?opaque=123456789",
    emailReplyToId: "ca4fdde7-2a67-4a6c-8393-62aa7245751f"
};

notifyClient
  .sendEmail(templateId, emailAddress, options) // Pass options as the third argument (optional)
  .then(response => console.log(response))
  .catch(err => console.error(err));
```

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). The promise will either:

* resolve with a `response` if successful
* fail with an `err` if unsuccessful

**Arguments**

**emailAddress (required)**

The email address of the recipient. For example:

```javascript
let emailAddress = "amala@example.com";
```

**templateId (required)**

To find the template ID:

1. [Sign in to NotifyNL](https://admin.notifynl.nl/sign-in).
2. Go to the **Templates** page and select the relevant template.
3. Select **Copy template ID to clipboard**.

For example:

```javascript
let templateId = "9d751e0e-f929-4891-82a1-a3e1c3c18ee3";
```

**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:

```javascript
let options = {
    personalisation: {
        first_name: "Amala",
        appointment_date: "1 January 2018 at 1:00pm",
        required_documents: ["passport", "utility bill", "other id"]
    },
};
```

Note: if you pass a list like `required_documents` in the example above, it will appear as bullet points in your message.

Find out how to [reduce the risk of malicious content injection in placeholders](#reducing-the-risk-of-malicious-content-injection-in-placeholders).

**reference (optional)**

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

```javascript
let options = {
    reference: "your-reference"
};
```

**oneClickUnsubscribeURL (recommended)**

If you send subscription emails you must let recipients opt out of receiving them. Read our Using Notify page for more information about [unsubscribe links](/using-notifynl/using-notifynl/unsubscribe-links.md).

The one-click unsubscribe URL will be added to the headers of your email. Email clients will use it to add an unsubscribe button.

```javascript
let options = {
    oneClickUnsubscribeURL: "https://example.com/unsubscribe.html?opaque=123456789"
};
```

The one-click unsubscribe URL must respond to an empty `POST` request by unsubscribing the user from your emails. You can include query parameters to help you identify the user.

Your unsubscribe URL and response must comply with the guidance specified in [Section 3.1 of IETF RFC 8058](https://www.rfcreader.com/#rfc8058_line139).

You can leave out this argument if the email being sent is not a subscription email.

You should also add an unsubscribe link to the bottom of your email. Find out how to add an unsubscribe link when you create a **New template** or **Edit** an email template.

You must also add an unsubscribe link to the bottom of your email. The unsubscribe link at the bottom of your email should take the email recipient to a webpage where they can confirm that they want to unsubscribe.

Find out how to add a link when you create a **New template** or **Edit** an email template.

**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 NotifyNL](https://admin.notifynl.nl/sign-in).
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**.

```javascript
let options = {
    emailReplyToId:"ca4fdde7-2a67-4a6c-8393-62aa7245751f"
};
```

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:

```json
{
    "id": "201b576e-c09b-467b-9dfa-9c3b689ee730", // required string - notification ID
    "reference": "your reference", // optional string - reference you provided when sending the message
    "content": {
        "subject": "Your upcoming pigeon registration appointment",  // required string - message subject
        "body": "Dear Amala\r\n\r\nYour pigeon registration appointment is scheduled for 1 January 2018 at 1:00pm.\r\n\r\nPlease bring:\r\n\n\n* passport\n* utility bill\n* other id\r\n\r\nYours,\r\nPigeon Affairs Bureau",  // required string - message content
        "from_email": "pigeon.affairs.bureau@notifications.service.gov.uk", // required string - "FROM" email address, not a real inbox
        "one_click_unsubscribe_url": "https://example.com/unsubscribe.html?opaque=123456789" // optional string
    },
    "uri": "https://api.notifications.service.gov.uk/v2/notifications/201b576e-c09b-467b-9dfa-9c3b689ee730", // required string
    "template": {
        "id": "9d751e0e-f929-4891-82a1-a3e1c3c18ee3", // required string - template ID
        "version": 1, // required integer
        "uri": "https://api.notifications.service.gov.uk/v2/template/9d751e0e-f929-4891-82a1-a3e1c3c18ee3" // required string
    }
};
```

#### **Reducing the risk of malicious content injection in placeholders**

Notify lets you [personalise messages](/using-notifynl/using-notifynl/personalisation.md) using placeholders.

You can [format](/using-notifynl/using-notifynl/formatting-emails-and-letters.md) content or add links and urls into placeholders using Markdown.

If you pass in information from untrusted sources (such as online forms) into your Notify template using personalisation, this may be used to add malicious content and links to notifications you send via Notify.

The malicious content could be:

* Markdown syntax intended to be rendered into HTML
* a plain text URL which would be rendered into a clickable phishing link

An example of how malicious content can be injected into Notify personalisation:

**Template in Notify**:

```markdown
Hello ((name))
```

**Personalisation**:

```json
{name: "Anne Example, now [click this evil link](https://malicious.link)"}
```

**Email will appear as**:

```
 Dear Anne Example, now click this evil link
```

We recommend you sanitise all input from untrusted sources to prevent the injection of malicious content. You can use a backslash to escape [individual characters](https://www.markdownguide.org/basic-syntax/#characters-you-can-escape). The characters of most concern are those that could be used to add a URL link such as `[`, `]`, `(` or `)`.

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                                                                            | How to fix                                                                                                                                                            |
| -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **ValidationError (status code 400)**                                                                    |                                                                                                                                                                       |
| `email_address Not a valid email address`                                                                | Provide a valid recipient email address.                                                                                                                              |
| `one_click_unsubscribe_url is not a valid https url`                                                     | Provide a valid https url for your unsubscribe link.                                                                                                                  |
| **BadRequestError (status code 400)**                                                                    |                                                                                                                                                                       |
| `email_reply_to_id <reply to id> does not exist in database for service id <service id>`                 | Go to your service Settings and copy a valid `email_reply_to_id`. Double check that the API key you are using and the `email_reply_to_id` belong to the same service. |
| `Emails cannot be longer than 2000000 bytes. Your message is <rendered template size in bytes> bytes.`   | Shorten your email message.                                                                                                                                           |
| `Cannot send to this recipient when service is in trial mode - see https://admin.notifynl.nl/trial-mode` | Your service cannot send this email in [trial mode](/using-notifynl/using-notifynl/trial-mode.md). To fix, you need to request for your service to go live.           |

### 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. NotifyNL 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 NotifyNL](https://admin.notifynl.nl/sign-in).
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 NotifyNL](https://admin.notifynl.nl/sign-in).
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 the following file types:

* CSV (.csv)
* image (.jpeg, .jpg, .png)
* Microsoft Excel Spreadsheet (.xlsx)
* Microsoft Word Document (.doc, .docx)
* PDF (.pdf)
* text (.json, .odt, .rtf, .txt)

Your file must be smaller than 2MB. [Contact the NotifyNL team](https://admin.notifynl.nl/support) if you need to send other file types.

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

```javascript
let fs = require("fs")

fs.readFile("path/to/document.pdf", function (err, pdfFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: "Amala",
      appointment_date: "1 January 2018 at 1:00pm",
      link_to_file: notifyClient.prepareUpload(pdfFile)
    }
  }).then(response => console.log(response)).catch(err => console.error(err))
})
```

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). The promise will either:

* resolve with a `response` if successful
* fail with an `err` if unsuccessful

**Set the filename**

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

You should provide a filename when you upload your file.

The filename should tell the recipient what the file contains. A memorable filename can help the recipient to find the file again later.

The filename must end with a file extension. For example, `.csv` for a CSV file. If you include the wrong file extension, recipients may not be able to open your file.

If you do not provide a filename for your file, Notify will:

* generate a random filename
* try to add the correct file extension

If Notify cannot add the correct file extension, recipients may not be able to open your file.

```javascript
let fs = require("fs")
fs.readFile("path/to/document.csv", function (err, csvFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: "Amala",
      appointment_date: "1 January 2018 at 1:00pm",
      link_to_file: notifyClient.prepareUpload(csvFile, { filename: "amala_pigeon_affairs_bureau_invite.csv" })
    }
  }).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](https://www.gov.uk/government/publications/government-security-classifications) policy

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

```javascript
let fs = require("fs")

fs.readFile("path/to/document.pdf", function (err, pdfFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: "Amala",
      appointment_date: "1 January 2018 at 1:00pm",
      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.

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

You can choose any value between 1 week and 78 weeks. When deciding this, you should consider:

* the need to protect the recipient’s personal information
* whether the recipient will need to download the file again later

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).

```javascript
let fs = require("fs")

fs.readFile("path/to/document.pdf", function (err, pdfFile) {
  console.log(err)
  notifyClient.sendEmail(templateId, emailAddress, {
    personalisation: {
      first_name: "Amala",
      appointment_date: "1 January 2018 at 1:00pm",
      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:

```json
{
  "id": "201b576e-c09b-467b-9dfa-9c3b689ee730",  // required string - notification ID
  "reference": "your reference",  // optional string - reference you provided when sending the message
  "content": {
    "subject": "Your upcoming pigeon registration appointment",  // required string - message subject
    "body": "Dear Amala\r\n\r\nYour pigeon registration appointment is scheduled for 1 January 2018 at 1:00pm.\r\n\r\n Here is a link to your invitation document:\r\nhttps://documents.service.gov.uk/d/YlxDzgNUQYi1Qg6QxIpptA/th46VnrvRxyVO9div6f7hA?key=R0VDmwJ1YzNYFJysAIjQd9yHn5qKUFg-nXHVe3Ioa3A\r\n\r\nPlease bring the invite with you to the appointment.\r\n\r\nYours,\r\nPigeon Affairs Bureau",  // required string - message content - see that the link to document is embedded in the message content
    "from_email": "pigeon.affairs.bureau@notifications.service.gov.uk",  // required string - "FROM" email address, not a real inbox
    "one_click_unsubscribe": "https://example.com/unsubscribe.html?opaque=123456789",  // optional string
  },
  "uri": "https://api.notifications.service.gov.uk/v2/notifications/201b576e-c09b-467b-9dfa-9c3b689ee730",  // required string
  "template": {
    "id": "9d751e0e-f929-4891-82a1-a3e1c3c18ee3",  // required string - template ID
    "version": 1,  // required integer
    "uri": "https://api.notifications.service.gov.uk/v2/template/9d751e0e-f929-4891-82a1-a3e1c3c18ee3"  // required string
  }
}
```

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                                                                                                                                            | How to fix                                                                                                                                                    |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **BadRequestError (status code 400)**                                                                                                                                    |                                                                                                                                                               |
| `Unsupported file type '(FILE TYPE)'. Supported types are: '(ALLOWED TYPES)'`                                                                                            | Wrong file type. You can only upload .csv, .doc, .docx, .jpeg, .jpg, .odt, .pdf, .png, .rtf, .txt or .xlsx files                                              |
| `filename cannot be longer than 100 characters`                                                                                                                          | Choose a shorter filename                                                                                                                                     |
| `filename must end with a file extension. For example, filename.csv`                                                                                                     | Include the file extension in your filename                                                                                                                   |
| `Unsupported value for retention_period '(PERIOD)'. Supported periods are from 1 to 78 weeks.`                                                                           | Choose a period between 1 and 78 weeks                                                                                                                        |
| `Unsupported value for confirm_email_before_download: '(VALUE)'. Use a boolean true or false value.`                                                                     | Use either True or False                                                                                                                                      |
| `File did not pass the virus scan`                                                                                                                                       | The file contains a virus                                                                                                                                     |
| `Send files by email has not been set up - add contact details for your service at https://admin.notifynl.nl/services/(SERVICE ID)/service-settings/send-files-by-email` | See how to [add contact details to the file download page](https://docs.notifications.service.gov.uk/java.html#add-contact-details-to-the-file-download-page) |
| `Can only send a file by email`                                                                                                                                          | Make sure you are using an email template                                                                                                                     |
| **Client error (no status code)**                                                                                                                                        |                                                                                                                                                               |
| `ValueError('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](/using-notifynl/using-notifynl/trial-mode.md). You can only send letters when your service is live.

To send Notify a request to go live:

1. [Sign in to NotifyNL](https://admin.notifynl.nl/sign-in).
2. Go to the **Settings** page.
3. In the **Your service is in trial mode** section, select **request to go live**.

**Method**

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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 NotifyNL](https://admin.notifynl.nl/sign-in).
2. Go to the **Templates** page and select the relevant template.
3. Select **Copy template ID to clipboard**.

For example:

```javascript
let templateId = "64415853-cb86-4cc4-b597-2aaa94ef8c39";
```

**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:

```json
{
  personalisation: {
    "address_line_1": "Amala Bird", // required string
    "address_line_2": "123 High Street", // required string
    "address_line_3": "Richmond upon Thames", // required string
    "address_line_4": "Middlesex",
    "address_line_5": "SW14 6BF",  // last line of address you include must be a postcode or a country name  outside the UK
    "name": "Amala",
    "appointment_date": "1 January 2018 at 1:00pm",
    // pass in an array and it will appear as bullet points in the letter:
    "required_documents": ["passport", "utility bill", "other id"]
  }
}
```

**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:

```javascript
let reference = "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:

```json
{
    "id": "3d1ce039-5476-414c-99b2-fac1e6add62c",  // required string - notification ID
    "reference": "your reference",  // optional string - reference you provided when sending the message
    "content": {
        "subject": "Your upcoming pigeon registration appointment",  // required string - letter heading
        "body": "Dear Amala\r\n\r\nYour pigeon registration appointment is scheduled for 1 January 2018 at 1:00pm.\r\n\r\nPlease bring:\r\n\n\n* passport\n* utility bill\n* other id\r\n\r\nYours,\r\nPigeon Affairs Bureau",  // required string - letter content
    },
    "uri": "https://api.notifications.service.gov.uk/v2/notifications/3d1ce039-5476-414c-99b2-fac1e6add62c",  // required string
    "template": {
        "id": "64415853-cb86-4cc4-b597-2aaa94ef8c39",  // required string - template ID
        "version": 3,  // required integer
        "uri": "https://api.notifications.service.gov.uk/v2/template/64415853-cb86-4cc4-b597-2aaa94ef8c39"  // required string
    }
}
```

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                                                                  | How to fix                                                                                                                                                                |
| ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **ValidationError (status code 400)**                                                          |                                                                                                                                                                           |
| `personalisation address_line_1 is a required property`                                        | Ensure that your template has a field for the first line of the address, check [personalisation](/using-notifynl/using-notifynl/personalisation.md) for more information. |
| `Must be a real NL postcode`                                                                   | Ensure that the value for the last line of the address is a real NL postcode.                                                                                             |
| `Must be a real address`                                                                       | Provide a real recipient address. We do not accept letters for “no fixed abode” addresses, as those cannot be delivered.                                                  |
| `Last line of address must be a real NL postcode or another country`                           | Ensure that the value for the last line of the address is a real NL postcode or the name of a country outside the NL.                                                     |
| `The last line of a BFPO address must not be a country.`                                       | The last line of a BFPO address must not be a country.                                                                                                                    |
| `Address must be at least 3 lines`                                                             | Provide at least 3 lines of address.                                                                                                                                      |
| `Address must be no more than 7 lines`                                                         | Provide no more than 7 lines of address.                                                                                                                                  |
| `Address lines must not start with any of the following characters: @ ( ) = [ ] ” \ / , < >`   | Change the start of an address line, so it doesn’t start with one of these characters. This is a requirement from our printing provider.                                  |
| `postage invalid. It must be either first, second or economy.`                                 | Specify valid postage option.                                                                                                                                             |
| **BadRequestError (status code 400)**                                                          |                                                                                                                                                                           |
| `Cannot send letters when service is in trial mode - see https://admin.notifynl.nl/trial-mode` | Your service cannot send this letter in [trial mode](/using-notifynl/using-notifynl/trial-mode.md).                                                                       |
| `Service is not allowed to send letters`                                                       | Turn on sending letters in your service Settings on NotifyNL webpage                                                                                                      |
| `letter_contact_id <letter_contact_id> does not exist in database for service id <service id>` | Go to your service Settings and copy a valid `letter_contact_id`. Check that the API key you are using and the `letter_contact_id` belong to the same service.            |
| **BadRequestError (status code 403)**                                                          |                                                                                                                                                                           |
| `Cannot send letters with a team api key`                                                      | Use the correct type of [API key](/misc/api-keys.md).                                                                                                                     |

### Send a precompiled letter

**Method**

```javascript
let 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:

```javascript
let reference = "your reference";
```

**pdfFile (required)**

The precompiled letter must be a PDF file which meets [the NotifyNL letter specification](/using-notifynl/using-notifynl/upload-a-letter.md). For example:

```javascript
let fs = require("fs")

fs.readFile("path/to/document.pdf", function (err, pdfFile) {
  if (err) {
    console.error(err)
  }
  let notification = notifyClient.sendPrecompiledLetter(
    "your reference", pdfFile
  )
})
```

**postage (optional)**

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

```javascript
let postage = "first";
```

**Response**

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

```json
{
    "id": "1d986ba7-fba6-49fb-84e5-75038a1dd968",  // required string - notification ID
    "reference": "your reference",  // required string - reference your provided
    "postage": "first"  // required string - postage you provided, or else default postage for the letter
}
```

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                                                                  | How to fix                                                                                                      |
| ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| **ValidationError (status code 400)**                                                          |                                                                                                                 |
| `reference is a required property`                                                             | Add a `reference` argument to the method call                                                                   |
| `postage invalid. It must be either first, second or economy.`                                 | Change the value of `postage` argument in the method call to either `"first"`, `"second"` or `"economy"`        |
| **BadRequestError (status code 400)**                                                          |                                                                                                                 |
| `Letter content is not a valid PDF`                                                            | PDF file format is required.                                                                                    |
| `Cannot send letters when service is in trial mode - see https://admin.notifynl.nl/trial-mode` | Your service cannot send this precompiled letter in [trial mode](/using-notifynl/using-notifynl/trial-mode.md). |
| **BadRequestError (status code 403)**                                                          |                                                                                                                 |
| `Cannot send letters with a team api key`                                                      | Use the correct type of [API key](/misc/api-keys.md).                                                           |

## Get message data

### Get the data for one message

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

**Method**

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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:

* check the response to the [original notification method call](https://docs.notifications.service.gov.uk/node.html#response)
* [sign in to NotifyNL](https://admin.notifynl.nl/sign-in) and go to the **API integration** page

**Response**

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

```json
{
    "id": "740e5834-3a29-46b4-9a6f-16142fde533a",  // required string - notification ID
    "reference": "your reference",  // optional string - reference you provided when sending the message
    "email_address": "amala@example.com",  // required string for emails
    "phone_number": "+447700900123",  // required string for text messages
    "line_1": "Amala Bird",  // required string for letter
    "line_2": "123 High Street",  // required string for letter
    "line_3": "Richmond upon Thames",  // required string for letter
    "line_4": "Middlesex",  // optional string for letter
    "line_5": "SW14 6BF",  // optional string for letter
    "line_6": null,  // optional string for letter
    "line_7": null, // optional string for letter
    "postage": "first / second / economy / europe / rest-of-world", // required string for letter
    "type": "sms / letter / email",  // required string
    "status": "sending / delivered / permanent-failure / temporary-failure / technical-failure",  // required string
    "template": {
        "version": 1, // required integer
        "id": "f33517ff-2a88-4f6e-b855-c550268ce08a",  // required string - template ID
        "uri": "/v2/template/{id}/{version}"  // required string
    },
    "body": "Hi Amala, your appointment is on 1 January 2018 at 1:00pm",  // required string - body of notification
    "subject": "Your upcoming pigeon registration appointment",  // required string for email - subject of email
    "created_at": "2024-05-17 15:58:38.342838",  // required string - date and time notification created
    "created_by_name": "Charlie Smith",  // optional string - name of the person who sent the notification if sent manually
    "sent_at": "2024-05-17 15:58:30.143000",  // optional string - date and time notification sent to provider
    "completed_at": "2024-05-17 15:59:10.321000",  // optional string - date and time notification delivered or failed
    "one_click_unsubscribe": "https://example.com/unsubscribe.html?opaque=123456789", // optional string, email only - URL that you provided so your recipients can unsubscribe
    "is_cost_data_ready": True,  // required boolean, this field is true if cost data is ready, and false if it isn't
    "cost_in_pounds": 0.0027,  // optional number - cost of the notification in pounds. The cost does not take free allowance into account
    "cost_details": {
    // for text messages:
        "billable_sms_fragments": 1,  // optional integer - number of billable sms fragments in your text message
        "international_rate_multiplier": 1,  // optional integer - for international sms rate is multiplied by this value
        "sms_rate": 0.0027,  // optional number - cost of 1 sms fragment
    // for letters:
        "billable_sheets_of_paper": 2,  // optional integer - number of sheets of paper in the letter you sent, that you will be charged for
        "postage": "first / second / economy / europe / rest-of-world"  // optional string
    }
}
```

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                         | How to fix                                                                                                                     |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| **ValidationError (status code 400)** |                                                                                                                                |
| `id is not a valid UUID`              | Check the notification ID.                                                                                                     |
| **NoResultFound (status code 404)**   |                                                                                                                                |
| `No result found`                     | If it’s outside the retention period, you may no longer get the status of the message. The default retention period is 7 days. |

### Get the data for multiple messages

This API call returns one page with data for up to 250 messages. You can get either the most recent messages, or get older messages by specifying a particular notification ID in the [`olderThan`](https://docs.notifications.service.gov.uk/node.html#olderthan-optional) argument.

You can only get messages that are within your data retention period. The default data retention period is 7 days. It can be changed in your Service Settings.

**Method**

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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:

* [email status](#email-status-descriptions)
* [text message status](#text-message-status-descriptions)
* [letter status](#letter-status-descriptions)
* [precompiled letter status](#precompiled-letter-status-descriptions)

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:

```javascript
let reference  = "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:

```javascript
let olderThan = "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:

```json
{
  "notifications": [
    {
      "id": "740e5834-3a29-46b4-9a6f-16142fde533a",  // required string - notification ID
      "reference": "STRING",  // optional string - client reference
      "email_address": "sender@something.com",  // required string for emails
      "phone_number": "+447900900123",  // required string for text messages
      "line_1": "ADDRESS LINE 1",  // required string for letter
      "line_2": "ADDRESS LINE 2",  // required string for letter
      "line_3": "ADDRESS LINE 3",  // required string for letter
      "line_4": "ADDRESS LINE 4",  // optional string for letter
      "line_5": "ADDRESS LINE 5",  // optional string for letter
      "line_6": "ADDRESS LINE 6",  // optional string for letter
      "postcode": "valid UK postcode", // optional string
      "postage": "first / second / economy / europe / rest-of-world", // required string for letter
      "type": "sms / letter / email",  // required string
      "status": "sending / delivered / permanent-failure / temporary-failure / technical-failure",  // required string
      "template": {
        "version": 1, // required integer
        "id": "f33517ff-2a88-4f6e-b855-c550268ce08a",  // required string - template ID
        "uri": "/v2/template/{id}/{version}"  // required string
      },
      "body": "STRING",  // required string - body of notification
      "subject": "STRING",  // required string for email - subject of email
      "created_at": "2024-05-17 15:58:38.342838",  // required string - date and time notification created
      "created_by_name": "STRING",  // optional string - name of the person who sent the notification if sent manually
      "sent_at": "2024-05-17 15:58:30.143000",  // optional string - date and time notification sent to provider
      "completed_at": "2024-05-17 15:59:10.321000",  // optional string - date and time notification delivered or failed
      "one_click_unsubscribe": "STRING", // optional string, email only - URL that you provided so your recipients can unsubscribe
      "is_cost_data_ready": true,  // required boolean, this field is true if cost data is ready, and false if it isn't
      "cost_in_pounds": 0.0027,  // optional number - cost of the notification in pounds. The cost does not take free allowance into account
      "cost_details": {
        // for text messages:
        "billable_sms_fragments": 1,  // optional integer - number of billable sms fragments in your text message
        "international_rate_multiplier": 1,  // optional integer - for international sms rate is multiplied by this value
        "sms_rate": 0.0027,  // optional number - cost of 1 sms fragment
        // for letters:
        "billable_sheets_of_paper": 2,  // optional integer - number of sheets of paper in the letter you sent, that you will be charged for
        "postage": "first / second / economy / europe / rest-of-world"  // optional string
      }
    }
  ],
  "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`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                                                                                                                                                                                                                              | How to fix                                                                                                        |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| **ValidationError (status code 400)**                                                                                                                                                                                                                      |                                                                                                                   |
| `status ‘elephant’ is not one of [cancelled, created, sending, sent, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, pending-virus-check, validation-failed, virus-scan-failed, returned-letter, accepted, received]` | Change the [status argument](https://docs.notifications.service.gov.uk/java.html#status-optional).                |
| `‘Apple’ is not one of [sms, email, letter]`                                                                                                                                                                                                               | Change the [template\_type argument](https://docs.notifications.service.gov.uk/java.html#template-type-optional). |

### Email status descriptions

| Status              | Description                                                                                                                                                                                                                                                                                                                                              |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `created`           | NotifyNL 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`           | NotifyNL has sent the message to the provider. The provider will try to deliver the message to the recipient for up to 72 hours. NotifyNL 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](https://www.gov.uk/service-manual/design/sending-emails-and-text-messages#protect-your-users-from-spam-and-phishing) before you try to send the message again. |
| `technical-failure` | <p>Your message was not sent because there was a problem between Notify and the provider.<br>You’ll have to try sending your messages again.</p>                                                                                                                                                                                                         |

### Text message status descriptions

| Status              | Description                                                                                                                                                                                                                                                                                                                                                                                     |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `created`           | NotifyNL 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`           | NotifyNL has sent the message to the provider. The provider will try to deliver the message to the recipient for up to 72 hours. NotifyNL is waiting for delivery information.                                                                                                                                                                                                                  |
| `pending`           | <p>NotifyNL is waiting for more delivery information.<br>NotifyNL 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.</p>                                                                                                                                               |
| `sent`              | The message was sent to an international number. The mobile networks in some countries do not provide any more delivery information. The NotifyNL 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 NotifyNL support](https://admin.notifynl.nl/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` | <p>Your message was not sent because there was a problem between Notify and the provider.<br>You’ll have to try sending your messages again. You will not be charged for text messages that are affected by a technical failure.</p>                                                                                                                                                            |

### Letter status descriptions

| Status              | Description                                                                         |
| ------------------- | ----------------------------------------------------------------------------------- |
| `accepted`          | NotifyNL 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` | NotifyNL 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`            | NotifyNL 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` | NotifyNL has not completed a virus scan of the precompiled letter file.                                                                                                                                   |
| `virus-scan-failed`   | NotifyNL found a potential virus in the precompiled letter file.                                                                                                                                          |
| `validation-failed`   | Content in the precompiled letter file is outside the printable area. See the [NotifyNL letter specification](https://admin.notifynl.nl/using-notify/guidance/letter-specification) for more information. |
| `technical-failure`   | NotifyNL 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**

```javascript
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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:

* check the response to the [original notification method call](#get-the-data-for-one-message)
* [sign in to NotifyNL](https://admin.notifynl.nl/sign-in) and go to the **API integration** page

**Response**

If the request is successful, the promise resolves with a `response` `object` which will be an instance of the [Buffer](https://nodejs.org/api/buffer.html#buffer_buffer) class containing the raw PDF data.

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                        | How to fix                                                                  |
| ---------------------------------------------------- | --------------------------------------------------------------------------- |
| **ValidationError (status code 400)**                |                                                                             |
| `id is not a valid UUID`                             | Check the notification ID.                                                  |
| `Notification is not a letter`                       | Check that you are looking up the correct notification.                     |
| **PDFNotReadyError (status code 400)**               |                                                                             |
| `PDF not available yet, try again later`             | Wait for the letter to finish processing. This usually takes a few seconds. |
| **BadRequestError (status code 400)**                |                                                                             |
| `File did not pass the virus scan`                   | You cannot retrieve the contents of a letter that contains a virus.         |
| `PDF not available for letters in technical-failure` | You cannot retrieve the contents of a letter in technical-failure.          |
| `Notification is not a letter`                       | Check that you are looking up the correct notification.                     |
| **NoResultFound (status code 404)**                  |                                                                             |
| `No result found`                                    | Check the notification ID.                                                  |

## Get a template

### Get a template by ID

**Method**

This returns the latest version of the template.

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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 NotifyNL](https://admin.notifynl.nl/sign-in) and go to the **Templates** page to find it. For example:

```javascript
let templateId = "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:

```json
{
    "id": "f33517ff-2a88-4f6e-b855-c550268ce08a", // required string - template ID
    "name": "Pigeon registration - appointment email", // required string - template name
    "type": "sms / email / letter" , // required string
    "created_at": "2024-05-10 10:30:31.142535", // required string - date and time template created
    "updated_at": "2024-08-25 13:00:09.123234", // required string - date and time template last updated
    "version": 2, // required integer - template version
    "created_by": "charlie.smith@pigeons.gov.uk", // required string
    "subject": "Your upcoming pigeon registration appointment",  // required string for email and letter - subject of email / heading of letter
    "body": "Dear ((first_name))\r\n\r\nYour pigeon registration appointment is scheduled for ((appointment_date)).\r\n\r\nPlease bring:\r\n\n\n((required_documents))\r\n\r\nYours,\r\nPigeon Affairs Bureau",  // required string - body of notification
    "letter_contact_block": "Pigeons Affairs Bureau\n10 Whitechapel High Street\nLondon\nE1 8EF" // optional string - present for letter templates where contact block is set, otherwise null
}
```

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                       | How to fix                                                                                                                         |
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| **NoResultFound (status code 404)** |                                                                                                                                    |
| `No Result Found`                   | Check your [template ID](https://docs.notifications.service.gov.uk/java.html#get-a-template-by-id-arguments-template-id-required). |

### Get a template by ID and version

**Method**

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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 NotifyNL](https://admin.notifynl.nl/sign-in) and go to the **Templates** page to find it. For example:

```javascript
let templateId = "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:

```json
{
    "id": "f33517ff-2a88-4f6e-b855-c550268ce08a", // required string - template ID
    "name": "Pigeon registration - appointment email", // required string - template name
    "type": "sms / email / letter" , // required string
    "created_at": "2024-05-10 10:30:31.142535", // required string - date and time template created
    "updated_at": "2024-08-25 13:00:09.123234", // required string - date and time template last updated
    "version": 1, // required integer - template version
    "created_by": "charlie.smith@pigeons.gov.uk", // required string
    "subject": "Your upcoming pigeon registration appointment",  // required string for email and letter - subject of email / heading of letter
    "body": "Dear ((first_name))\r\n\r\nYour pigeon registration appointment is scheduled for ((appointment_date)).\r\n\r\nPlease bring:\r\n\n\n((required_documents))\r\n\r\nYours,\r\nPigeon Affairs Bureau",  // required string - body of notification
    "letter_contact_block": "Pigeons Affairs Bureau\n10 Whitechapel High Street\nLondon\nE1 8EF" // optional string - present for letter templates where contact block is set, otherwise null
}
```

**Error codes**

If the request is not successful, the promise fails with an `err`. To learn more about error structure, go to [Errors section](#errors).

| Error message                       | How to fix                                                                                                                         |
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| **NoResultFound (status code 404)** |                                                                                                                                    |
| `No Result Found`                   | Check your [template ID](https://docs.notifications.service.gov.uk/java.html#get-a-template-by-id-arguments-template-id-required). |

### Get all templates

**Method**

This returns the latest version of all templates.

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). The promise will either:

* resolve with a `response` if successful
* fail with an `err` if unsuccessful

**Arguments**

**templateType (optional)**

If you leave out this argument, the method 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:

```json
{
    "templates": [
        {
            "id": "f33517ff-2a88-4f6e-b855-c550268ce08a", // required string - template ID
            "name": "Pigeon registration - appointment email", // required string - template name
            "type": "sms / email / letter" , // required string
            "created_at": "2024-05-10 10:30:31.142535", // required string - date and time template created
            "updated_at": "2024-08-25 13:00:09.123234", // required string - date and time template last updated
            "version": 2, // required integer - template version
            "created_by": "charlie.smith@pigeons.gov.uk", // required string
            "subject": "Your upcoming pigeon registration appointment",  // required string for email and letter - subject of email / heading of letter
            "body": "Dear ((first_name))\r\n\r\nYour pigeon registration appointment is scheduled for ((appointment_date)).\r\n\r\nPlease bring:\r\n\n\n((required_documents))\r\n\r\nYours,\r\nPigeon Affairs Bureau",  // required string - body of notification
            "letter_contact_block": "Pigeons Affairs Bureau\n10 Whitechapel High Street\nLondon\nE1 8EF" // optional string - present for letter templates where contact block is set, otherwise null
        },
        {
        //...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.

```javascript
notifyClient
  .previewTemplateById(templateId, personalisation)
  .then((response) => console.log(response))
  .catch((err) => console.error(err))
```

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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 NotifyNL](https://admin.notifynl.nl/sign-in) and go to the **Templates** page to find it. For example:

```javascript
let templateId = "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:

```json
{
  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:

```json
{
  "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`. To learn more about error structure, go to [Errors section](#errors).

| Error message                                      | How to fix                                                                                                                         |
| -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| **BadRequestError (status code 400)**              |                                                                                                                                    |
| `Missing personalisation: [PERSONALISATION FIELD]` | Check that the personalisation arguments in the method match the placeholder fields in the template.                               |
| **NoResultFound (status code 404)**                |                                                                                                                                    |
| `No Result Found`                                  | Check your [template ID](https://docs.notifications.service.gov.uk/java.html#get-a-template-by-id-arguments-template-id-required). |

## 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`](https://docs.notifications.service.gov.uk/node.html#get-a-page-of-received-text-messages-arguments-olderthan-optional) argument.

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

You can also set up [callbacks](/misc/callbacks.md) for received text messages.

### Enable received text messages

To receive text messages:

1. Go to the **Text message settings** section of the **Settings** page.
2. Select **Change** on the **Receive text messages** row.

### Get a page of received text messages

**Method**

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

The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises). 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:

```javascript
let olderThan = "740e5834-3a29-46b4-9a6f-16142fde533a";
```

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.

```json
{
    "received_text_messages":
    [
        {
            "id": "b51f638b-4295-46e0-a06e-cd41eee7c33b", // required string - ID of received text message
            "user_number": "447700900123", // required string - number of the end user who sent the message
            "notify_number": "07700900456", // required string - your receiving number
            "created_at": "2024-12-12 18:39:16.123346", // required string - date and time template created
            "service_id": "26785a09-ab16-4eb0-8407-a37497a57506", // required string - service ID
            "content": "STRING" // required string - text content
        },
        {
            //...another received text message
        }
    ],
    "links": {
        "current": "/received-text-messages",
        "next": "/received-text-messages?other_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`. To learn more about error structure, go to [Errors section](#errors).

There are no errors specific to this endpoint, but you may encounter:

* [various schema validation errors](#schema-validation-errors), for example when you forget to pass in an argument, or pass in an argument of a wrong type.
* errors that are not related to generating a preview template, but instead are related to things like authentication and rate limits. You can find a list of these errors [in General errors](#general-errors)

## Errors

### Error handling

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

* a status code, for example `400`
* an error type, for example `BadRequestError`
* a message, for example `Mobile numbers can only include: 0 1 2 3 4 5 6 7 8 9 ( ) + -`

These details about the error can be accessed by reading `err.response.data` which is `json`, for example:

```json
{
  errors: [
    {
      error: 'BadRequestError',
      message: 'Mobile numbers can only include: 0 1 2 3 4 5 6 7 8 9 ( ) + -'
    }
  ],
  status_code: 400
}
```

A simple illustration of error handling:

```javascript
notifyClient.sendSms("f33517ff-2a88-4f6e-b855-c550268ce08a", "+447700900123", {
    personalisation: {
                        first_name: "Amala",
                        appointment_date: "1 January 2018 at 1:00pm"
                       },
    reference: "your reference",
    smsSenderId: "445f705f-e394-445d-a5a0-1c80ab958e22"
  })
  .then(response => console.log(response))
  .catch(err => {
      if (err.response.data.status_code >= 500 && err.response.data.status_code <= 599){
          // retry logic;
      }
      else {
        console.log(err.response.data.errors[0]);
      }
})
```

Do not use the content of the messages in your code. These can sometimes change, which may affect your API integration.

Use the status code or the error type instead, as these will not change.

### General errors

You may encounter following errors when making requests to a number of NotifyNL’s API endpoints.

| Error message                                                                                            | How to fix                                                                                                       |
| -------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| **BadRequestError (status code 400)**                                                                    |                                                                                                                  |
| `Cannot send to this recipient using a team-only API key.`                                               | Use a live API key, or add recipient to `Guest list` (located in API Integration section)                        |
| `Cannot send to this recipient when service is in trial mode – see https://admin.notifynl.nl/trial-mode` | You need to request for your service to go live before you can send messages to people outside your team.        |
| **BadRequestError (status code 403)**                                                                    |                                                                                                                  |
| `Error: Your system clock must be accurate to within 30 seconds`                                         | Check your system clock                                                                                          |
| `Invalid token: API key not found`                                                                       | Use the correct API key. Refer to [API keys](/misc/api-keys.md) for more information                             |
| **RateLimitError (status code 429)**                                                                     |                                                                                                                  |
| `Exceeded rate limit for key type <team/test/live> of 3000/<custom limit> requests per 60 seconds`       | Refer to [API rate limits](https://docs.notifications.service.gov.uk/java.html#rate-limits) for more information |
| **TooManyRequestsError (status code 429)**                                                               |                                                                                                                  |
| `Exceeded send limits (<sms/email/letter/international_sms>: <LIMIT SIZE>) for today`                    | Refer to [service limits](/misc/limits.md#daily-limits) for the limit size                                       |
| **Exception (status code 500)**                                                                          |                                                                                                                  |
| `Internal server error`                                                                                  | Notify was unable to process the request, resend your notification.                                              |

### Schema validation errors

The following are a few examples of schema validation errors you may encounter when making a request to a Notify endpoint.

| Error message                                                                            | How to fix                                                                |
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| **ValidationError (status code 400)**                                                    |                                                                           |
| `template_id is a required property`                                                     | Provide the missing argument.                                             |
| `sms_sender_id is not a valid UUID`                                                      | Check the argument to make sure that it is valid for the given data type. |
| `personalisation <data type of argument you sent> is not of type object`                 | Provide argument in the correct type.                                     |
| `reference <reference string you provided> is too long`                                  | Provide a shorter string.                                                 |
| `type <invalid type> is not one of [sms, email, letter]`                                 | Make sure that the argument matches one of the items in the list.         |
| `Additional properties are not allowed (<list of unexpected properties> was unexpected)` | Only provide allowed arguments for the endpoint.                          |

### Endpoint-specific errors

In addition to the above, you may also encounter endpoint-specific errors, which are listed under each relevant API endpoint section.

Find references for endpoint-specific errors in:

* [send a message](#send-a-message)
* [get message data](#get-message-data)
* [get a template](#get-a-template)
* [get received text messages](#get-received-text-messages)

***

{% columns %}
{% column %}
{% content-ref url="/pages/zmjcVSa5Xe7QsTwT7Crx" %}
[REST API documentation](/tech-documentation/rest-api-documentation.md)
{% endcontent-ref %}

{% content-ref url="/pages/gzLqwtaF4w08ZFqaZXmP" %}
[Testing](/misc/testing.md)
{% endcontent-ref %}

{% content-ref url="/pages/eJ5Siv2htOqgwa3qWwtS" %}
[API keys](/misc/api-keys.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
{% content-ref url="/pages/1CI9M7KfQyF0y4yCEZT9" %}
[Limits](/misc/limits.md)
{% endcontent-ref %}

{% content-ref url="/pages/Slz631l7ubYSloQijgE1" %}
[Callbacks](/misc/callbacks.md)
{% endcontent-ref %}

{% content-ref url="/pages/tJqjLxiKW6ITgckwkFno" %}
[API architecture](/misc/api-architecture.md)
{% endcontent-ref %}
{% endcolumn %}
{% endcolumns %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.notifynl.nl/tech-documentation/node-js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
