Skip to content

escwxyz/crypto-pay-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto Pay API Client for Rust

Crates.io Documentation License CI codecov

A type-safe Rust client for the Crypto Bot API with async support.

Features

  • Complete type safety with typestate builder pattern
  • Async/await support
  • Comprehensive error handling
  • Built-in parameter validation
  • Zero configuration
  • Webhook support
  • Full API coverage

Quick Start

Add to your Cargo.toml:

[dependencies]
crypto-pay-api = "0.2.0"

Basic Example

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(())
}

API Usage

All API methods follow a consistent builder pattern:

client.api_method()
    .optional_param(value)
    .execute()
    .await?

Creating Invoices

let invoice = client.create_invoice()
    .asset(CryptoCurrencyCode::Ton)
    .amount(dec!(100.0))
    .description("Premium subscription".to_string())
    .payload("user_123".to_string())
    .execute()
    .await?;

Querying Invoices

let invoices = client.get_invoices()
    .asset(CryptoCurrencyCode::Ton)
    .invoice_ids(vec![123, 456])
    .count(50)
    .execute()
    .await?;

Deleting Invoices

let success = client.delete_invoice(invoice_id)
    .execute()
    .await?;

Creating Transfers

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?;

Getting Balance

let balances = client.get_balance()
    .execute()
    .await?;

for balance in balances {
    println!("{}: {}", balance.currency_code, balance.available);
}

Getting Exchange Rates

let rates = client.get_exchange_rates()
    .execute()
    .await?;

Getting Statistics

let stats = client.get_stats()
    .start_at(Utc::now() - Duration::days(7))
    .end_at(Utc::now())
    .execute()
    .await?;

API Coverage

Invoices

  • Create invoice (create_invoice)
  • Get invoices (get_invoices)
  • Delete invoice (delete_invoice)

Transfers

  • Transfer (transfer)
  • Get transfers (get_transfers)

Checks

  • Create check (create_check)
  • Get checks (get_checks)
  • Delete check (delete_check)

Other Features

  • Get balance (get_balance)
  • Get exchange rates (get_exchange_rates)
  • Get currencies (get_currencies)
  • Get app info (get_me)
  • Get statistics (get_stats)

Webhook Handling

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.

Custom Configuration

let client = CryptoBot::builder()
    .api_token("YOUR_API_TOKEN")
    .base_url("https://pay.crypt.bot/api")
    .timeout(Duration::from_secs(30))
    .build()?;

Error Handling

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),
}

Documentation

Contributing

Contributions are welcome! Please check out our Contributing Guide.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Rust client for the Crypto Pay API by Telegram CryptoBot

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages