# FastAPI

> Send your first email from a FastAPI endpoint by calling the MailBlastr API with httpx.

Send your first email from a FastAPI app by calling the MailBlastr REST API directly. The `mailblastr` SDK is for Node.js — Python users POST JSON to `https://api.mailblastr.com/emails` using the async [`httpx`](https://www.python-httpx.org/) client, which pairs naturally with FastAPI.

## Prerequisites

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.

## 1. Install an HTTP client

**Pip**

```bash
pip install httpx
```

## 2. Set your API key

**.env**

```ini
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Send email from an endpoint

POST the email body to the MailBlastr API from a route handler. The easiest way to send is with the `html` field.

**main.py**

```py
import os
import httpx
from fastapi import FastAPI

app = FastAPI()


@app.post("/")
async def send_mail():
    async with httpx.AsyncClient() as client:
        res = await client.post(
            "https://api.mailblastr.com/emails",
            headers={"Authorization": f"Bearer {os.environ['MAILBLASTR_API_KEY']}"},
            json={
                "from": "Acme <onboarding@yourdomain.com>",
                "to": ["delivered@example.com"],
                "subject": "hello world",
                "html": "<strong>it works!</strong>",
            },
        )
    res.raise_for_status()
    return res.json()
```

> **Note:** A successful call returns the created email's `id`. See every supported field in the [Send an email](https://www.mailblastr.com/docs/api/emails-send) reference.
