K Khay logo
K Khay
Get Started
crates.io: kkhayv1.0.4Async Tokio

Rust SDK

The official Rust SDK for K Khay Gateway. Asynchronous, compile-time typed, and zero-unsafe. Powered by Tokio and Reqwest for ultra-low latency transaction processing in Actix-web, Axum, or Rocket.

1. Installation

Cargo Package Manager
$cargo add kkhay

2. Async Invoice Creation

main.rs
use kkhay::{KkhayClient, CreateInvoiceParams};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("KKHAY_API_KEY").expect("KKHAY_API_KEY must be set");
let client = KkhayClient::new(api_key);
let params = CreateInvoiceParams {
amount: 50.00,
currency: "USD".to_string(),
pay_network: Some("bsc".to_string()),
pay_token: Some("USDT".to_string()),
order_id: "ORD-9901".to_string(),
title: Some("VPN 1-Year Pass".to_string()),
customer_email: Some("client@privacy.org".to_string()),
redirect_url: Some("https://mysite.com/success".to_string()),
cancel_url: Some("https://mysite.com/cancel".to_string()),
};
let invoice = client.create_invoice(params).await?;
println!("Invoice ID: {}", invoice.id);
println!("Hosted URL: {}", invoice.hosted_url);
Ok(())
}

3. Webhook Verification (Axum)

webhook.rs
use axum::{
body::Bytes,
http::{HeaderMap, StatusCode},
response::IntoResponse,
routing::post,
Router,
};
use kkhay::verify_signature;
async fn handle_webhook(headers: HeaderMap, body: Bytes) -> Result<impl IntoResponse, StatusCode> {
let signature = headers
.get("x-kkhay-signature")
.and_then(|v| v.to_str().ok())
.ok_or(StatusCode::BAD_REQUEST)?;
let secret = std::env::var("KKHAY_WEBHOOK_SECRET").unwrap();
if !verify_signature(&body, signature, &secret) {
return Err(StatusCode::UNAUTHORIZED);
}
// Process confirmed payment event
Ok(StatusCode::OK)
}