ctrader-rs 0.1.2

Rust SDK for the cTrader Open API
Documentation
//! Get all broker account IDs linked to your access token.
//!
//! Usage:
//!   cargo run --example get_accounts

use std::time::Duration;

use ctrader_rs::{Client, Config};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenvy::dotenv().ok();

    tracing_subscriber::fmt()
        .with_env_filter("get_accounts=debug,ctrader_rs=debug")
        .with_line_number(true)
        .init();

    let client_id = std::env::var("CTRADER_CLIENT_ID")?;
    let secret = std::env::var("CTRADER_SECRET")?;
    let token = std::env::var("CTRADER_TOKEN")?;

    let config = Config::new(client_id, secret).deadline(Duration::from_secs(5));

    tracing::debug!("Connecting to demo.ctraderapi.com:5035 …");
    let client = Client::start(config).await?;
    tracing::debug!("✓ Connected and application authenticated");

    // Fetch all accounts linked to the token
    let res = client.get_accounts_by_access_token(&token).await?;

    if res.ctid_trader_account.is_empty() {
        tracing::debug!("No accounts found for this access token.");
    } else {
        tracing::debug!("\nFound {} account(s):\n", res.ctid_trader_account.len());
        tracing::debug!(
            "{:<25} {:<8} {:<15} {}",
            "ctidTradingAccountId",
            "Type",
            "TraderLogin",
            "Broker"
        );
        tracing::debug!("{}", "-".repeat(65));
        for acc in &res.ctid_trader_account {
            tracing::debug!(
                "{:<25} {:<8} {:<15} {}",
                acc.ctid_trader_account_id,
                if acc.is_live.unwrap_or(false) {
                    "live"
                } else {
                    "demo"
                },
                acc.trader_login.unwrap_or(0),
                acc.broker_title_short.as_deref().unwrap_or(""),
            );
        }
    }

    Ok(())
}