How to Use Notify with Python
Send email from Python with the requests library or urllib.
Send email from Python with a single HTTP call. Authenticate with the x-api-key header.
Prerequisites
- API key — generated when you sign up
- Verified domain (optional) — required for production sends from a custom
fromaddress - Python 3.9+ recommended
Send an email with requests
import os
import requests
def send_email():
response = requests.post(
"https://notify.cx/api/email/send",
headers={
"Content-Type": "application/json",
"x-api-key": os.environ["NOTIFY_API_KEY"],
},
json={
"to": "john@example.com",
"subject": "Hello world",
"message": "<h1>Welcome!</h1><p>Thanks for joining us.</p>",
},
timeout=30,
)
response.raise_for_status()
print(response.json())
if __name__ == "__main__":
send_email()
Install and run:
pip install requests
NOTIFY_API_KEY=your_key python send_email.py
The message field accepts plain text or HTML. See Sending Emails for both formats.
Send with the standard library (urllib)
No third-party packages required:
import json
import os
import urllib.error
import urllib.request
def send_email():
payload = json.dumps({
"to": "john@example.com",
"subject": "Hello world",
"message": "<h1>Welcome!</h1><p>Thanks for joining us.</p>",
}).encode("utf-8")
request = urllib.request.Request(
"https://notify.cx/api/email/send",
data=payload,
method="POST",
headers={
"Content-Type": "application/json",
"x-api-key": os.environ["NOTIFY_API_KEY"],
},
)
try:
with urllib.request.urlopen(request, timeout=30) as response:
print(json.loads(response.read().decode("utf-8")))
except urllib.error.HTTPError as exc:
body = exc.read().decode("utf-8")
raise RuntimeError(f"Notify error ({exc.code}): {body}") from exc
if __name__ == "__main__":
send_email()
Optional from address
After you verify a domain, pass from:
json={
"from": "noreply@your-verified-domain.com",
"to": "john@example.com",
"subject": "Hello world",
"message": "Thanks for joining us.",
}