Rust SDK
Send email with Warp
Use Parcel Wing from Warp filters and async handlers.
Install the SDK
The official Rust SDK is published as parcelwing.
Terminal
cargo add parcelwingcargo add tokio --features macros,rt-multi-thread
Warp filter
Create the Parcel Wing client during server startup, then call it from trusted async handlers.
src/main.rs
use parcelwing::{Client, EmailSendRequest};use serde::Deserialize;use std::convert::Infallible;use warp::Filter;#[derive(Clone, Deserialize)]struct WelcomeRequest {email: String,first_name: Option<String>,}#[tokio::main]async fn main() -> Result<(), parcelwing::Error> {let client = Client::new(std::env::var("PARCELWING_API_KEY").unwrap())?;let client_filter = warp::any().map(move || client.clone());let send_welcome = warp::path("send-welcome").and(warp::post()).and(client_filter).and(warp::body::json()).and_then(send_welcome);warp::serve(send_welcome).run(([0, 0, 0, 0], 8080)).await;Ok(())}async fn send_welcome(client: Client, payload: WelcomeRequest) -> Result<impl warp::Reply, Infallible> {let response = match client.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{Ok(emails) => serde_json::json!({ "id": emails[0].id }),Err(error) => serde_json::json!({ "error": error.to_string() }),};Ok(warp::reply::json(&response))}
Production notes
- Keep
PARCELWING_API_KEYin server-side environment variables. - Build one SDK client during server startup and reuse it from trusted handlers.
- Log
parcelwing::Error::Apidetails for support and debugging.