Integration

Send emails from Django with Postkit

Send transactional emails from Django views or management commands using Postkit's REST API. No Django email backend needed — just HTTP.

1. Set your API key

pip install httpx
# settings.py or .env
POSTKIT_API_KEY = "pk_live_..."

2. Send an email

python
# views.py
import httpx
from django.conf import settings
from django.http import JsonResponse

def send_welcome(request):
    data = json.loads(request.body)

    response = httpx.post(
        "https://api.postkit.eu/v1/emails",
        headers={
            "Authorization": f"Bearer {settings.POSTKIT_API_KEY}",
            "Content-Type": "application/json",
        },
        json={
            "from": "hello@yourapp.eu",
            "to": data["email"],
            "subject": "Welcome aboard!",
            "html": f"<h1>Welcome, {data['name']}!</h1>",
        },
    )

    return JsonResponse(response.json())

3. Handle webhooks

Postkit sends delivery events (sent, delivered, bounced, opened, clicked) via HMAC-SHA256 signed webhooks following the Standard Webhooks specification.

python
# views.py
import hashlib, hmac, base64, json
from django.conf import settings
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def postkit_webhook(request):
    body = request.body.decode()
    msg_id = request.headers.get("webhook-id", "")
    timestamp = request.headers.get("webhook-timestamp", "")
    signature = request.headers.get("webhook-signature", "")

    secret_b64 = settings.POSTKIT_WEBHOOK_SECRET.replace("whsec_", "")
    secret = base64.b64decode(secret_b64)
    content = f"{msg_id}.{timestamp}.{body}"
    expected = base64.b64encode(
        hmac.new(secret, content.encode(), hashlib.sha256).digest()
    ).decode()

    valid = any(
        hmac.compare_digest(s.replace("v1,", ""), expected)
        for s in signature.split(" ")
    )

    if not valid:
        return JsonResponse({"error": "Invalid signature"}, status=401)

    event = json.loads(body)
    print(f"Postkit event: {event['type']}")
    return JsonResponse({"received": True})

Start sending in under 5 minutes

Free plan. No credit card required.

Get Started Free