Documentation
Sign inGet started

Python · Flask

Send your first email from a Flask route using the Bird Python SDK's sync client. Three steps: install, send, run.

1. Install the SDK

Contoh kode
pip install bird-sdk flask
Contoh kode
# or
uv add bird-sdk flask
poetry add bird-sdk flask
Requires Python 3.10+. The import package is bird.

2. Send an email

Export your API key — the SDK reads BIRD_API_KEY from the environment and infers the region (us1 or eu1) from the bk_us1_ / bk_eu1_ prefix, so Bird() needs no arguments:
Contoh kode
export BIRD_API_KEY="bk_us1_..."
Create app.py. Construct one Bird at module load and reuse it across requests — it pools connections and is safe to share across threads:
Contoh kode
from bird import Bird, APIError
from flask import Flask, jsonify

app = Flask(__name__)
client = Bird()


@app.post("/send")
def send():
    try:
        message = client.email.send(
            from_="onboarding@messagebird.dev",
            to=["delivered@messagebird.dev"],
            subject="Hello from Bird",
            html="<p>My first Bird email.</p>",
        )
    except APIError as err:
        return jsonify({"error": str(err)}), 502
    return jsonify({"id": message.id, "status": message.status}), 202
from_ is the Python spelling of the from field (from is a reserved word). onboarding@messagebird.dev is Bird's shared onboarding sender — no domain verification needed — and delivered@messagebird.dev is a sandbox recipient that always delivers.
Transient failures retry automatically, and a retried send reuses one idempotency key across attempts, so it never double-applies.

3. Try it

Contoh kode
flask run
curl -X POST http://localhost:5000/send
Bird accepts the message with a 202 and delivers it asynchronously; your route returns the em_ id and status:
Contoh kode
{
  "id": "em_019c1930687b7bfa8a1b2c3d4e5f6789",
  "status": "accepted"
}
Fetch the message by its em_ id with client.email.get(...) and watch the status move from accepted to delivered.

Next steps