cbat 0.0.19

The unofficial Rust crate for the Coinbase Advanced Trade API
Documentation
# cbat - Coinbase Advanced Trade API SDK for Rust
<p align="center">
    <a href="https://crates.io/crates/cbat" title="crates.io version">
        <img src="https://img.shields.io/crates/v/cbat?style=for-the-badge&logoColor=89b4fa&labelColor=11111b&color=89b4fa"
            alt="crates.io version"></a>
    <a href="https://crates.io/crates/cbat" title="crates.io download counter">
        <img src="https://img.shields.io/crates/d/cbat?style=for-the-badge&logoColor=89dceb&labelColor=11111b&color=89dceb"
            alt="crates.io downloads"></a>
    <a href="https://github.com/confessore/cbat" title="repo size">
        <img src="https://img.shields.io/github/repo-size/confessore/cbat?style=for-the-badge&logoColor=a6e3a1&labelColor=11111b&color=a6e3a1"
            alt="GitHub repo size"></a>
</p>

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`:

```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:

```yaml
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"
```

```rust
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

```rust
use cbat::prelude::*;

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

### Create a Limit Order

```rust
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

```rust
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](https://docs.rs/cbat).

## License

MIT