{"library":"notifications-node-client","title":"GOV.UK Notify Node.js Client","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install notifications-node-client"],"cli":null},"imports":["import { NotifyClient } from 'notifications-node-client';","const { NotifyClient } = require('notifications-node-client');","import type { NotificationClientOptions } from 'notifications-node-client';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { NotifyClient } from 'notifications-node-client';\n\n// Ensure you set your GOV.UK Notify API key in an environment variable\nconst API_KEY = process.env.GOVUK_NOTIFY_API_KEY ?? 'your-api-key';\n\n// Instantiate the client\nconst notifyClient = new NotifyClient(API_KEY);\n\n// Example: Sending an email\nasync function sendExampleEmail() {\n  const emailAddress = 'test@example.com';\n  const templateId = 'your-email-template-id'; // Replace with your actual template ID\n  const personalisation = {\n    name: 'John Doe',\n    application_status: 'approved',\n  };\n  const reference = 'my-email-ref-123'; // Optional unique reference\n\n  try {\n    console.log(`Attempting to send email to ${emailAddress} using template ${templateId}...`);\n    const response = await notifyClient.sendEmail(\n      templateId,\n      emailAddress,\n      personalisation,\n      reference\n    );\n    console.log('Email sent successfully!');\n    console.log('Response:', JSON.stringify(response.data, null, 2));\n  } catch (error: any) {\n    console.error('Failed to send email:');\n    // GOV.UK Notify errors often have a 'response.data.errors' structure\n    if (error.response && error.response.data && error.response.data.errors) {\n      console.error('API Error Details:', JSON.stringify(error.response.data.errors, null, 2));\n    } else {\n      console.error(error.message || error);\n    }\n    process.exit(1);\n  }\n}\n\n// Example: Sending an SMS\nasync function sendExampleSms() {\n  const phoneNumber = '+447900900123'; // Replace with a valid UK phone number\n  const templateId = 'your-sms-template-id'; // Replace with your actual template ID\n  const personalisation = {\n    verification_code: '123456',\n  };\n  const reference = 'my-sms-ref-456'; // Optional unique reference\n\n  try {\n    console.log(`Attempting to send SMS to ${phoneNumber} using template ${templateId}...`);\n    const response = await notifyClient.sendSms(\n      templateId,\n      phoneNumber,\n      personalisation,\n      reference\n    );\n    console.log('SMS sent successfully!');\n    console.log('Response:', JSON.stringify(response.data, null, 2));\n  } catch (error: any) {\n    console.error('Failed to send SMS:');\n    if (error.response && error.response.data && error.response.data.errors) {\n      console.error('API Error Details:', JSON.stringify(error.response.data.errors, null, 2));\n    } else {\n      console.error(error.message || error);\n    }\n    process.exit(1);\n  }\n}\n\n// Run the examples (uncomment to execute)\n// sendExampleEmail();\n// sendExampleSms();\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}