Python · Django
Send your first email from a Django view using the Bird Python SDK's sync client. Three steps: install, send, run.
1. Install the SDK
कोड उदाहरण
mkdir bird-django-quickstart
cd bird-django-quickstart
python -m pip install messagebird-sdk django
django-admin startproject config .
python manage.py startapp myappRequires 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:
कोड उदाहरण
export BIRD_API_KEY="bk_us1_..."Add a view in myapp/views.py. Construct one Bird at module load and reuse it across requests: it pools connections and is safe to share across threads.
कोड उदाहरण
from bird import Bird, APIError
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
client = Bird()
@csrf_exempt
@require_POST
def send_email(request):
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 JsonResponse({"error": str(err)}, status=502)
return JsonResponse({"id": message.id, "status": message.status}, status=202)The CSRF exemption keeps this local cURL example runnable. Add authentication and an appropriate CSRF policy before exposing the route.
Wire it up in config/urls.py:
कोड उदाहरण
from django.urls import path
from myapp.views import send_email
urlpatterns = [path("send/", send_email)]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.
The SDK retries transient failures automatically and reuses the call's idempotency key on each attempt. This prevents a retry from sending the message twice.
3. Try it
कोड उदाहरण
python manage.py runserver
curl -X POST http://localhost:8000/send/Bird accepts the message with a 202 and delivers it asynchronously; your view returns the em_ ID and status:
कोड उदाहरण
{
"id": "em_01ky7ma8y2es1s2akzk53tmjn0",
"status": "accepted"
}Fetch the message by its em_ ID with client.email.get(...) and watch the status move from accepted to delivered.
Next steps
- Python SDK email reference: every method and option.
- Send your first email: the same flow with curl and the full sandbox address list.
- Sending domains: verify your own domain for production sending.
- Email API reference: the full request and response schema.
Related resources
Continue with the documentation, guides and examples for this topic. Resources are in English.
Watch the guideTesting email without spamming anyoneExplore the capabilityTest email deliveryFollow the learning pathOperate messaging reliably
Try the practice and get an implementation brief