# Axum

> Send your first email from an Axum handler using reqwest against the MailBlastr API.

The `mailblastr` SDK is for Node.js — Rust users send email by POSTing JSON to `https://api.mailblastr.com/emails`. The example below builds a small [Axum](https://github.com/tokio-rs/axum) app whose handler calls the API with the async [`reqwest`](https://crates.io/crates/reqwest) client on the [Tokio](https://tokio.rs) runtime.

## 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 dependencies

Add Axum, an async HTTP client, a runtime, and JSON support to your Cargo project.

```bash
cargo add axum
cargo add reqwest -F json
cargo add tokio -F macros,rt-multi-thread
cargo add serde_json
```

## 2. Set your API key

**.env**

```toml
MAILBLASTR_API_KEY=mb_xxxxxxxxx
```

## 3. Send an email

Build an Axum router with a single route whose handler POSTs the email and returns the created `id`.

**src/main.rs**

```rust
use axum::{http::StatusCode, routing::get, Router};
use serde_json::json;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(endpoint));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn endpoint() -> Result<String, StatusCode> {
    let api_key = std::env::var("MAILBLASTR_API_KEY")
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    let res = reqwest::Client::new()
        .post("https://api.mailblastr.com/emails")
        .bearer_auth(api_key)
        .json(&json!({
            "from": "Acme <onboarding@yourdomain.com>",
            "to": ["delivered@example.com"],
            "subject": "Hello World",
            "html": "<strong>It works!</strong>",
        }))
        .send()
        .await
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    let body: serde_json::Value = res
        .json()
        .await
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    Ok(body["id"].to_string())
}
```

Opening your browser at `http://localhost:3000` (or running `curl localhost:3000`) sends an email and returns its `id`.

> **Note:** See [Send an email](https://www.mailblastr.com/docs/api/emails-send) for the full body schema — `cc`, `bcc`, `reply_to`, `attachments`, and more.
