Axum

Learn how to integrate Reloop with Axum.

Axum Integration

Integrate Reloop into your Rust application using the Axum web framework.

Installation

Add the official Rust SDK and standard tokio dependencies to your Cargo.toml:

[dependencies]
axum = "0.7"
tokio = { version = "1", features = ["full"] }
reloop-rust = "0.1"

Sending an Email

use axum::{routing::post, Json, Router};
use reloop_rust::{ReloopClient, SendEmailParams};
use std::env;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/send-email", post(send_email));
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn send_email() -> Result<Json<serde_json::Value>, (axum::http::StatusCode, String)> {
    let api_key = env::var("RELOOP_API_KEY").map_err(|_| {
        (axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Missing API Key".to_string())
    })?;
    
    let client = ReloopClient::new(api_key);
    
    let params = SendEmailParams {
        from: "onboarding@reloop.dev".to_string(),
        to: vec!["delivered@resend.dev".to_string()],
        subject: "Hello from Axum".to_string(),
        html: "<p>Congrats on sending your first email via Reloop from Rust Axum!</p>".to_string(),
    };

    match client.emails.send(params).await {
        Ok(res) => Ok(Json(res)),
        Err(e) => Err((axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
    }
}

Was this page helpful?

Edit this page