Sign inGet Started

Rust SDK

Send email with Axum

Use Parcel Wing from Axum handlers with shared application state.

Install the SDK

The official Rust SDK is published as parcelwing.

Terminal

cargo add parcelwing
cargo add tokio --features macros,rt-multi-thread

Axum handler

Create the Parcel Wing client during server startup, then call it from trusted async handlers.

src/main.rs

use axum::{extract::State, routing::post, Json, Router};
use parcelwing::{Client, EmailSendRequest};
use serde::Deserialize;
use serde_json::{json, Value};
 
#[derive(Clone)]
struct AppState {
parcelwing: Client,
}
 
#[derive(Deserialize)]
struct WelcomeRequest {
email: String,
first_name: Option<String>,
}
 
#[tokio::main]
async fn main() -> Result<(), parcelwing::Error> {
let state = AppState {
parcelwing: Client::new(std::env::var("PARCELWING_API_KEY").unwrap())?,
};
 
let app = Router::new()
.route("/send-welcome", post(send_welcome))
.with_state(state);
 
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();
Ok(())
}
 
async fn send_welcome(
State(state): State<AppState>,
Json(payload): Json<WelcomeRequest>,
) -> Result<Json<Value>, String> {
let emails = state
.parcelwing
.emails()
.send(
EmailSendRequest::new(std::env::var("PARCELWING_FROM_EMAIL").unwrap(), payload.email)
.template_alias("welcome")
.template_param("first_name", payload.first_name.unwrap_or_else(|| "friend".to_string())),
)
.await
.map_err(|error| error.to_string())?;
 
Ok(Json(json!({ "id": emails[0].id })))
}

Production notes

  • Keep PARCELWING_API_KEY in server-side environment variables.
  • Build one SDK client during server startup and reuse it from trusted handlers.
  • Log parcelwing::Error::Api details for support and debugging.