# Rust

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

SendPing ships an official Rust crate (`cargo add sendping`) — see [Send emails with Rust](https://www.sendping.co/docs/integrations/rust) for the typed, async client — but you do not need it: this guide sends email by POSTing JSON to `https://www.sendping.co/api/emails`. The example below uses the async [`reqwest`](https://crates.io/crates/reqwest) client with [Tokio](https://tokio.rs).

## 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.
- An async runtime — Tokio below; any executor that can drive `reqwest` futures works.
- Rust 1.75 or newer if you add the official `sendping` crate, which declares that as its `rust-version`.

## 1. Create a project and add dependencies

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

```bash
cargo init sendping-rust-example
cd sendping-rust-example
cargo add reqwest -F json
cargo add tokio -F macros,rt-multi-thread
cargo add serde_json
```

## 2. Set your API key

```bash
export SENDPING_API_KEY=mb_xxxxxxxxx
```

> **Note:** Rust does not read `.env` files on its own, and the example below calls `std::env::var` directly. Export the variable in your shell, or add [`dotenvy`](https://crates.io/crates/dotenvy) (`cargo add dotenvy`) and call `dotenvy::dotenv().ok();` as the first line of `main`.

## 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("SENDPING_API_KEY")?;

    let res = reqwest::Client::new()
        .post("https://www.sendping.co/api/emails")
        .bearer_auth(api_key)
        // REQUIRED: reqwest sends no User-Agent of its own, and the API answers
        // a request without one with 403 validation_error before it authenticates.
        .header("User-Agent", "my-app/1.0")
        .json(&json!({
            "from": "Acme <onboarding@yourdomain.com>",
            "to": ["delivered@test.sendping.co"],
            "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.sendping.co/docs/api/emails-send) for the full body schema.
