Skip to main content

Crate cs2_api

Crate cs2_api 

Source
Expand description

§cs2-api: Counter-Strike 2 esports and skin market API client for Rust

The official Rust client for the CS2 API at cs2-api.org.

Live scores, match results, schedules, player stats (rating, ADR, KAST), teams and rosters, the world ranking and Valve Regional Standings, events and brackets, transfers, and skin prices across marketplaces.

Get a free API key (500 requests/month): https://cs2-api.org

§Quickstart

use cs2_api::Client;

fn main() -> Result<(), cs2_api::Error> {
    // Reads the key from the CITO_API_KEY environment variable.
    let cs2 = Client::from_env()?;

    // Every match being played right now.
    let live = cs2.live()?;
    for m in live.as_array().into_iter().flatten() {
        println!(
            "{} {}-{} {}",
            m["team1Name"], m["team1Score"], m["team2Score"], m["team2Name"]
        );
    }

    let ranking = cs2.world_ranking(10)?;
    let vitality = cs2.team("vitality")?; // roster + recent results
    let donk = cs2.player("donk")?;
    let prices = cs2.skin_prices("ak-47-redline-field-tested-1ab1a2dc")?;
    println!("{ranking}\n{vitality}\n{donk}\n{prices}");
    Ok(())
}

Every method returns the data field of the API’s JSON envelope as a serde_json::Value. Use Client::get_raw to get the whole envelope, including meta for pagination.

Structs§

Client
CS2 API client. Cheap to clone; clones share one connection pool.
ClientBuilder
Builder for a Client with a custom base URL or timeout.

Enums§

Error
Errors returned by the client.
Value
Represents any valid JSON value.

Constants§

API_KEY_ENV
Environment variable the client reads the API key from.
DEFAULT_BASE_URL
Default API base URL.
VERSION
Crate version, sent in the User-Agent header.

Functions§

build_url
Builds a full request URL from a base URL, a path and query pairs.
encode_segment
Percent-encodes one URL path segment (or query component).