Rust

The official Rust server SDK for the Bias API. It is async-first (Tokio), with typed models, builder-based request parameters, cursor pagination, and idiomatic Result-based error handling. The crate is generated from the Bias OpenAPI specification.

Installation

[dependencies]
bias = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

Requires Rust 1.90 or later.

Usage

Construct a Client with your API key. Resources are accessed through methods on the client, and request parameters use a builder.

#[tokio::main]
async fn main() -> bias::Result<()> {
    let client = bias::Client::new(std::env::var("BIAS_API_KEY").unwrap());

    // Create a customer.
    let customer = client
        .customers()
        .create(
            bias::CustomerCreateParams::builder()
                .name("Acme, Inc.")
                .email("billing@acme.example")
                .build(),
        )
        .await?;
    println!("created {:?}", customer);

    // Retrieve one by id.
    let same = client.customers().get(customer.id.clone()).await?;
    println!("{same:?}");

    Ok(())
}

Pagination

List methods return a single page. Call items() on the page to access the rows, and read has_more to check for further pages.

let page = client
    .customers()
    .list(bias::CustomerListParams::builder().limit(100).build())
    .await?;

for customer in page.items() {
    println!("{customer:?}");
}

To walk every page automatically, use list_paginate, which returns a Paginator. Its items() method yields a Stream over every row across all pages, fetching subsequent pages with the cursor as you consume it.

use futures::StreamExt;

let mut customers = client
    .customers()
    .list_paginate(bias::CustomerListParams::builder().limit(100).build())?
    .items();

while let Some(customer) = customers.next().await {
    let customer = customer?;
    println!("{customer:?}");
}

Streaming events

The events resource can stream events over a long-lived connection. list_stream forces stream=true and returns a Stream of events; heartbeats sent to keep the connection alive are skipped automatically.

use futures::StreamExt;

let mut stream = client
    .events()
    .list_stream(bias::EventListParams::builder().build())?;

while let Some(event) = stream.next().await {
    let event = event?;
    println!("{:?}: {}", event.r#type, event.id);
}

See the event streaming guide for more.

Error handling

API errors surface as Err(bias::Error::Api(_)); transport and deserialization failures are their own variants. Match on the error to handle specific cases.

match client.customers().get("cus_missing").await {
    Ok(customer) => { let _ = customer; }
    Err(bias::Error::Api(e)) => {
        eprintln!("api error: {} (request {:?})", e.message, e.request_id);
    }
    Err(e) => eprintln!("other error: {e}"),
}

Cargo features

FeatureDefaultDescription
rustls-tlsTLS via rustls.
native-tlsTLS via the platform’s native stack.
blockingEnable the blocking reqwest backend.
timeExpose chrono-free OffsetDateTime accessors for millisecond timestamps.

For example, to use the platform’s native TLS stack instead of rustls:

[dependencies]
bias = { version = "1", default-features = false, features = ["native-tls"] }