Prerequisites
Use a verified sending domain, your account ID, and a domain-scoped send-only API v2 key. Use sandbox mode for every test. Management tasks need a separate full API key. Use Python 3.13, Azure Functions Core Tools v4 and the Python v2 programming model.Install the Dependencies
Try a Sandbox Send
SetAHASEND_API_KEY, AHASEND_ACCOUNT_ID and AHASEND_FROM in your shell or local secret store. Use a send-only key and an address on your verified domain. Save this as quick_send.py:
quick_send.py
python quick_send.py in the activated environment. It prints acceptance statuses and exits with an error if sending fails. Sandbox acceptance does not deliver mail. The production version below adds input checks and a stable request key.
Add the Shared Sending Code
Save this assend_email.py beside the function file. It uses Python’s standard library, so this example needs no SDK package. Set AHASEND_API_KEY, AHASEND_ACCOUNT_ID, AHASEND_FROM, AHASEND_TO and SEND_TOKEN in the runtime environment. Use a long random SEND_TOKEN for requests from your trusted server. For the first run set AHASEND_TO=recipient@example.com and AHASEND_DELIVERY_MODE=sandbox; keep a sandbox credential in place while testing. Set live only when ready to deliver to your own approved recipients.
send_email.py
event_id; a new business event needs a new ID. See idempotency for time limits and uncertain outcomes. Record an internal event ID on failures without logging API keys or message bodies. Add request limits at the hosting layer.
Create the HTTP Function
Save this asfunction_app.py:
function_app.py
host.json:
host.json
Run with Core Tools
Activate.venv, export the variables from the shared code, and run:
.venv and local.settings.json out of Git. In another shell with SEND_TOKEN set, run:
Prepare the Hosted App
Select Python 3.13 on a supported Linux Functions plan. Includerequirements.txt, host.json, function_app.py and send_email.py in the project. Keep secrets in Key Vault references and ordinary configuration in app settings. Add x-functions-key to hosted requests alongside the bearer token. Follow Microsoft’s Python deployment guidance and enforce your application’s permissions and request limits.
Track final delivery with webhooks. Also see Azure in Node.js, Azure in Go and Python SMTP.
Related Guides
- Before sending: verify a domain and create a send-only key.
- Other ways to send: REST API, SMTP, CLI quickstart, Node.js SDK and Go SDK.
- Request rules: API authentication, scopes, idempotency, errors and rate limits.
- Testing and events: sandbox mode, CLI webhook testing, event payloads, signature verification and delivery retries.
- Data and limits: retention, tracking and plans and feature availability.

