Axum
Learn how to cash your first crypto payment using Axum the vxpay Rust SDK.
Prerequisites
1. Install
Get the vxpay Rust SDK and the Tokio (opens in a new tab) runtime.
cargo add vxpay-rs
cargo add tokio -F macros,rt-multi-thread
2. Accept onchain payment
main.rs
use std::sync::Arc;
use axum::{extract::State, http::StatusCode, routing::get, Router};
use vxpay_rs::{VXPAY, Result};
// Cloning the vxpay client is fine and cheap as the internal HTTP client is not cloned.
#[derive(Clone)]
struct AppState {
vxpay: VXPAY,
}
#[tokio::main]
async fn main() {
let shared_state = Arc::new(AppState {
vxpay: VXPAY::new("YOUR_API_KEY"),
});
// build our application with a single route
let app = Router::new()
.route("/", get(endpoint))
// provide the state so the router can access it
.with_state(shared_state);
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn endpoint(State(state): State<Arc<AppState>>) -> Result<String, StatusCode> {
...
}
Opening your browser at http://localhost:3000
(or running curl localhost:3001
) should create a checkout and return you its id!