Send email with Flask
One route, one call, nothing else in the way.
You need a verified sending domain and an API key from Settings › API Keys. If neither exists yet, the quickstart covers both in a couple of minutes.
Install
pip install sending-sdk flaskSend
import os
from flask import Flask, request, jsonify
from sending import Sending
app = Flask(__name__)
client = Sending(api_key=os.environ["SENDING_API_KEY"])
@app.post("/signup")
def signup():
data = request.get_json()
res = client.emails.send({
"from": "Acme <[email protected]>",
"to": data["email"],
"subject": "Welcome",
"html": "<p>You're in.</p>",
"idempotencyKey": f"welcome-{data['user_id']}",
})
return jsonify(res), 202A successful call answers 202 with the message id: accepted and queued, not yet delivered. Delivery, opens, bounces and complaints arrive later, on webhooks or in the dashboard.
What changes in Flask
Under Gunicorn each worker builds its own client at import time, which is what you want: the object is cheap and holds no shared state.
The thing that catches people
For webhooks read request.get_data() rather than request.json: the signature covers the exact bytes we sent, and reparsing changes them.
Next
- All the fields: cc and bcc, reply-to, attachments, templates, scheduling.
- Webhooks: delivery, bounce and complaint events, signed with HMAC.
- Domains: SPF, DKIM and MAIL FROM, and why sending is refused until they are in place.
- MCP: the same operations as tools, when the one writing the code is an agent.