This Node.js script lets you send test emails using the GOV.UK Notify API by generating the required JWT authentication token and making a direct API call.
- Loads configuration from a
.envfile - Generates a JWT for GOV.UK Notify authentication
- Sends a real email (using your template and credentials) via the Notify API
- Prints all key variables and the API response for debugging
- Node.js (version 14 or higher recommended)
- A GOV.UK Notify account with:
- A valid API key
- A template ID for the email you want to send
- The recipient email address (should be on your team or guest list if using a team/guest key)
git clone https://github.com/your-username/govuk-notify-node-test.git
cd govuk-notify-node-testnpm installCreate a file named .env in the root of the project with the following contents:
GOV_NOTIFY_API_KEY=your-govuk-notify-api-key-here
GOV_NOTIFY_TEMPLATE_ID=your-template-id-here
RECIPIENT_EMAIL=recipient@example.com
VERIFY_URL=https://your-app.com/verify?token=example
RESET_URL=https://your-app.com/reset-password?token=example- Replace the values with your real GOV.UK Notify API key, template ID, and recipient email address.
- Set the
VERIFY_URLand/orRESET_URLif your template requires them as personalisation fields.
Important:
Do not commit your .env file to public repositories. Add .env to your .gitignore.
Run the script using Node.js:
node notify-send.jsThe script will:
- Print the parsed variables and JWT token
- Print the request payload
- Send the email via GOV.UK Notify
- Print the HTTP status and response from the API
- 403 AuthError: Double-check your API key, template ID, and that the recipient email is allowed for your Notify account.
- Invalid Key Format: Ensure your API key is in the correct format:
key_name-service_id-api_secret - Missing Variables: The script will exit if any required environment variable is missing.
- Adjust the
personalisationobject innotify-send.jsto match your template’s required fields. - To add more fields, update both your
.envfile and the script accordingly.
MIT
Questions or issues?
Open an issue or contact your GOV.UK Notify administrator.
Let me know if you want this tailored for a different filename, extra troubleshooting, or more advanced usage!
Sources