Sign inGet Started

Rust SDK

Send email with Poem

Use Parcel Wing from Poem handlers and shared data.

Install the SDK

The official Rust SDK is published as parcelwing.

Terminal

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

Poem handler

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

src/main.rs

use parcelwing::{Client, EmailSendRequest};
use poem::{handler, listener::TcpListener, post, web::{Data, Json}, Route, Server};
use serde::{Deserialize, Serialize};
 
#[derive(Deserialize)]
struct WelcomeRequest {
email: String,
first_name: Option<String>,
}
 
#[derive(Serialize)]
struct SendResponse {
id: String,
}
 
#[handler]
async fn send_welcome(
Data(client): Data<&Client>,
Json(payload): Json<WelcomeRequest>,
) -> poem::Result<Json<SendResponse>> {
let emails = 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
.map_err(poem::Error::from_string)?;
 
Ok(Json(SendResponse { id: emails[0].id.clone() }))
}
 
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let client = Client::new(std::env::var("PARCELWING_API_KEY").unwrap()).unwrap();
let app = Route::new().at("/send-welcome", post(send_welcome)).data(client);
 
Server::new(TcpListener::bind("0.0.0.0:8080")).run(app).await
}

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.