Sign inGet Started

Rust SDK

Send email with Tide

Use Parcel Wing from Tide routes with 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

Tide route

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, Serialize};
 
#[derive(Clone)]
struct AppState {
parcelwing: Client,
}
 
#[derive(Deserialize)]
struct WelcomeRequest {
email: String,
first_name: Option<String>,
}
 
#[derive(Serialize)]
struct SendResponse {
id: String,
}
 
#[async_std::main]
async fn main() -> tide::Result<()> {
let client = Client::new(std::env::var("PARCELWING_API_KEY").unwrap()).unwrap();
let mut app = tide::with_state(AppState { parcelwing: client });
 
app.at("/send-welcome").post(|mut req: tide::Request<AppState>| async move {
let payload: WelcomeRequest = req.body_json().await?;
let emails = req
.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| tide::Error::new(502, error))?;
 
Ok(tide::Body::from_json(&SendResponse { id: emails[0].id.clone() })?)
});
 
app.listen("0.0.0.0:8080").await?;
Ok(())
}

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.