GOV.UK Notify Python Client

10.0.1 · active · verified Fri Apr 17

The `notifications-python-client` is the official Python API client for GOV.UK Notify, a service that allows government departments to send emails, SMS messages, and letters to their users. It is actively maintained by the Government Digital Service (GDS), with version 10.0.1 being the latest stable release, reflecting ongoing development and adherence to GOV.UK standards.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates sending an email notification using the GOV.UK Notify client, including how to handle API keys from environment variables and basic error handling. Remember to replace `YOUR_EMAIL_TEMPLATE_ID` with a valid ID from your Notify service.

import os
from notifications_python_client.notifications import NotificationsAPIClient

# Ensure these are set as environment variables or retrieve securely
# NOTIFY_API_KEY should be your service's API key (UUID format)
api_key = os.environ.get('NOTIFY_API_KEY')

if not api_key:
    raise ValueError("NOTIFY_API_KEY environment variable not set.")

# For staging/test environments, you MUST specify the base_url:
# client = NotificationsAPIClient(api_key=api_key, base_url='https://api.staging.notifications.service.gov.uk')
# For production (default):
client = NotificationsAPIClient(api_key=api_key)

try:
    response = client.send_email_notification(
        email_address='test@example.com',
        template_id='YOUR_EMAIL_TEMPLATE_ID', # Replace with a valid template ID from your Notify account
        personalisation={
            'name': 'Test User',
            'application_status': 'approved'
        },
        reference='my_app_ref_123' # Optional reference for your records
    )
    print("Email sent successfully:")
    print(response)
except Exception as e:
    print(f"Error sending email: {e}")

view raw JSON →