# Python

> Send your first email from Python using requests against the MailBlastr API.

MailBlastr has no Python SDK — you send email by POSTing JSON to `https://api.mailblastr.com/emails`. The examples below use the popular [`requests`](https://pypi.org/project/requests/) library, but any HTTP client (including the stdlib `urllib`) works.

## 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 requests
```

## 2. Set your API key

Read your key from the environment so it never lives in source.

**.env**

```sh
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Send email using HTML

The easiest way to send an email is with the `html` field.

**requests**

```python
import os
import requests

res = requests.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()
print(res.json()["id"])
```

**urllib (stdlib)**

```python
import json, os, urllib.request

body = json.dumps({
    "from": "Acme <onboarding@yourdomain.com>",
    "to": ["delivered@example.com"],
    "subject": "hello world",
    "html": "<strong>it works!</strong>",
}).encode()

req = urllib.request.Request(
    "https://api.mailblastr.com/emails",
    data=body,
    headers={
        "Authorization": f"Bearer {os.environ['MAILBLASTR_API_KEY']}",
        "Content-Type": "application/json",
    },
    method="POST",
)

with urllib.request.urlopen(req) as resp:
    print(json.load(resp)["id"])
```

> **Note:** Inside Flask, FastAPI, or Django, call the API the same way from your view/route handler. See [Send an email](https://www.mailblastr.com/docs/api/emails-send) for every body field.
