notifications-node-client 7.0.5 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/DOCUMENTATION.md DELETED
@@ -1,1284 +0,0 @@
1
- # Node.js client documentation
2
-
3
- This documentation is for developers interested in using the GOV.UK Notify Node.js client to send emails, text messages or letters.
4
-
5
- ## Set up the client
6
-
7
- ### Install the client
8
-
9
- Run the following in the command line:
10
-
11
- ```shell
12
- npm install --save notifications-node-client
13
- ```
14
-
15
- ### Create a new instance of the client
16
-
17
- Add this code to your application:
18
-
19
- ```javascript
20
- var NotifyClient = require('notifications-node-client').NotifyClient
21
-
22
- var notifyClient = new NotifyClient(apiKey)
23
- ```
24
-
25
- To get an API key, [sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in) and go to the __API integration__ page. You can find more information in the [API keys](#api-keys) section of this documentation.
26
-
27
- #### Connect through a proxy (optional)
28
-
29
- Add this code to your application:
30
-
31
- ```javascript
32
- proxyConfig = {
33
- host: proxyHost,
34
- port: proxyPort
35
- }
36
-
37
- notifyClient.setProxy(proxyConfig)
38
- ```
39
-
40
- where the `proxyConfig` should be an object supported by [axios](https://github.com/axios/axios).
41
-
42
- ## Send a message
43
-
44
- You can use GOV.UK Notify to send text messages, emails (including documents) and letters.
45
-
46
- ### Send a text message
47
-
48
- #### Method
49
-
50
- ```javascript
51
- notifyClient
52
- .sendSms(templateId, phoneNumber, {
53
- personalisation: personalisation,
54
- reference: reference,
55
- smsSenderId: smsSenderId
56
- })
57
- .then(response => console.log(response))
58
- .catch(err => console.error(err))
59
- ```
60
-
61
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
62
-
63
- - resolve with a `response` if successful
64
- - fail with an `err` if unsuccessful
65
-
66
- #### Arguments
67
-
68
- ##### templateId (required)
69
-
70
- To find the template ID:
71
-
72
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
73
- 1. Go to the __Templates__ page and select the relevant template.
74
- 1. Select __Copy template ID to clipboard__.
75
-
76
- For example:
77
-
78
- ```
79
- 'f33517ff-2a88-4f6e-b855-c550268ce08a'
80
- ```
81
-
82
- ##### phoneNumber (required)
83
-
84
- The phone number of the recipient of the text message. This number can be a UK or international number. For example:
85
-
86
- ```
87
- '+447900900123'
88
- ```
89
-
90
- ##### personalisation (required)
91
-
92
- 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:
93
-
94
- ```javascript
95
- {
96
- 'first_name': 'Amala',
97
- 'reference_number': '300241'
98
- }
99
- ```
100
-
101
- ##### reference (required)
102
-
103
- 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:
104
-
105
- ```
106
- 'your_reference_here'
107
- ```
108
-
109
- ##### smsSenderId (optional)
110
-
111
- A unique identifier of the sender of the text message notification. For example:
112
-
113
- ```
114
- '8e222534-7f05-4972-86e3-17c5d9f894e2'
115
- ```
116
-
117
- To find the text message sender:
118
-
119
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
120
- 1. Go to the __Settings__ page.
121
- 1. In the __Text Messages__ section, select __Manage__ on the __Text Message sender__ row.
122
-
123
- In this screen, you can either:
124
-
125
- - copy the sender ID that you want to use and paste it into the method
126
- - select __Change__ to change the default sender that the service will use, and select __Save__
127
-
128
- You can leave out this argument if your service only has one text message sender, or if you want to use the default sender.
129
-
130
- #### Response
131
-
132
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
133
-
134
- ```javascript
135
- {
136
- 'id': 'bfb50d92-100d-4b8b-b559-14fa3b091cda',
137
- 'reference': null,
138
- 'content': {
139
- 'body': 'Some words',
140
- 'from_number': '40604'
141
- },
142
- 'uri': 'https://api.notifications.service.gov.uk/v2/notifications/ceb50d92-100d-4b8b-b559-14fa3b091cd',
143
- 'template': {
144
- 'id': 'ceb50d92-100d-4b8b-b559-14fa3b091cda',
145
- 'version': 1,
146
- 'uri': 'https://api.notifications.service.gov.uk/v2/templates/bfb50d92-100d-4b8b-b559-14fa3b091cda'
147
- }
148
- }
149
- ```
150
-
151
- If you are using the [test API key](#test), all your messages come back with a `delivered` status.
152
-
153
- All messages sent using the [team and guest list](#team-and-guest-list) or [live](#live) keys appear on your dashboard.
154
-
155
- #### Error codes
156
-
157
- If the request is not successful, the promise fails with an `err`.
158
-
159
- |err.response.data.status_code|err.response.data.errors|How to fix|
160
- |:---|:---|:---|
161
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient using a team-only API key"`<br>`}]`|Use the correct type of [API key](#api-keys)|
162
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
163
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
164
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
165
- |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"`<br>`}]`|Refer to [API rate limits](#rate-limits) for more information|
166
- |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](#daily-limits) for the limit number|
167
- |`500`|`[{`<br>`"error": "Exception",`<br>`"message": "Internal server error"`<br>`}]`|Notify was unable to process the request, resend your notification|
168
-
169
- ### Send an email
170
-
171
- #### Method
172
-
173
- ```javascript
174
- notifyClient
175
- .sendEmail(templateId, emailAddress, {
176
- personalisation: personalisation,
177
- reference: reference,
178
- emailReplyToId: emailReplyToId
179
- })
180
- .then(response => console.log(response))
181
- .catch(err => console.error(err))
182
- ```
183
-
184
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
185
-
186
- - resolve with a `response` if successful
187
- - fail with an `err` if unsuccessful
188
-
189
- #### Arguments
190
-
191
- ##### templateId (required)
192
-
193
- To find the template ID:
194
-
195
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
196
- 1. Go to the __Templates__ page and select the relevant template.
197
- 1. Select __Copy template ID to clipboard__.
198
-
199
- For example:
200
-
201
- ```
202
- "f33517ff-2a88-4f6e-b855-c550268ce08a"
203
- ```
204
-
205
- ##### emailAddress (required)
206
-
207
- The email address of the recipient. For example:
208
-
209
- ```
210
- "sender@something.com"
211
- ```
212
-
213
- ##### personalisation (required)
214
-
215
- 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:
216
-
217
- ```javascript
218
- {
219
- personalisation: {
220
- 'first_name': 'Amala',
221
- 'application_number': '300241'
222
- }
223
- }
224
- ```
225
-
226
- ##### reference (required)
227
-
228
- 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:
229
-
230
- ```
231
- "your_reference_here"
232
- ```
233
-
234
- ##### emailReplyToId (optional)
235
-
236
- 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.
237
-
238
- To add a reply-to email address:
239
-
240
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
241
- 1. Go to the __Settings__ page.
242
- 1. In the __Email__ section, select __Manage__ on the __Reply-to email addresses__ row.
243
- 1. Select __Add reply-to address__.
244
- 1. Enter the email address you want to use, and select __Add__.
245
-
246
- ```
247
- emailReplyToId='8e222534-7f05-4972-86e3-17c5d9f894e2'
248
- ```
249
-
250
- 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.
251
-
252
- #### Response
253
-
254
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
255
-
256
- ```javascript
257
- {
258
- 'id': 'bfb50d92-100d-4b8b-b559-14fa3b091cda',
259
- 'reference': null,
260
- 'content': {
261
- 'subject': 'Licence renewal',
262
- 'body': 'Dear Bill, your licence is due for renewal on 3 January 2016.',
263
- 'from_email': 'the_service@gov.uk'
264
- },
265
- 'uri': 'https://api.notifications.service.gov.uk/v2/notifications/ceb50d92-100d-4b8b-b559-14fa3b091cd',
266
- 'template': {
267
- 'id': 'ceb50d92-100d-4b8b-b559-14fa3b091cda',
268
- 'version': 1,
269
- 'uri': 'https://api.notificaitons.service.gov.uk/service/your_service_id/templates/bfb50d92-100d-4b8b-b559-14fa3b091cda'
270
- }
271
- }
272
- ```
273
-
274
- #### Error codes
275
-
276
- If the request is not successful, the promise fails with an `err`.
277
-
278
- |err.response.data.status_code|err.response.data.errors|How to fix|
279
- |:--- |:---|:---|
280
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient using a team-only API key"`<br>`}]`|Use the correct type of [API key](#api-keys)|
281
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
282
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
283
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
284
- |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"`<br>`}]`|Refer to [API rate limits](#rate-limits) for more information|
285
- |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](#daily-limits) for the limit number|
286
- |`500`|`[{`<br>`"error": "Exception",`<br>`"message": "Internal server error"`<br>`}]`|Notify was unable to process the request, resend your notification|
287
-
288
- ### Send a file by email
289
-
290
- 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.
291
-
292
- The links are unique and unguessable. GOV.UK Notify cannot access or decrypt your file.
293
-
294
- Your file will be available to download for a default period of 26 weeks (6 months).
295
-
296
- To help protect your files you can also:
297
-
298
- * ask recipients to confirm their email address before downloading
299
- * choose the length of time that a file is available to download
300
-
301
- #### Add contact details to the file download page
302
-
303
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
304
- 1. Go to the __Settings__ page.
305
- 1. In the __Email__ section, select __Manage__ on the __Send files by email__ row.
306
- 1. Enter the contact details you want to use, and select __Save__.
307
-
308
- #### Add a placeholder to the template
309
-
310
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
311
- 1. Go to the __Templates__ page and select the relevant email template.
312
- 1. Select __Edit__.
313
- 1. Add a placeholder to the email template using double brackets. For example: "Download your file at: ((link_to_file))"
314
-
315
- Your email should also tell recipients how long the file will be available to download.
316
-
317
- #### Upload your file
318
-
319
- You can upload PDF, CSV, JSON, .odt, .txt, .rtf, .xlsx and MS Word Document files. Your file must be smaller than 2MB. [Contact the GOV.UK Notify team](https://www.notifications.service.gov.uk/support/ask-question-give-feedback) if you need to send other file types.
320
-
321
- Pass the file object as a value into the `personalisation` argument. For example:
322
-
323
- ```javascript
324
- var fs = require('fs')
325
-
326
- fs.readFile('path/to/document.pdf', function (err, pdfFile) {
327
- console.log(err)
328
- notifyClient.sendEmail(templateId, emailAddress, {
329
- personalisation: {
330
- first_name: 'Amala',
331
- application_date: '2018-01-01',
332
- link_to_file: notifyClient.prepareUpload(pdfFile)
333
- }
334
- }).then(response => console.log(response)).catch(err => console.error(err))
335
- })
336
- ```
337
-
338
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
339
-
340
- - resolve with a `response` if successful
341
- - fail with an `err` if unsuccessful
342
-
343
- ##### CSV Files
344
-
345
- Uploads for CSV files should use the `isCsv` option on the `prepareUpload()` function. For example:
346
-
347
- ```javascript
348
- var fs = require('fs')
349
-
350
- fs.readFile('path/to/document.csv', function (err, csvFile) {
351
- console.log(err)
352
- notifyClient.sendEmail(templateId, emailAddress, {
353
- personalisation: {
354
- first_name: 'Amala',
355
- application_date: '2018-01-01',
356
- link_to_file: notifyClient.prepareUpload(csvFile, { isCsv: true })
357
- }
358
- }).then(response => console.log(response)).catch(err => console.error(err))
359
- })
360
- ```
361
-
362
- #### Ask recipients to confirm their email address before they can download the file
363
-
364
- 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.
365
-
366
- This security feature is turned on by default.
367
-
368
- ##### Turn off email address check (not recommended)
369
-
370
- If you do not want to use this feature, you can turn it off on a file-by-file basis.
371
-
372
- To do this you will need version 5.2.0 of the Node.js client library, or a more recent version.
373
-
374
- You should not turn this feature off if you send files that contain:
375
-
376
- * personally identifiable information
377
- * commercially sensitive information
378
- * information classified as ‘OFFICIAL’ or ‘OFFICIAL-SENSITIVE’ under the [Government Security Classifications](https://www.gov.uk/government/publications/government-security-classifications) policy
379
-
380
- To let the recipient download the file without confirming their email address, set the `confirmEmailBeforeDownload` option to `false`.
381
-
382
- ```javascript
383
- var fs = require('fs')
384
-
385
- fs.readFile('path/to/document.pdf', function (err, pdfFile) {
386
- console.log(err)
387
- notifyClient.sendEmail(templateId, emailAddress, {
388
- personalisation: {
389
- first_name: 'Amala',
390
- application_date: '2018-01-01',
391
- link_to_file: notifyClient.prepareUpload(pdfFile, { confirmEmailBeforeDownload: false })
392
- }
393
- }).then(response => console.log(response)).catch(err => console.error(err))
394
- })
395
- ```
396
-
397
- #### Choose the length of time that a file is available to download
398
-
399
- Set the number of weeks you want the file to be available using the `retentionPeriod` option.
400
-
401
- To use this feature will need version 5.2.0 of the Node.js client library, or a more recent version.
402
-
403
- You can choose any value between 1 week and 78 weeks. When deciding this, you should consider:
404
-
405
- * the need to protect the recipient’s personal information
406
- * whether the recipient will need to download the file again later
407
-
408
- If you do not choose a value, the file will be available for the default period of 26 weeks (6 months).
409
-
410
- Files sent before 12 April 2023 had a longer default period of 78 weeks (18 months).
411
-
412
- ```javascript
413
- var fs = require('fs')
414
-
415
- fs.readFile('path/to/document.pdf', function (err, pdfFile) {
416
- console.log(err)
417
- notifyClient.sendEmail(templateId, emailAddress, {
418
- personalisation: {
419
- first_name: 'Amala',
420
- application_date: '2018-01-01',
421
- link_to_file: notifyClient.prepareUpload(pdfFile, { retentionPeriod: '52 weeks' })
422
- }
423
- }).then(response => console.log(response)).catch(err => console.error(err))
424
- })
425
- ```
426
-
427
- #### Response
428
-
429
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
430
-
431
- ```javascript
432
- {
433
- 'id': '740e5834-3a29-46b4-9a6f-16142fde533a',
434
- 'reference': 'your_reference_here',
435
- 'content': {
436
- 'subject': 'SUBJECT TEXT',
437
- 'body': 'MESSAGE TEXT',
438
- 'from_email': 'SENDER EMAIL'
439
- },
440
- 'uri': 'https://api.notifications.service.gov.uk/v2/notifications/740e5834-3a29-46b4-9a6f-16142fde533a',
441
- 'template': {
442
- 'id': 'f33517ff-2a88-4f6e-b855-c550268ce08a',
443
- 'version': INTEGER,
444
- 'uri': 'https://api.notifications.service.gov.uk/v2/template/f33517ff-2a88-4f6e-b855-c550268ce08a'
445
- }
446
- }
447
- ```
448
-
449
- #### Error codes
450
-
451
- If the request is not successful, the promise fails with an `err`.
452
-
453
- |err.response.data.status_code|err.response.data.errors|How to fix|
454
- |:---|:---|:---|
455
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient using a team-only API key"`<br>`}]`|Use the correct type of [API key](#api-keys)|
456
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can't send to this recipient when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
457
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported file type '(FILE TYPE)'. Supported types are: '(ALLOWED TYPES)'"`<br>`}]`|Wrong file type. You can only upload .pdf, .csv, .txt, .doc, .docx, .xlsx, .rtf or .odt files|
458
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported value for retention_period '(PERIOD)'. Supported periods are from 1 to 78 weeks."`<br>`}]`|Choose a period between 1 and 78 weeks|
459
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Unsupported value for confirm_email_before_download: '(VALUE)'. Use a boolean true or false value."`<br>`}]`|Use either true or false|
460
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "File did not pass the virus scan"`<br>`}]`|The file contains a virus|
461
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"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"`<br>`}]`|See how to [add contact details to the file download page](#add-contact-details-to-the-file-download-page)|
462
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Can only send a file by email"`<br>`}]`|Make sure you are using an email template|
463
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
464
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
465
- |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"`<br>`}]`|Refer to [API rate limits](#rate-limits) for more information|
466
- |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](#daily-limits) for the limit number|
467
- |`500`|`[{`<br>`"error": "Exception",`<br>`"message": "Internal server error"`<br>`}]`|Notify was unable to process the request, resend your notification.|
468
- |`N/A`|`File is larger than 2MB`|The file is too big. Files must be smaller than 2MB|
469
-
470
- ### Send a letter
471
-
472
- When you add a new service it will start in [trial mode](https://www.notifications.service.gov.uk/features/trial-mode). You can only send letters when your service is live.
473
-
474
- To send Notify a request to go live:
475
-
476
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
477
- 1. Go to the __Settings__ page.
478
- 1. In the __Your service is in trial mode__ section, select __request to go live__.
479
-
480
- #### Method
481
-
482
- ```javascript
483
- notifyClient
484
- .sendLetter(templateId, {
485
- personalisation: personalisation,
486
- reference: reference
487
- })
488
- .then(response => console.log(response))
489
- .catch(err => console.error(err))
490
- ```
491
-
492
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
493
-
494
- - resolve with a `response` if successful
495
- - fail with an `err` if unsuccessful
496
-
497
- #### Arguments
498
-
499
- ##### templateId (required)
500
-
501
- To find the template ID:
502
-
503
- 1. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in).
504
- 1. Go to the __Templates__ page and select the relevant template.
505
- 1. Select __Copy template ID to clipboard__.
506
-
507
- For example:
508
-
509
- ```
510
- "f33517ff-2a88-4f6e-b855-c550268ce08a"
511
- ```
512
-
513
- ##### personalisation (required)
514
-
515
- The personalisation argument always contains the following parameters for the letter recipient’s address:
516
-
517
- - `address_line_1`
518
- - `address_line_2`
519
- - `address_line_3`
520
- - `address_line_4`
521
- - `address_line_5`
522
- - `address_line_6`
523
- - `address_line_7`
524
-
525
- The address must have at least 3 lines.
526
-
527
- The last line needs to be a real UK postcode or the name of a country outside the UK.
528
-
529
- Notify checks for international addresses and will automatically charge you the correct postage.
530
-
531
- The `postcode` personalisation argument has been replaced. If your template still uses `postcode`, Notify will treat it as the last line of the address.
532
-
533
- Any other placeholder fields included in the letter template also count as required parameters. You must provide their values in an `object`. For example:
534
-
535
- ```javascript
536
- {
537
- personalisation: {
538
- 'address_line_1': 'The Occupier',
539
- 'address_line_2': '123 High Street',
540
- 'address_line_3': 'SW14 6BH',
541
- 'application_date': '2018-01-01'
542
- }
543
- }
544
- ```
545
-
546
- ##### reference (optional)
547
-
548
- 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:
549
-
550
- ```
551
- "your_reference_here"
552
- ```
553
-
554
- #### Response
555
-
556
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
557
-
558
- ```javascript
559
- {
560
- 'id': '740e5834-3a29-46b4-9a6f-16142fde533a',
561
- 'reference': null,
562
- 'content': {
563
- 'subject': 'Licence renewal',
564
- 'body': 'Dear Bill, your licence is due for renewal on 3 January 2016.'
565
- },
566
- 'uri': 'https://api.notifications.service.gov.uk/v2/notifications/740e5834-3a29-46b4-9a6f-16142fde533a',
567
- 'template': {
568
- 'id': 'f33517ff-2a88-4f6e-b855-c550268ce08a',
569
- 'version': 1,
570
- 'uri': 'https://api.notifications.service.gov.uk/v2/template/f33517ff-2a88-4f6e-b855-c550268ce08a'
571
- },
572
- 'scheduled_for': null
573
- }
574
- ```
575
-
576
- #### Error codes
577
-
578
- If the request is not successful, the promise fails with an `err`.
579
-
580
- |err.response.data.status_code|err.response.data.errors|How to fix|
581
- |:--- |:---|:---|
582
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send letters with a team api key"`<br>`}]`|Use the correct type of [API key](#api-keys).|
583
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send letters when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode).|
584
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "personalisation address_line_1 is a required property"`<br>`}]`|Ensure that your template has a field for the first line of the address, refer to [personalisation](#send-a-letter-arguments-personalisation-required) for more information.|
585
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "Must be a real UK postcode"`<br>`}]`|Ensure that the value for the last line of the address is a real UK postcode.|
586
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "Last line of address must be a real UK postcode or another country"`<br>`}]`|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.|
587
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock.|
588
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information.|
589
- |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"`<br>`}]`|Refer to [API rate limits](#rate-limits) for more information.|
590
- |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](#daily-limits) for the limit number.|
591
- |`500`|`[{`<br>`"error": "Exception",`<br>`"message": "Internal server error"`<br>`}]`|Notify was unable to process the request, resend your notification.|
592
-
593
-
594
- ### Send a precompiled letter
595
-
596
- #### Method
597
-
598
- ```javascript
599
- var response = notifyClient.sendPrecompiledLetter(
600
- reference,
601
- pdfFile,
602
- postage
603
- )
604
- ```
605
-
606
- #### Arguments
607
-
608
- ##### reference (required)
609
-
610
- 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:
611
-
612
- ```
613
- "your_reference_here"
614
- ```
615
-
616
- ##### pdfFile
617
-
618
- The precompiled letter must be a PDF file which meets [the GOV.UK Notify letter specification](https://www.notifications.service.gov.uk/using-notify/guidance/letter-specification). For example:
619
-
620
- ```javascript
621
- var fs = require('fs')
622
-
623
- fs.readFile('path/to/document.pdf', function (err, pdfFile) {
624
- if (err) {
625
- console.error(err)
626
- }
627
- var notification = notifyClient.sendPrecompiledLetter(
628
- 'your reference', pdfFile
629
- )
630
- })
631
- ```
632
-
633
- ##### postage (optional)
634
-
635
- 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.
636
-
637
-
638
- #### Response
639
-
640
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
641
-
642
- ```javascript
643
- {
644
- 'id': '740e5834-3a29-46b4-9a6f-16142fde533a',
645
- 'reference': 'your-letter-reference',
646
- 'postage': 'second'
647
- }
648
- ```
649
-
650
- #### Error codes
651
-
652
- If the request is not successful, the promise fails with an `err`.
653
-
654
- |err.response.data.status_code|err.response.data.errors|How to fix|
655
- |:--- |:---|:---|
656
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send letters with a team api key"`<br>`}]`|Use the correct type of [API key](#api-keys)|
657
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Cannot send letters when service is in trial mode - see https://www.notifications.service.gov.uk/trial-mode"`<br>`}]`|Your service cannot send this notification in [trial mode](https://www.notifications.service.gov.uk/features/using-notify#trial-mode)|
658
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "personalisation address_line_1 is a required property"`<br>`}]`|Send a valid PDF file|
659
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "postage invalid. It must be either first or second."`<br>`}]`|Change the value of `postage` argument in the method call to either 'first' or 'second'|
660
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
661
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
662
- |`429`|`[{`<br>`"error": "RateLimitError",`<br>`"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"`<br>`}]`|Refer to [API rate limits](#rate-limits) for more information|
663
- |`429`|`[{`<br>`"error": "TooManyRequestsError",`<br>`"message": "Exceeded send limits (LIMIT NUMBER) for today"`<br>`}]`|Refer to [service limits](#daily-limits) for the limit number|
664
- |N/A|`"message":"precompiledPDF must be a valid PDF file"`|Send a valid PDF file|
665
- |N/A|`"message":"reference cannot be null or empty"`|Populate the reference parameter|
666
- |N/A|`"message":"precompiledPDF cannot be null or empty"`|Send a PDF file with data in it|
667
-
668
- ## Get message status
669
-
670
-
671
- ### Get the status of one message
672
-
673
- You can only get the status of messages sent within the retention period. The default retention period is 7 days.
674
-
675
- #### Method
676
-
677
- ```javascript
678
- notifyClient
679
- .getNotificationById(notificationId)
680
- .then((response) => console.log(response))
681
- .catch((err) => console.error(err))
682
- ```
683
-
684
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
685
-
686
- - resolve with a `response` if successful
687
- - fail with an `err` if unsuccessful
688
-
689
- #### Arguments
690
-
691
- ##### notificationId (required)
692
-
693
- The ID of the notification. To find the notification ID, you can either:
694
-
695
- * check the response to the [original notification method call](#response)
696
- * [sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in) and go to the __API integration__ page
697
-
698
- #### Response
699
-
700
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
701
-
702
- ```javascript
703
- {
704
- 'id': 'notify_id',
705
- 'body': 'Hello Foo',
706
- 'subject': 'null|email_subject',
707
- 'reference': 'client reference',
708
- 'email_address': 'email address',
709
- 'phone_number': 'phone number',
710
- 'line_1': 'full name of a person or company',
711
- 'line_2': '123 The Street',
712
- 'line_3': 'Some Area',
713
- 'line_4': 'Some Town',
714
- 'line_5': 'Some county',
715
- 'line_6': 'Something else',
716
- 'line_7': 'UK postcode or country',
717
- 'postage': 'first or second',
718
- 'type': 'sms|letter|email',
719
- 'status': 'current status',
720
- 'template': {
721
- 'version': 1,
722
- 'id': 1,
723
- 'uri': '/template/{id}/{version}'
724
- },
725
- 'created_by_name': 'name of the person who sent the notification if sent manually',
726
- 'created_at': 'created at',
727
- 'sent_at': 'sent to provider at'
728
- }
729
- ```
730
-
731
- #### Error codes
732
-
733
- If the request is not successful, the promise fails with an `err`.
734
-
735
- |err.response.data.status_code|err.response.data.errors|How to fix|
736
- |:---|:---|:---|
737
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "id is not a valid UUID"`<br>`}]`|Check the notification ID|
738
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
739
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
740
- |`404`|`[{`<br>`"error": "NoResultFound",`<br>`"message": "No result found"`<br>`}]`|Check the notification ID|
741
-
742
-
743
- ### Get the status of multiple messages
744
-
745
- 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`](#olderthan-optional) argument.
746
-
747
- You can only get the status of messages that are 7 days old or newer.
748
-
749
- #### Method
750
-
751
- ```javascript
752
- notifyClient
753
- .getNotifications(templateType, status, reference, olderThan)
754
- .then((response) => console.log(response))
755
- .catch((err) => console.error(err))
756
- ```
757
-
758
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
759
-
760
- - resolve with a `response` if successful
761
- - fail with an `err` if unsuccessful
762
-
763
- To get the most recent messages, you must pass in an empty `olderThan` argument or `null`.
764
-
765
- 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.
766
-
767
- #### Arguments
768
-
769
- You can pass in empty arguments or `null` to ignore these filters.
770
-
771
- ##### status (optional)
772
-
773
- You can filter by each:
774
-
775
- * [email status](#email-status-descriptions)
776
- * [text message status](#text-message-status-descriptions)
777
- * [letter status](#letter-status-descriptions)
778
- * [precompiled letter status](#precompiled-letter-status-descriptions)
779
-
780
- You can leave out this argument to ignore this filter.
781
-
782
- ##### notificationType (optional)
783
-
784
- You can filter by:
785
-
786
- * `email`
787
- * `sms`
788
- * `letter`
789
-
790
- ##### reference (optional)
791
-
792
- 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:
793
-
794
- ```
795
- "your_reference_here"
796
- ```
797
-
798
- ##### olderThan (optional)
799
-
800
- 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:
801
-
802
- ```
803
- "8e222534-7f05-4972-86e3-17c5d9f894e2"
804
- ```
805
-
806
- If you pass in an empty argument or `null`, the client returns the most recent 250 notifications.
807
-
808
- #### Response
809
-
810
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
811
-
812
- ```javascript
813
- {
814
- 'notifications': [
815
- {
816
- 'id': 'notify_id',
817
- 'reference': 'client reference',
818
- 'email_address': 'email address',
819
- 'phone_number': 'phone number',
820
- 'line_1': 'full name of a person or company',
821
- 'line_2': '123 The Street',
822
- 'line_3': 'Some Area',
823
- 'line_4': 'Some Town',
824
- 'line_5': 'Some county',
825
- 'line_6': 'Something else',
826
- 'postcode': 'postcode',
827
- 'postage': 'first or second',
828
- 'type': 'sms | letter | email',
829
- 'status': 'sending | delivered | permanent-failure | temporary-failure | technical-failure',
830
- 'template': {
831
- 'version': 1,
832
- 'id': 1,
833
- 'uri': '/template/{id}/{version}'
834
- },
835
- 'created_by_name': 'name of the person who sent the notification if sent manually',
836
- 'created_at': 'created at',
837
- 'sent_at': 'sent to provider at'
838
- }
839
- // …
840
- ],
841
- 'links': {
842
- 'current': '/notifications?template_type=sms&status=delivered',
843
- 'next': '/notifications?other_than=last_id_in_list&template_type=sms&status=delivered'
844
- }
845
- }
846
- ```
847
-
848
- #### Error codes
849
-
850
- If the request is not successful, the promise fails with an `err`.
851
-
852
- |err.response.data.status_code|err.response.data.errors|How to fix|
853
- |:---|:---|:---|
854
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "bad status is not one of [created, sending, sent, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, accepted, received]"`<br>`}]`|Contact the GOV.UK Notify team|
855
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "Applet is not one of [sms, email, letter]"`<br>`}]`|Contact the GOV.UK Notify team|
856
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
857
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
858
-
859
- ### Email status descriptions
860
-
861
- |Status|Description|
862
- |:---|:---|
863
- |#`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.|
864
- |#`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.|
865
- |#`delivered`|The message was successfully delivered.|
866
- |#`permanent-failure`|The provider could not deliver the message because the email address was wrong. You should remove these email addresses from your database.|
867
- |#`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.|
868
- |#`technical-failure`|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.|
869
-
870
- ### Text message status descriptions
871
-
872
- |Status|Description|
873
- |:---|:---|
874
- |#`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.|
875
- |#`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.|
876
- |#`pending`|GOV.UK Notify is waiting for more delivery information.<br>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.|
877
- |#`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'.|
878
- |#`delivered`|The message was successfully delivered. If a recipient blocks your sender name or mobile number, your message will still show as delivered.|
879
- |#`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](https://www.notifications.service.gov.uk/support). If not, you should remove them from your database. You’ll still be charged for text messages that cannot be delivered.
880
- |#`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.|
881
- |#`technical-failure`|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.|
882
-
883
- ### Letter status descriptions
884
-
885
- |Status|Description|
886
- |:---|:---|
887
- |#`accepted`|GOV.UK Notify has sent the letter to the provider to be printed.|
888
- |#`received`|The provider has printed and dispatched the letter.|
889
- |#`cancelled`|Sending cancelled. The letter will not be printed or dispatched.|
890
- |#`technical-failure`|GOV.UK Notify had an unexpected error while sending the letter to our printing provider.|
891
- |#`permanent-failure`|The provider cannot print the letter. Your letter will not be dispatched.|
892
-
893
- ### Precompiled letter status descriptions
894
-
895
- |Status|Description|
896
- |:---|:---|
897
- |#`accepted`|GOV.UK Notify has sent the letter to the provider to be printed.|
898
- |#`received`|The provider has printed and dispatched the letter.|
899
- |#`cancelled`|Sending cancelled. The letter will not be printed or dispatched.|
900
- |#`pending-virus-check`|GOV.UK Notify has not completed a virus scan of the precompiled letter file.|
901
- |#`virus-scan-failed`|GOV.UK Notify found a potential virus in the precompiled letter file.|
902
- |#`validation-failed`|Content in the precompiled letter file is outside the printable area. See the [GOV.UK Notify letter specification](https://www.notifications.service.gov.uk/using-notify/guidance/letter-specification) for more information.|
903
- |#`technical-failure`|GOV.UK Notify had an unexpected error while sending the letter to our printing provider.|
904
- |#`permanent-failure`|The provider cannot print the letter. Your letter will not be dispatched.|
905
-
906
- ### Get a PDF for a letter notification
907
-
908
- #### Method
909
-
910
- ```javascript
911
- notifyClient
912
- .getPdfForLetterNotification(notificationId)
913
- .then(function (fileBuffer) {
914
- return fileBuffer
915
- }) /* the response is a file buffer, an instance of Buffer */
916
- .catch((err) => console.error(err))
917
- ```
918
-
919
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
920
-
921
- - resolve with `fileBuffer` if successful
922
- - fail with an `err` if unsuccessful
923
-
924
- #### Arguments
925
-
926
- ##### notificationId (required)
927
-
928
- The ID of the notification. To find the notification ID, you can either:
929
-
930
- * check the response to the [original notification method call](#get-the-status-of-one-message-response)
931
- * [sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in) and go to the __API integration__ page
932
-
933
- #### Response
934
-
935
- If the request to the client is successful, the client will return an instance of the [Buffer](https://nodejs.org/api/buffer.html#buffer_buffer) class containing the raw PDF data.
936
-
937
- #### Error codes
938
-
939
- If the request is not successful, the client will return an `HTTPError` containing the relevant error code:
940
-
941
- |error.status_code|error.message|How to fix|
942
- |:---|:---|:---|
943
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "id is not a valid UUID"`<br>`}]`|Check the notification ID|
944
- |`400`|`[{`<br>`"error": "PDFNotReadyError",`<br>`"message": "PDF not available yet, try again later"`<br>`}]`|Wait for the notification to finish processing. This usually takes a few seconds|
945
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "File did not pass the virus scan"`<br>`}]`|You cannot retrieve the contents of a letter notification that contains a virus|
946
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "PDF not available for letters in technical-failure"`<br>`}]`|You cannot retrieve the contents of a letter notification in technical-failure|
947
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "Notification is not a letter"`<br>`}]`|Check that you are looking up the correct notification|
948
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
949
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
950
- |`404`|`[{`<br>`"error": "NoResultFound",`<br>`"message": "No result found"`<br>`}]`|Check the notification ID|
951
-
952
-
953
- ## Get a template
954
-
955
- ### Get a template by ID
956
-
957
- #### Method
958
-
959
- This returns the latest version of the template.
960
-
961
- ```javascript
962
- notifyClient
963
- .getTemplateById(templateId)
964
- .then((response) => console.log(response))
965
- .catch((err) => console.error(err))
966
- ```
967
-
968
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
969
-
970
- - resolve with a `response` if successful
971
- - fail with an `err` if unsuccessful
972
-
973
- #### Arguments
974
-
975
- ##### templateId (required)
976
-
977
- The ID of the template. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in) and go to the __Templates__ page to find it. For example:
978
-
979
- ```
980
- "f33517ff-2a88-4f6e-b855-c550268ce08a"
981
- ```
982
-
983
- #### Response
984
-
985
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
986
-
987
- ```javascript
988
- {
989
- 'id': 'template_id',
990
- 'name': 'template name',
991
- 'type': 'sms|email|letter',
992
- 'created_at': 'created at',
993
- 'updated_at': 'updated at',
994
- 'version': 'version',
995
- 'created_by': 'someone@example.com',
996
- 'body': 'body',
997
- 'subject': 'null|email_subject',
998
- 'letter_contact_block': 'null|letter_contact_block'
999
- }
1000
- ```
1001
-
1002
- #### Error codes
1003
-
1004
- If the request is not successful, the promise fails with an `err`.
1005
-
1006
- |err.response.data.status_code|err.response.data.errors|How to fix|
1007
- |:---|:---|:---|
1008
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
1009
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
1010
- |`404`|`[{`<br>`"error": "NoResultFound",`<br>`"message": "No Result Found"`<br>`}]`|Check your [template ID](#get-a-template-by-id-arguments-templateid-required)|
1011
-
1012
-
1013
- ### Get a template by ID and version
1014
-
1015
- #### Method
1016
-
1017
- ```javascript
1018
- notifyClient
1019
- .getTemplateByIdAndVersion(templateId, version)
1020
- .then((response) => console.log(response))
1021
- .catch((err) => console.error(err))
1022
- ```
1023
-
1024
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
1025
-
1026
- - resolve with a `response` if successful
1027
- - fail with an `err` if unsuccessful
1028
-
1029
- #### Arguments
1030
-
1031
- ##### templateId (required)
1032
-
1033
- The ID of the template. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in) and go to the __Templates__ page to find it. For example:
1034
-
1035
- ```
1036
- "f33517ff-2a88-4f6e-b855-c550268ce08a"
1037
- ```
1038
-
1039
- ##### version (required)
1040
-
1041
- The version number of the template.
1042
-
1043
- #### Response
1044
-
1045
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
1046
-
1047
- ```javascript
1048
- {
1049
- 'id': 'template_id',
1050
- 'name': 'template name',
1051
- 'type': 'sms|email|letter',
1052
- 'created_at': 'created at',
1053
- 'updated_at': 'updated at',
1054
- 'version': 'version',
1055
- 'created_by': 'someone@example.com',
1056
- 'body': 'body',
1057
- 'subject': 'null|email_subject',
1058
- 'letter_contact_block': 'null|letter_contact_block'
1059
- }
1060
- ```
1061
-
1062
- #### Error codes
1063
-
1064
- If the request is not successful, the promise fails with an `err`.
1065
-
1066
- |err.response.data.status_code|err.response.data.errors|How to fix|
1067
- |:---|:---|:---|
1068
- |`400`|`[{`<br>`"error": "ValidationError",`<br>`"message": "id is not a valid UUID"`<br>`}]`|Check the notification ID|
1069
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
1070
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
1071
- |`404`|`[{`<br>`"error": "NoResultFound",`<br>`"message": "No Result Found"`<br>`}]`|Check your [template ID](#get-a-template-by-id-and-version-arguments-templateid-required) and [version](#version-required)|
1072
-
1073
-
1074
- ### Get all templates
1075
-
1076
- #### Method
1077
-
1078
- This returns the latest version of all templates.
1079
-
1080
- ```javascript
1081
- notifyClient
1082
- .getAllTemplates(templateType)
1083
- .then((response) => console.log(response))
1084
- .catch((err) => console.error(err))
1085
- ```
1086
-
1087
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
1088
-
1089
- - resolve with a `response` if successful
1090
- - fail with an `err` if unsuccessful
1091
-
1092
- #### Arguments
1093
-
1094
- ##### templateType (optional)
1095
-
1096
- If you do not use `templateType`, the client returns all templates. Otherwise you can filter by:
1097
-
1098
- - `email`
1099
- - `sms`
1100
- - `letter`
1101
-
1102
- #### Response
1103
-
1104
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
1105
-
1106
- ```javascript
1107
- {
1108
- 'templates': [
1109
- {
1110
- 'id': 'template_id',
1111
- 'name': 'template name',
1112
- 'type': 'sms|email|letter',
1113
- 'created_at': 'created at',
1114
- 'updated_at': 'updated at',
1115
- 'version': 'version',
1116
- 'created_by': 'someone@example.com',
1117
- 'body': 'body',
1118
- 'subject': 'null|email_subject',
1119
- 'letter_contact_block': 'null|letter_contact_block'
1120
- },
1121
- {
1122
- // … another template
1123
- }
1124
- ]
1125
- }
1126
- ```
1127
-
1128
- If no templates exist for a template type or there no templates for a service, the object is empty.
1129
-
1130
- ### Generate a preview template
1131
-
1132
- #### Method
1133
-
1134
- This generates a preview version of a template.
1135
-
1136
- ```javascript
1137
- personalisation = { 'foo': 'bar' }
1138
- notifyClient
1139
- .previewTemplateById(templateId, personalisation)
1140
- .then((response) => console.log(response))
1141
- .catch((err) => console.error(err))
1142
- ```
1143
-
1144
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
1145
-
1146
- - resolve with a `response` if successful
1147
- - fail with an `err` if unsuccessful
1148
-
1149
- 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.
1150
-
1151
- #### Arguments
1152
-
1153
- ##### templateId (required)
1154
-
1155
- The ID of the template. [Sign in to GOV.UK Notify](https://www.notifications.service.gov.uk/sign-in) and go to the __Templates__ page to find it. For example:
1156
-
1157
- ```
1158
- "f33517ff-2a88-4f6e-b855-c550268ce08a"
1159
- ```
1160
-
1161
- ##### personalisation (optional)
1162
-
1163
- 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:
1164
-
1165
- ```javascript
1166
- {
1167
- personalisation: {
1168
- 'first_name': 'Amala',
1169
- 'reference_number': '300241'
1170
- }
1171
- }
1172
- ```
1173
-
1174
- You can leave out this argument if a template does not have any placeholder fields for personalised information.
1175
-
1176
- #### Response
1177
-
1178
- If the request is successful, the promise resolves with a `response` `object`. For example, the `response.data` property will look like this:
1179
-
1180
- ```javascript
1181
- {
1182
- 'id': 'notify_id',
1183
- 'type': 'sms|email|letter',
1184
- 'version': 'version',
1185
- 'body': 'Hello bar', // with substitution values
1186
- 'subject': 'null|email_subject',
1187
- 'html': '<p>Example</p>' // Returns the rendered body (email templates only)
1188
- }
1189
- ```
1190
-
1191
- #### Error codes
1192
-
1193
- If the request is not successful, the promise fails with an `err`.
1194
-
1195
- |err.response.data.status_code|err.response.data.errors|How to fix|
1196
- |:---|:---|:---|
1197
- |`400`|`[{`<br>`"error": "BadRequestError",`<br>`"message": "Missing personalisation: [PERSONALISATION FIELD]"`<br>`}]`|Check that the personalisation arguments in the method match the placeholder fields in the template|
1198
- |`400`|`[{`<br>`"error": "NoResultFound",`<br>`"message": "No result found"`<br>`}]`|Check the [template ID](#generate-a-preview-template-arguments-templateid-required)|
1199
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
1200
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|
1201
-
1202
-
1203
- ## Get received text messages
1204
-
1205
- 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`](#get-a-page-of-received-text-messages-arguments-olderthan-optional) argument.
1206
-
1207
- You can only get messages that are 7 days old or newer.
1208
-
1209
- You can also set up [callbacks](#callbacks) for received text messages.
1210
-
1211
- ### Enable received text messages
1212
-
1213
- To receive text messages:
1214
-
1215
- 1. Go to the **Text message settings** section of the **Settings** page.
1216
- 1. Select **Change** on the **Receive text messages** row.
1217
-
1218
- ### Get a page of received text messages
1219
-
1220
- #### Method
1221
-
1222
- ```javascript
1223
- notifyClient
1224
- .getReceivedTexts(olderThan)
1225
- .then((response) => console.log(response))
1226
- .catch((err) => console.error(err))
1227
- ```
1228
-
1229
- The method returns a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) [external link]. The promise will either:
1230
-
1231
- - resolve with a `response` if successful
1232
- - fail with an `err` if unsuccessful
1233
-
1234
- To get the most recent messages, you must pass in an empty argument or `null`.
1235
-
1236
- 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.
1237
-
1238
- #### Arguments
1239
-
1240
- ##### olderThan (optional)
1241
-
1242
- 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:
1243
-
1244
- ```
1245
- 8e222534-7f05-4972-86e3-17c5d9f894e2"
1246
- ```
1247
-
1248
- If you pass in an empty argument or `null`, the client returns the most recent 250 text messages.
1249
-
1250
- #### Response
1251
-
1252
- If the request to the client is successful, the promise resolves with an `object` containing all received texts.
1253
-
1254
- ```javascript
1255
- {
1256
- 'received_text_messages':
1257
- [
1258
- {
1259
- 'id': 'notify_id', // required
1260
- 'user_number': 'user number', // required user number
1261
- 'notify_number': 'notify number', // receiving number
1262
- 'created_at': 'created at', // required
1263
- 'service_id': 'service id', // required service id
1264
- 'content': 'text content' // required text content
1265
- }
1266
- // …
1267
- ],
1268
- 'links': {
1269
- 'current': '/received-test-messages',
1270
- 'next': '/received-text-messages?older_than=last_id_in_list'
1271
- }
1272
- }
1273
- ```
1274
-
1275
- If the notification specified in the `olderThan` argument is older than 7 days, the promise resolves an empty response.
1276
-
1277
- #### Error codes
1278
-
1279
- If the request is not successful, the promise fails with an `err`.
1280
-
1281
- |err.response.data.status_code|err.response.data.errors|How to fix|
1282
- |:---|:---|:---|
1283
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Error: Your system clock must be accurate to within 30 seconds"`<br>`}]`|Check your system clock|
1284
- |`403`|`[{`<br>`"error": "AuthError",`<br>`"message": "Invalid token: API key not found"`<br>`}]`|Use the correct API key. Refer to [API keys](#api-keys) for more information|