GOV.UK Notify Python Client
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
-
403 Forbidden: Not a valid API key. Check the key you are using is valid.
cause Incorrect API key, a revoked key, or using an old-format key with a newer client version.fixVerify the API key is correct and active in the GOV.UK Notify admin interface. If your key is old, generate a new one. Ensure no whitespace or typos. -
403 Forbidden: Not authorised to send to this recipient (email address, phone number)
cause Attempting to send to an email/phone number not pre-approved on the service (e.g., not a team member) when using a 'team-only' API key, or your service is in trial mode and hasn't been approved for live sending to arbitrary recipients.fixUse a 'live' API key (if your service is approved) or ensure the recipient is added to your team members in GOV.UK Notify for testing with a 'team-only' key. Verify your service status in Notify admin. -
400 Bad Request: Template not found
cause The `template_id` provided does not exist or is not associated with the service using the provided API key.fixDouble-check the `template_id` in your GOV.UK Notify account and ensure it belongs to the correct service that the API key is linked to.
Warnings
- breaking GOV.UK Notify API keys issued after May 2018 use a simpler format (UUID only). Older keys (prefixed with service ID) require `notifications-python-client` versions older than `6.0` or conversion to the new format. Using an old-format key with recent client versions will result in authentication failures.
- breaking Version 10.0.0 removed the explicit `requests` dependency pinning and made changes to how `base_url` is handled, especially regarding default values. This might impact environments where `requests` versions are tightly controlled or custom `base_url` configurations were subtly relied upon.
- gotcha By default, the client points to the production GOV.UK Notify API. For testing or staging environments, you must explicitly set the `base_url` parameter during client initialization.
Install
-
pip install notifications-python-client
Imports
- NotificationsAPIClient
from notifications_python_client.notifications import NotificationsAPIClient
Quickstart
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}")