Rust SDK
On this page
Related contents
Query ScopeDB and write events from async Rust applications. This guide uses scopedb-client 0.5.0.
Install and configure
Use Rust 1.91.0 or later. In your application's Cargo project, install the SDK, JSON support, and Tokio:
cargo add scopedb-client@0.5.0 serde_json
cargo add tokio --features macros,rt-multi-threadThe examples run inside Tokio's async runtime. Follow the application connection guide to obtain an endpoint and API key, then set them in the shell where you run the examples:
export SCOPEDB_ENDPOINT="https://<endpoint>"
export SCOPEDB_API_KEY="<api-key>"Keep the key on your server. SDKs do not reuse the CLI's interactive login.
Run a query
Save this complete program as src/main.rs and run cargo run. It verifies the connection without creating a table or requiring sample data.
use std::error::Error;
use scopedb_client::Client;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
let endpoint = std::env::var("SCOPEDB_ENDPOINT")?;
let api_key = std::env::var("SCOPEDB_API_KEY")?;
let client = Client::builder(endpoint)
.api_key(api_key)
.build()?;
let result = client.query("SELECT 1 AS ready").await?;
println!("ready: {}", result.into_objects()?[0]["ready"]);
Ok(())
}Expected output:
ready: 1query() waits for execution to finish; into_objects() returns rows keyed by column name. Reuse the client, and add the following examples inside main before Ok(()).
Write rows
Apply the events table definition used by the quickstart through the Catalog API. Applying the same definition again preserves existing rows and rejects changes that could remove data unless allow_destructive: true is passed:
use scopedb_client::{CatalogOptions, CatalogTable};
let table = client.table("events");
let definition: CatalogTable = serde_json::from_value(serde_json::json!({
"name": "events",
"columns": [
{ "name": "time", "data_type": "timestamp" },
{ "name": "service", "data_type": "string" },
{ "name": "name", "data_type": "string" },
{ "name": "message", "data_type": "string" },
{ "name": "var", "data_type": "object" },
],
}))?;
table.catalog().apply(&definition, CatalogOptions::default()).await?;Append one event and wait for it to commit:
let row = serde_json::json!({
"time": "2026-10-23T09:00:00Z",
"service": "checkout",
"name": "payment_failed",
"message": "Credit card gateway timed out",
"var": {"region": "apac", "gateway": "credit_card"},
});
let stream = table.append_stream().build()?;
stream.send(&row).await?;
let report = stream.shutdown().await?;
println!("Written rows: {}", report.committed_rows);send() accepts a row into the SDK's buffer. shutdown() sends remaining rows, waits for delivery, and closes the stream. With this default failure policy, a failed batch returns an error. A successful run prints Written rows: 1.
Read the event back after shutdown:
let written = client.query("
FROM events
WHERE time = '2026-10-23T09:00:00Z'::timestamp
AND service = 'checkout'
AND name = 'payment_failed'
SELECT service, name, var['gateway']::string AS gateway
LIMIT 10
").await?;
println!("{:?}", written.to_objects()?);The result includes checkout, payment_failed, and credit_card. Each run appends another event. The examples use the default scopedb database and public schema; use with_database(...) or with_schema(...) on the table for another table location.
Work with results
Use to_objects() for named columns or to_values() for positional rows. An empty result produces an empty vector. first() returns an optional row:
if let Some(first) = written.first()? {
println!("{}", first["name"]);
}Object conversion rejects duplicate column names; use unique aliases or positional results. The consuming into_objects() and into_values() methods move the result instead of borrowing it.
Cells use the SDK's Value enum, including signed and unsigned 64-bit integers, Jiff timestamps and intervals, and Value::Null. Arrays, objects, and any values retain their textual representation; match the variant before parsing. raw_rows() exposes the original result text.
Manage table definitions
Read the table's complete definition and catalog version:
let catalog = table.catalog();
let snapshot = catalog.get().await?;
println!("{:?}", snapshot.definition.columns);
println!("{}", snapshot.version);To change its comment, preserve the definition and preview the replacement:
let mut definition = snapshot.definition;
definition.comment = Some("Checkout events".to_string());
let preview = catalog.apply(&definition, CatalogOptions {
dry_run: true,
expected_version: Some(snapshot.version),
..Default::default()
}).await?;
println!("{:?}", preview);Review before, after, and destructive. Apply the same definition with the preview's version:
let applied = catalog.apply(&definition, CatalogOptions {
expected_version: Some(preview.version),
..Default::default()
}).await?;
if let Some(after) = applied.after {
println!("{}", after.comment.unwrap_or_default());
}apply() replaces the complete definition; omitted fields are cleared. Typed reads reject unknown fields; use raw() to retain them during exports or edits. The version covers the entire tenant catalog. A stale version returns HTTP 412 with code precondition_failed and ErrorKind::Conflict; read the definition again and preview a new plan. Potentially destructive changes also require allow_destructive: true; this comment edit does not. See the Catalog contract for resource selection and interrupted writes.
Configure queries and writes
Query execution
Use a statement builder to set execution limits or retain a handle for progress and cancellation:
use std::time::Duration;
let mut handle = client.statement("SELECT 1 AS ready")
.with_max_elapsed(Duration::from_secs(60))
.submit().await?;
println!("{:?}", handle.snapshot());
let query_result = handle.wait().await?;
println!("{:?}", query_result.into_objects()?);snapshot() borrows cached state; refresh().await fetches an active statement's state. client.statement_handle(id) resumes tracking a known ID. with_max_elapsed includes queueing; with_max_estimated separately limits estimated duration.
Dropping or timing out a query future does not request server-side cancellation. Call handle.cancel().await and inspect its returned snapshot: execution may have finished already or may still be active. Confirmed cancellation makes wait() return ErrorKind::Cancelled.
Batching and delivery
Use flush() to wait for buffered writes while keeping a stream open. Streams target 4 MiB batches and allow four concurrent requests by default. Each SDK request is capped at 8 MiB and 200,000 rows.
For a long-running producer that must continue after a failed batch:
use scopedb_client::AppendFailurePolicy;
let telemetry = table.append_stream()
.failure_policy(AppendFailurePolicy::Continue)
.max_concurrent_batches(1)
.build()?;
if let Err(error) = telemetry.try_send(&row) {
eprintln!("Not queued: {error}");
}
// Stop producers before shutting down the stream.
let delivery = telemetry.shutdown().await?;
println!("{:?}", delivery);try_send() returns an error when a row cannot enter the local buffer. Continue mode reports failures without retaining failed payloads for replay; use durable storage when loss is unacceptable. Inspect outcome, failed_rows, and ambiguous_rows as well as committed rows. See AppendStreamBuilder for buffering, circuit breaking, and retry controls.
Streams and finite writers retry validated HTTP 429 admission refusals by default. retry_ambiguous(true) allows retries of transient ambiguous failures and accepts the risk of duplicate rows. max_retries(0) disables SDK retries; client-side append timeouts are never retried automatically.
Finite writes
For a slice of rows, use a reusable finite writer. write() waits for its scheduled requests and returns a report with input ranges and outcomes:
let writer = table.append_writer().build()?;
let rows = vec![row.clone()];
match writer.write(&rows).await {
Ok(report) => println!("Written rows: {}", report.committed_rows()),
Err(error) => {
eprintln!("Write progress: {:?}", error.append_write_report());
return Err(error.into());
}
}On failure, inspect append_write_report() before replaying input: earlier requests may have committed, and unsubmitted() identifies the suffix that was never sent. Use append_input_index() to locate a row that failed local encoding. See AppendWriter for request limits, concurrency, and cancellation behavior.
Row encoding
Streams accept serializable structs or serde_json::Value objects through Serde. For existing NDJSON, use Table::append, which makes one attempt. Shape values in the producer; Append does not run a ScopeQL transformation. See the shared JSON input types for accepted field values and schema rules.
Timeouts
Queries and Catalog reads have no SDK deadline by default. Bound local waiting in the application and use the statement's execution limit to bound server work.
Append attempts and Catalog mutations default to 30 seconds, including response-body reads. Configure ClientBuilder::write_timeout or a stream's attempt_timeout with std::time::Duration. Rust requires a finite, positive duration; this attempt deadline cannot be disabled. See ClientBuilder when supplying an HTTP client with its own timeouts.
Handle errors
Use kind() for the failure category and operation() for the SDK operation. Preserve the server code and HTTP metadata for diagnostics:
if let Err(error) = client.query("SELECT 1 AS ready").await {
eprintln!("{:?} {:?}", error.kind(), error.operation());
eprintln!("{:?} {:?} {:?}", error.code(), error.http_status(), error.request_id());
eprintln!("{:?} {:?}", error.statement_details(), error.write_details());
return Err(error.into());
}For an Append failure, inspect append_details().status: Failed means no rows took effect or can take effect later; Ambiguous means the request may have taken effect or may still take effect later. A successful request returns AppendOutcome::Succeeded. Handle each failed request as a whole batch; structured row diagnostics may be absent. is_retryable() describes whether repeating the same call as-is is safe and useful; ambiguous writes and errors carrying aggregate progress reports return false.
Failed send_all() calls retain admission progress in append_admission(). Failed flush() and shutdown() calls retain delivery counts in append_delivery_report(). Read those reports before replaying input, since part of the work may already have committed.
Catalog writes never retry automatically. catalog_outcome() returns CatalogMutationOutcome::Rejected for known rejection or Unknown when the mutation may have committed; read the current definition before replaying an unknown mutation. If a statement submission or later wait fails, use error.statement_id() with client.statement_handle(id) to resume tracking the existing execution. See Error for recovery contracts and HTTP protocol for response formats.