Recipe

Reqwest (Rust) primer

Reqwest is the ergonomic HTTP client of choice for Rust services calling the Meridian gateway. This primer covers async setup, streaming chat completions, and retry semantics so your worker stays resilient under load.

1.Install and configure

Add reqwest with the JSON and streaming features. Build a single Client at startup — it pools connections and reuses TLS sessions across requests.

# Cargo.toml
[dependencies]
reqwest = { version = "0.12", features = ["json", "stream"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1"

2.Call the chat endpoint

Point reqwest at https://llm.getnimbus.net/v1/chat/completions with a bearer token. Meridian accepts the OpenAI request shape verbatim, so existing JSON bodies port without change.

let client = reqwest::Client::new();
let res = client
    .post("https://llm.getnimbus.net/v1/chat/completions")
    .bearer_auth(std::env::var("MERIDIAN_KEY")?)
    .json(&serde_json::json!({
        "model": "azure/model-router",
        "messages": [{"role": "user", "content": "ping"}]
    }))
    .send()
    .await?
    .error_for_status()?
    .json::<serde_json::Value>()
    .await?;

3.Retry, timeout, and stream

Wrap calls with a 60s read timeout and exponential backoff on 429 and 5xx. For streamed completions, set stream: true in the body and consume the byte stream line by line, splitting on data: SSE prefixes.

use futures_util::StreamExt;
let mut stream = client.post(URL)
    .bearer_auth(key)
    .timeout(std::time::Duration::from_secs(60))
    .json(&body_with_stream_true)
    .send().await?
    .bytes_stream();
while let Some(chunk) = stream.next().await {
    handle_sse_chunk(chunk?);
}