Expand description
Integration of the standardwebhooks crate with the Axum web framework.
This crate provides an extractor for Axum that verifies webhook requests according to the Standard Webhooks specification.
§Example
use axum::{Router, routing::post, Json};
use axum_standardwebhooks::{StandardWebhook, SharedWebhook, Webhook};
use serde_json::Value;
use std::sync::Arc;
use axum::extract::FromRef;
async fn webhook_handler(StandardWebhook(Json(payload)): StandardWebhook<Json<Value>>) -> String {
// The webhook signature has been verified, and we can safely use the payload
format!("Received webhook: {}", payload)
}
#[derive(Clone)]
struct AppState {
webhook: SharedWebhook,
}
impl FromRef<AppState> for SharedWebhook {
fn from_ref(state: &AppState) -> Self {
state.webhook.clone()
}
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/webhooks", post(webhook_handler))
.with_state(AppState {
webhook: SharedWebhook::new(Webhook::new("whsec_C2FVsBQIhrscChlQIMV+b5sSYspob7oD").unwrap()),
});
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}Structs§
- Shared
Webhook - A thread-safe wrapper around
Webhookto make it shareable between Axum handlers. - Standard
Webhook - An extractor that verifies a webhook request and extracts the inner payload.
- Webhook
Enums§
- Standard
Webhook Rejection - Represents the ways in which webhook verification and extraction can fail. Represents the ways in which webhook verification and extraction can fail.
- Webhook
Error