GOV.UK Notify Node.js Client

8.3.2 · active · verified Tue Apr 21

The `notifications-node-client` is the official Node.js client library for interacting with the GOV.UK Notify API. It provides a programmatic interface for sending various types of government-related communications, including transactional emails, SMS messages, and physical letters. Currently at version 8.3.2, the library is actively maintained by alphagov, ensuring continuous compatibility with the GOV.UK Notify service and adherence to UK government security and accessibility standards. Releases typically align with Node.js LTS cycles or significant API updates. Its key differentiator is its direct, secure, and officially supported integration with the critical GOV.UK Notify infrastructure, making it indispensable for public sector applications in the UK that require reliable and compliant communication capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `NotifyClient` and send both an email and an SMS message using predefined templates and personalisation data. It highlights secure API key handling via environment variables and includes robust error handling for API responses.

import { NotifyClient } from 'notifications-node-client';

// Ensure you set your GOV.UK Notify API key in an environment variable
const API_KEY = process.env.GOVUK_NOTIFY_API_KEY ?? 'your-api-key';

// Instantiate the client
const notifyClient = new NotifyClient(API_KEY);

// Example: Sending an email
async function sendExampleEmail() {
  const emailAddress = 'test@example.com';
  const templateId = 'your-email-template-id'; // Replace with your actual template ID
  const personalisation = {
    name: 'John Doe',
    application_status: 'approved',
  };
  const reference = 'my-email-ref-123'; // Optional unique reference

  try {
    console.log(`Attempting to send email to ${emailAddress} using template ${templateId}...`);
    const response = await notifyClient.sendEmail(
      templateId,
      emailAddress,
      personalisation,
      reference
    );
    console.log('Email sent successfully!');
    console.log('Response:', JSON.stringify(response.data, null, 2));
  } catch (error: any) {
    console.error('Failed to send email:');
    // GOV.UK Notify errors often have a 'response.data.errors' structure
    if (error.response && error.response.data && error.response.data.errors) {
      console.error('API Error Details:', JSON.stringify(error.response.data.errors, null, 2));
    } else {
      console.error(error.message || error);
    }
    process.exit(1);
  }
}

// Example: Sending an SMS
async function sendExampleSms() {
  const phoneNumber = '+447900900123'; // Replace with a valid UK phone number
  const templateId = 'your-sms-template-id'; // Replace with your actual template ID
  const personalisation = {
    verification_code: '123456',
  };
  const reference = 'my-sms-ref-456'; // Optional unique reference

  try {
    console.log(`Attempting to send SMS to ${phoneNumber} using template ${templateId}...`);
    const response = await notifyClient.sendSms(
      templateId,
      phoneNumber,
      personalisation,
      reference
    );
    console.log('SMS sent successfully!');
    console.log('Response:', JSON.stringify(response.data, null, 2));
  } catch (error: any) {
    console.error('Failed to send SMS:');
    if (error.response && error.response.data && error.response.data.errors) {
      console.error('API Error Details:', JSON.stringify(error.response.data.errors, null, 2));
    } else {
      console.error(error.message || error);
    }
    process.exit(1);
  }
}

// Run the examples (uncomment to execute)
// sendExampleEmail();
// sendExampleSms();

view raw JSON →