A type-safe Rust client for the Crypto Bot API with async support.
- Complete type safety with typestate builder pattern
- Async/await support
- Comprehensive error handling
- Built-in parameter validation
- Zero configuration
- Webhook support
- Full API coverage
Add to your Cargo.toml:
[dependencies]
crypto-pay-api = "0.2.0"use crypto_pay_api::prelude::*;
#[tokio::main]
async fn main() -> Result<(), CryptoBotError> {
// Initialize client
let client = CryptoBot::builder()
.api_token("YOUR_API_TOKEN")
.build()?;
// Create an invoice using the builder pattern
let invoice = client.create_invoice()
.asset(CryptoCurrencyCode::Ton)
.amount(dec!(10.5))
.description("Test payment".to_string())
.execute()
.await?;
println!("Payment URL: {}", invoice.bot_invoice_url);
Ok(())
}All API methods follow a consistent builder pattern:
client.api_method()
.optional_param(value)
.execute()
.await?let invoice = client.create_invoice()
.asset(CryptoCurrencyCode::Ton)
.amount(dec!(100.0))
.description("Premium subscription".to_string())
.payload("user_123".to_string())
.execute()
.await?;let invoices = client.get_invoices()
.asset(CryptoCurrencyCode::Ton)
.invoice_ids(vec![123, 456])
.count(50)
.execute()
.await?;let success = client.delete_invoice(invoice_id)
.execute()
.await?;let transfer = client.transfer()
.user_id(123456789)
.asset(CryptoCurrencyCode::Usdt)
.amount(dec!(50.0))
.spend_id("unique_id_123".to_string())
.comment("Payment for services".to_string())
.execute()
.await?;let balances = client.get_balance()
.execute()
.await?;
for balance in balances {
println!("{}: {}", balance.currency_code, balance.available);
}let rates = client.get_exchange_rates()
.execute()
.await?;let stats = client.get_stats()
.start_at(Utc::now() - Duration::days(7))
.end_at(Utc::now())
.execute()
.await?;- Create invoice (
create_invoice) - Get invoices (
get_invoices) - Delete invoice (
delete_invoice)
- Transfer (
transfer) - Get transfers (
get_transfers)
- Create check (
create_check) - Get checks (
get_checks) - Delete check (
delete_check)
- Get balance (
get_balance) - Get exchange rates (
get_exchange_rates) - Get currencies (
get_currencies) - Get app info (
get_me) - Get statistics (
get_stats)
use crypto_pay_api::prelude::*;
#[tokio::main]
async fn main() -> Result<(), CryptoBotError> {
let client = CryptoBot::builder()
.api_token("YOUR_API_TOKEN")
.build()?;
let mut handler = client.webhook_handler().build();
// Register payment callback
handler.on_update(|update| async move {
println!("Invoice paid: {:?}", update.payload);
Ok(())
});
// Start webhook server
// ... integrate with your web framework
Ok(())
}See examples/axum_webhook.rs for a complete example using axum.
let client = CryptoBot::builder()
.api_token("YOUR_API_TOKEN")
.base_url("https://pay.crypt.bot/api")
.timeout(Duration::from_secs(30))
.build()?;The library provides detailed error types:
match client.get_balance().execute().await {
Ok(balances) => {
for balance in balances {
println!("{}: {}", balance.currency_code, balance.available);
}
}
Err(CryptoBotError::ValidationError { kind, message, field }) => {
eprintln!("Validation error: {} (field: {:?})", message, field);
}
Err(e) => eprintln!("Other error: {}", e),
}Contributions are welcome! Please check out our Contributing Guide.
This project is licensed under the MIT License - see the LICENSE file for details.