# Rust

> Send your first email from Rust using reqwest against the MailBlastr API.

MailBlastr has no Rust crate — you send email by POSTing JSON to `https://api.mailblastr.com/emails`. The example below uses the async [`reqwest`](https://crates.io/crates/reqwest) client with [Tokio](https://tokio.rs).

## 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. Create a project and add dependencies

Create a Cargo project and add an async HTTP client plus a runtime.

```bash
cargo init mailblastr-rust-example
cd mailblastr-rust-example
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 email

**src/main.rs**

```rust
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = std::env::var("MAILBLASTR_API_KEY")?;

    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?;

    let body: serde_json::Value = res.json().await?;
    println!("{}", body["id"]);

    Ok(())
}
```

> **Note:** Inside an Axum app, send this request from a handler. See [Send an email](https://www.mailblastr.com/docs/api/emails-send) for the full body schema.
