Skip to content

malmas18/notify-client-node

 
 

Repository files navigation

Notify Node.js client

This documentation is for developers interested in using a Node.js client to integrate with Notify.

Table of Contents

Installation

npm install --save @govau-platforms/notify-client

Getting started

Typescript:

import { Client } from "@govau-platforms/notify-client";
const notifyClient = new Client({ apiKey: "xxxxx" });

Javascript:

const { Client } = require("@govau-platforms/notify-client");
const notifyClient = new Client({ apiKey: "xxxxx" });

Generate an API key by logging in to Notify.gov.au and going to the API integration page.

Connect through a proxy (optional)

notifyClient.setProxy(proxyUrl);

Send messages

Text message

Method

Click here to expand for more information.
notifyClient
  .sendSms(templateId, phoneNumber, {
    personalisation: personalisation,
    reference: reference,
    smsSenderId: smsSenderId
  })
  .then(response => console.log(response))
  .catch(err => console.error(err));

Response

If the request is successful, response will be an object.

Click here to expand for more information.
{
    "id": "bfb50d92-100d-4b8b-b559-14fa3b091cda",
    "reference": null,
    "content": {
        "body": "Some words",
        "from_number": "40604"
    },
    "uri": "https://rest-api.notify.gov.au/v2/notifications/ceb50d92-100d-4b8b-b559-14fa3b091cd",
    "template": {
        "id": "ceb50d92-100d-4b8b-b559-14fa3b091cda",
       "version": 1,
       "uri": "https://rest-api.notify.gov.au/v2/templates/bfb50d92-100d-4b8b-b559-14fa3b091cda"
    }
}

Otherwise the client will return an error err:

err.error.status_code err.error.errors
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM of 10 requests per 10 seconds"
}]
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (50) for today"
}]
400 [{
"error": "BadRequestError",
"message": "Can"t send to this recipient using a team-only API key"
]}
400 [{
"error": "BadRequestError",
"message": "Can"t send to this recipient when service is in trial mode - see https://notify.gov.au/trial-mode"
}]

Arguments

Click here to expand for more information.
phoneNumber

The phone number of the recipient, only required for sms notifications.

templateId

Find by clicking API info for the template you want to send.

options
reference

An optional identifier you generate. The reference can be used as a unique reference for the notification. Because Notify does not require this reference to be unique you could also use this reference to identify a batch or group of notifications.

You can omit this argument if you do not require a reference for the notification.

personalisation

If a template has placeholders, you need to provide their values, for example:

personalisation = {
  first_name: "Amala",
  reference_number: "300241"
};

This does not need to be provided if your template does not contain placeholders.

smsSenderId

Optional. Specifies the identifier of the sms sender to set for the notification. The identifiers are found in your service Settings, when you 'Manage' your 'Text message sender'.

If you omit this argument your default sms sender will be set for the notification.

Example usage with optional reference -

statusCallbackUrl

Optional. Specifies the identifier of the HTTPS URL for delivery status updates to be sent to.

statusCallbackBearerToken

Optional. Specifies the identifier of the Bearer token that will be used for authentication to the delivery status callback URL. This must be provided if the status callback URL is provided.

Email

Method

Click here to expand for more information.
notifyClient
  .sendEmail(templateId, emailAddress, {
    personalisation: personalisation,
    reference: reference,
    emailReplyToId: emailReplyToId
  })
  .then(response => console.log(response))
  .catch(err => console.error(err));

Response

If the request is successful, response will be an object.

Click here to expand for more information.
{
    "id": "bfb50d92-100d-4b8b-b559-14fa3b091cda",
    "reference": null,
    "content": {
        "subject": "Licence renewal",
        "body": "Dear Bill, your licence is due for renewal on 3 January 2016.",
        "from_email": "[email protected]"
    },
    "uri": "https://rest-api.notify.gov.au/v2/notifications/ceb50d92-100d-4b8b-b559-14fa3b091cd",
    "template": {
        "id": "ceb50d92-100d-4b8b-b559-14fa3b091cda",
        "version": 1,
        "uri": "https://rest-api.notify.gov.au/service/your_service_id/templates/bfb50d92-100d-4b8b-b559-14fa3b091cda"
    }
}

Otherwise the client will return an error error object:

status_code errors
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM of 10 requests per 10 seconds"
}]
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (50) for today"
}]
400 [{
"error": "BadRequestError",
"message": "Can"t send to this recipient using a team-only API key"
]}
400 [{
"error": "BadRequestError",
"message": "Can"t send to this recipient when service is in trial mode - see https://notify.gov.au/trial-mode"
}]

Arguments

Click here to expand for more information.
emailAddress

The email address of the recipient, only required for email notifications.

templateId

Find by clicking API info for the template you want to send.

options
reference

An optional identifier you generate. The reference can be used as a unique reference for the notification. Because Notify does not require this reference to be unique you could also use this reference to identify a batch or group of notifications.

You can omit this argument if you do not require a reference for the notification.

emailReplyToId

Optional. Specifies the identifier of the email reply-to address to set for the notification. The identifiers are found in your service Settings, when you 'Manage' your 'Email reply to addresses'.

If you omit this argument your default email reply-to address will be set for the notification.

personalisation

If a template has placeholders, you need to provide their values, for example:

personalisation = {
  first_name: "Amala",
  application_number: "300241"
};
emailReplyToId

Optional. Specifies the identifier of the email reply-to address to set for the notification. The identifiers are found in your service Settings, when you 'Manage' your 'Email reply to addresses'. If you omit this argument your default email reply-to address will be set for the notification.

statusCallbackUrl

Optional. Specifies the identifier of the HTTPS URL for delivery status updates to be sent to.

statusCallbackBearerToken

Optional. Specifies the identifier of the Bearer token that will be used for authentication to the delivery status callback URL. This must be provided if the status callback URL is provided.

Send a document by email

Send files without the need for email attachments.

Click here to expand for more information.

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

Contact the GOV.AU Notify team to enable this function for your service.

Add a placeholder field to the template

In Notify, use double brackets to add a placeholder field to the email template. For example:

"Download your document at: ((link_to_document))"

Upload your document

The document you upload must be a PDF file smaller than 2MB.

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

Click here to expand for more information.
var fs = require("fs");

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

Response

If the request to the client is successful, the client returns a response object, with a following body attribute:

Click here to expand for more information.
{
  "id": "740e5834-3a29-46b4-9a6f-16142fde533a",
  "reference": "STRING",
  "content": {
    "subject": "SUBJECT TEXT",
    "body": "MESSAGE TEXT",
    "from_email": "SENDER EMAIL"
  },
  "uri": "https://rest-api.notify.gov.au/v2/notifications/740e5834-3a29-46b4-9a6f-16142fde533a",
  "template": {
    "id": "f33517ff-2a88-4f6e-b855-c550268ce08a",
    "version": INTEGER,
    "uri": "https://rest-api.notify.gov.au/v2/template/f33517ff-2a88-4f6e-b855-c550268ce08a"
  }
}

Error codes

If the request is not successful, the client returns an error error object:

Click here to expand for more information.
error.status_code error.message How to fix
400 [{
"error": "BadRequestError",
"message": "Can't send to this recipient using a team-only API key"
]}
Use the correct type of API key
400 [{
"error": "BadRequestError",
"message": "Can't send to this recipient when service is in trial mode - see https://notify.gov.au/trial-mode"
}]
Your service cannot send this notification in trial mode
400 [{
"error": "BadRequestError",
"message": "Unsupported document type '{}'. Supported types are: {}"
}]
The document you upload must be a PDF file
400 [{
"error": "BadRequestError",
"message": "Document didn't pass the virus scan"
}]
The document you upload must be virus free
403 [{
"error": "AuthError",
"message": "Error: Your system clock must be accurate to within 30 seconds"
}]
Check your system clock
403 [{
"error": "AuthError",
"message": "Invalid token: signature, api token not found"
}]
Use the correct type of API key
429 [{
"error": "RateLimitError",
"message": "Exceeded rate limit for key type TEAM/TEST/LIVE of 3000 requests per 60 seconds"
}]
Refer to API rate limits for more information
429 [{
"error": "TooManyRequestsError",
"message": "Exceeded send limits (LIMIT NUMBER) for today"
}]
Refer to service limits for the limit number
500 [{
"error": "Exception",
"message": "Internal server error"
}]
Notify was unable to process the request, resend your notification.
N/A [{
"error": "Exception",
"message": "Document is larger than 2MB."
}]
The file you tried to upload was above the 2MB limit. Send a file that weighs less than 2MB.

Get the status of one message

Method

Click here to expand for more information.
notifyClient
  .getNotificationById(notificationId)
  .then(response => {})
  .catch(err => {});

Response

Click here to expand for more information.

If the request is successful, response will be an object:

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

Otherwise the client will return an error error object:

status_code errors
404 [{
"error": "NoResultFound",
"message": "No result found"
}]
400 [{
"error": "ValidationError",
"message": "id is not a valid UUID"
}]

Arguments

Click here to expand for more information.
notificationId

The ID of the notification.

Get the status of all messages

Method

Click here to expand for more information.
notifyClient
  .getNotifications({
    templateType: "email",
    templateId: "xxxxx",
    status: "delivered",
    reference: "client-ref-no",
    olderThanId: "xxxxx"
  })
  .then(response => {})
  .catch(err => {});

Response

If the request is successful, response will be an object.

Click here to expand for more information.
{ "notifications":
    [{
        "id": "notify_id",
        "reference": "client reference",
        "email_address": "email address",
        "phone_number": "phone number",
        "line_1": "full name of a person or company",
        "line_2": "123 The Street",
        "line_3": "Some Area",
        "line_4": "Some Town",
        "line_5": "Some county",
        "line_6": "Something else",
        "postcode": "postcode",
        "type": "sms | email",
        "status": sending | delivered | permanent-failure | temporary-failure | technical-failure
        "template": {
            "version": 1,
          "id": 1,
          "uri": "/template/{id}/{version}"
       },
       "created_by_name": "name of the person who sent the notification if sent manually",
       "created_at": "created at",
       "sent_at": "sent to provider at",
    },
    
  ],
  "links": {
     "current": "/notifications?template_type=sms&status=delivered",
     "next": "/notifications?other_than=last_id_in_list&template_type=sms&status=delivered"
  }
}
status_code errors
400 [{
"error": "ValidationError",
"message": "bad status is not one of [created, sending, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure]"
}]
400 [{
"error": "Apple is not one of [sms, email]"
}]

Arguments

Click here to expand for more information.
templateType

If omitted, all messages are returned. Otherwise you can filter by:

  • email
  • sms
templateId

If omitted, all messages are returned. Otherwise you can filter by a template ID string.

status

email

You can filter by:

  • sending - the message is queued to be sent by the provider.
  • delivered - the message was successfully delivered.
  • failed - this will return all failure statuses permanent-failure, temporary-failure and technical-failure.
  • permanent-failure - the provider was unable to deliver message, email does not exist; remove this recipient from your list.
  • temporary-failure - the provider was unable to deliver message, email box was full; you can try to send the message again.
  • technical-failure - Notify had a technical failure; you can try to send the message again.

You can omit this argument to ignore this filter.

text message

You can filter by:

  • sending - the message is queued to be sent by the provider.
  • delivered - the message was successfully delivered.
  • failed - this will return all failure statuses permanent-failure, temporary-failure and technical-failure.
  • permanent-failure - the provider was unable to deliver message, phone number does not exist; remove this recipient from your list.
  • temporary-failure - the provider was unable to deliver message, the phone was turned off; you can try to send the message again.
  • technical-failure - Notify had a technical failure; you can try to send the message again.

You can omit this argument to ignore this filter.

reference

This is the reference you gave at the time of sending the notification. This can be omitted to ignore the filter.

olderThanId

If omitted, all messages are returned. Otherwise you can filter to retrieve all notifications older than the given notification id.

Get a template by ID

Method

Click here to expand for more information.
notifyClient
  .getTemplateById(templateId)
  .then(response => {})
  .catch(err => {});

Response

If the request is successful, response will be an object.

Click here to expand for more information.
{
    "id": "template_id",
    "name": "template name",
    "type": "sms|email",
    "created_at": "created at",
    "updated_at": "updated at",
    "version": "version",
    "created_by": "[email protected]",
    "body": "body",
    "subject": "null|email_subject"
}

Otherwise the client will return an error error object:

status_code errors
404 [{
"error": "NoResultFound",
"message": "No result found"
}]

Arguments

Click here to expand for more information.
templateId

Find by clicking API info for the template you want to send.

Get a template by ID and version

Method

Click here to expand for more information.
notifyClient
  .getTemplateByIdAndVersion(templateId, version)
  .then(response => {})
  .catch(err => {});

Response

If the request is successful, response will be an object.

Click here to expand for more information.
{
    "id": "template_id",
    "name": "template name",
    "type": "sms|email",
    "created_at": "created at",
    "updated_at": "updated at",
    "version": "version",
    "created_by": "[email protected]",
    "body": "body",
    "subject": "null|email_subject"
}

Otherwise the client will return an error error object:

status_code errors
404 [{
"error": "NoResultFound",
"No result found"
}]

Arguments

Click here to expand for more information.
templateId

Find by clicking API info for the template you want to send.

version

The version number of the template

Get all templates

Method

Click here to expand for more information.
notifyClient
  .getAllTemplates(templateType)
  .then(response => {})
  .catch(err => {});

This will return the latest version for each template.

Response

If the request is successful, response will be an object.

Click here to expand for more information.
{
    "templates" : [
        {
            "id": "template_id",
            "name": "template name",
            "type": "sms|email",
            "created_at": "created at",
            "updated_at": "updated at",
            "version": "version",
            "created_by": "[email protected]",
            "body": "body",
            "subject": "null|email_subject"
        },
        {
            ... another template
        }
    ]
}

If no templates exist for a template type or there no templates for a service, the response will be an object with an empty templates list element:

{
    "templates" : []
}

Arguments

Click here to expand for more information.
templateType

If omitted, all messages are returned. Otherwise you can filter by:

  • email
  • sms

Generate a preview template

Method

Click here to expand for more information.
personalisation = { foo: "bar" };
notifyClient
  .previewTemplateById(templateId, personalisation)
  .then(response => {})
  .catch(err => {});

Response

Click here to expand for more information.

If the request is successful, response will be an object:

{
    "id": "notify_id",
    "type": "sms|email",
    "version": "version",
    "body": "Hello bar" // with substitution values,
    "subject": "null|email_subject"
}

Otherwise the client will return an error error object:

status_code errors
400 [{
"error": "BadRequestError",
"Missing personalisation: [name]"
}]
404 [{
"error": "NoResultFound",
"No result found"
}]

Arguments

Click here to expand for more information.
templateId

Find by clicking API info for the template you want to send.

personalisation

If a template has placeholders you need to provide their values. For example:

personalisation = {
  first_name: "Amala",
  reference_number: "300241"
};

Otherwise the parameter can be omitted or undefined can be passed in its place.

Get received text messages with pagination

This will return one page of text messages (250) per call.

Method

Click here to expand for more information.
notifyClient
  .getReceivedTexts(olderThanId)
  .then(response => {})
  .catch(err => {});

Response

Click here to expand for more information.

If the request is successful, response will be a json object:

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

Arguments

Click here to expand for more information.
olderThanId

If omitted, returns 250 of the latest received text messages. Otherwise the next 250 received text messages older than the given id are returned.

Tests

There are unit and integration tests that can be run to test functionality of the client. You will need to have the relevant environment variables sourced to run the tests.

To run the unit tests:

npm test

To run the integration tests:

npm test --integration

About

Node.js client for Notify.gov.au

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 98.4%
  • Makefile 1.6%