cbat 0.0.19

The unofficial Rust crate for the Coinbase Advanced Trade API
Documentation

cbat - Coinbase Advanced Trade API SDK for Rust

A Rust SDK for the Coinbase Advanced Trade API, providing async/await support for trading, account management, and real-time data via WebSocket.

Installation

Add this to your Cargo.toml:

[dependencies]
cbat = "0.0.19"

Quick Start

Set your environment variables. The secret must be an EC key in sec1 format and will be converted to pkcs#8:

CBAT_KEY_NAME="organizations/{org_id}/apiKeys/{key_id}"
CBAT_KEY_SECRET="-----BEGIN EC PRIVATE KEY-----\nYOUR PRIVATE KEY\n-----END EC PRIVATE KEY-----\n"
use cbat::prelude::*;

#[tokio::main]
async fn main() {
    let client = Client::new(
        "my-app",
        std::env::var("CBAT_KEY_NAME").unwrap(),
        std::env::var("CBAT_KEY_SECRET").unwrap()
    );
    
    // Get account information
    let accounts = ApiAccounts::list_accounts(&client).await.unwrap();
    println!("Accounts: {:?}", accounts);
}

Features

  • Accounts: List accounts, get balances, manage portfolios
  • Orders: Create, edit, cancel orders, preview orders
  • Products: Get product info, order books, market trades, candles
  • Public API: Server time, public products, market data
  • WebSocket: Real-time feeds for tickers, candles, level2, user events
  • Futures & Perpetuals: Support for futures and perpetual contracts

Usage Examples

Get Product Information

use cbat::prelude::*;

let product = ApiPublic::get_public_product(&client, "BTC-USD").await.unwrap();
println!("Product: {:?}", product);

Create a Limit Order

use cbat::prelude::*;

let request = CreateOrderRequest {
    client_order_id: &uuid::Uuid::new_v4().to_string(),
    product_id: "BTC-USD",
    side: "BUY",
    order_configuration: OrderConfiguration {
        limit_limit_gtc: Some(LimitLimitGtc {
            base_size: Some("0.001".to_string()),
            limit_price: Some("50000.0".to_string()),
            post_only: None,
            quote_size: None,
        }),
        ..Default::default()
    },
    ..Default::default()
};

let order = ApiOrders::create_order(&client, request).await.unwrap();
println!("Order created: {:?}", order);

WebSocket Subscription

use cbat::prelude::*;

let mut ws_client = WebSocketClient::new(&client).await.unwrap();
ws_client.subscribe(vec![Channel::Ticker("BTC-USD".to_string())]).await.unwrap();

while let Some(message) = ws_client.next().await {
    println!("Received: {:?}", message);
}

API Reference

Full API documentation is available at docs.rs/cbat.

License

MIT