# Flask

> Send your first email from a Flask route by calling the SendPing API with requests.

Python has an official package — `pip install sendping` — and it works unchanged from a Flask view. The example below instead POSTs JSON to `https://www.sendping.co/api/emails` with the [`requests`](https://pypi.org/project/requests/) library, keeping the dependency list short. See [SDKs](https://www.sendping.co/docs/resources/sdks) for the package.

## Prerequisites

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

## 1. Install an HTTP client

**Pip**

```bash
pip install requests
```

## 2. Set your API key

**.env**

```ini
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 3. Send email from a route

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

**index.py**

```py
import os
import requests
from flask import Flask, jsonify

app = Flask(__name__)


@app.route("/")
def index():
    res = requests.post(
        "https://www.sendping.co/api/emails",
        headers={"Authorization": f"Bearer {os.environ['SENDPING_API_KEY']}"},
        json={
            "from": "Acme <onboarding@yourdomain.com>",
            "to": ["delivered@test.sendping.co"],
            "subject": "hello world",
            "html": "<strong>it works!</strong>",
        },
    )
    res.raise_for_status()
    return jsonify(res.json())


if __name__ == "__main__":
    app.run()
```

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