Brain Docs

Reference

Rust SDKInstallationHTTP clientWire client
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
Rust SDKHTTP client

HTTP client

BrainHttpClient — construction, the builder configuration methods, borrowed inputs, and what the HTTP tier cannot do.

PURPOSE

BrainHttpClient talks JSON to the Brain HTTP edge — brain-edge self-hosted, or the Arc cloud gateway. This page documents construction and the conventions that apply to every verb on it.

Construction

Bearer API keystable
fn new(api_key: impl Into<String>, base_url: impl Into<String>) -> Self
fn try_new(api_key: impl Into<String>, base_url: impl Into<String>)
    -> Result<Self, BrainHttpError>
fn localhost(api_key: impl Into<String>) -> Self
fn with_timeout(self, timeout: Duration) -> Self
fn with_retry_policy(self, policy: HttpRetryPolicy) -> Self
ParameterTypeRequired
new(api_key, base_url)BrainHttpClientrequired

Sent as Authorization: Bearer <api_key>. Validates nothing — it returns Self and has nowhere to report a problem.

try_new(api_key, base_url)Result<BrainHttpClient, BrainHttpError>optional

The checked form. Refuses a credential or base URL that cannot work, with status: 0 and code: "config", instead of failing later as an opaque 401 or a request to nowhere.

localhost(api_key)BrainHttpClientoptional

Shorthand for the self-host address, http://127.0.0.1:8080.

with_timeout(timeout)BrainHttpClientoptional

Per-request timeout, as a Duration. A timeout surfaces as a BrainHttpError with status == 0.

with_retry_policy(policy)BrainHttpClientoptional

Replaces the default policy. See Retries.

use std::time::Duration;
use brain_db_sdk::http::BrainHttpClient;

let brain = BrainHttpClient::new(
    std::env::var("BRAIN_API_KEY")?,
    std::env::var("BRAIN_BASE_URL").unwrap_or_else(|_| "http://127.0.0.1:8080".into()),
)
.with_timeout(Duration::from_secs(15));

The configuration methods consume and return Self, so they chain at construction. There is no close — the client holds no connection.

What try_new checks

Every failure is a BrainHttpError with status: 0 and code: "config", returned before any request is attempted.

CheckMessage
api_key is non-emptyapi_key is required
api_key carries no ASCII control characterapi_key must not contain control characters
base_url has an http or https schemebase_url must be http:// or https://, got …

The control-character rule covers every C0 character and DEL, except HTAB — RFC 7230 permits a tab in a field value and reqwest sends it, so rejecting it would make this constructor stricter than the request path it guards. The range stops below 0x80, so a key carrying a non-ASCII letter still works. The ordinary way one gets in is a key read from an env file with a trailing newline; the message names no part of the key, because it is a credential.

The scheme check is not the SSRF guard it is in Python — reqwest cannot speak anything but HTTP — so here it earns its place by catching the argument swap below. base_url is trimmed before it is checked and before it is stored, so a stray space or newline from an env var neither fails validation nor resurfaces at request time.

All three checks are identical in the Python and TypeScript clients, which validate from their single constructor, so the same key is accepted or refused the same way in all three SDKs.

Key first, URL second — and a swap still compiles

Both parameters are impl Into<String>, so new("https://edge", key) type-checks and silently sends your base URL as the bearer token. try_new is what catches it: an API key in the URL position has no scheme, so the scheme check fails loudly.

let brain = BrainHttpClient::try_new(
    std::env::var("BRAIN_API_KEY")?,
    std::env::var("BRAIN_BASE_URL")?,
)?;

Conventions

Borrowed inputs

Every verb takes &Input and returns Result<Output, BrainHttpError>:

let res = brain.encode(&EncodeInput { text: "...".into(), ..Default::default() }).await?;

Because the input is borrowed rather than consumed, retrying is the same call with the same value — nothing to clone and nothing to rebuild.

Most input structs implement Default, so ..Default::default() is the idiomatic way to set only the fields you care about.

Async runtime

The verbs are async fn. The crate does not start a runtime for you; drive them from whatever executor your application already runs.

Error type

One error type, BrainHttpError, carrying status, a stable string code and field_errors. status == 0 means the request never reached the edge. See Errors.

What the HTTP tier cannot do

OptionTypeDefault / Env
Transactionswire only

No txn_begin. Each HTTP verb is its own unit. See txnBegin.

act_aswire only

The space comes from the credential. See act_as.

Frame-level streamingwire only

No *_frames variants. Only subscribe streams, over SSE.

ping and cancel_streamwire only

There is no connection to keep alive and no stream id to cancel.

See also
Wire clientErrorsRetries

Was this page helpful?

Installation

Add brain-db-sdk to a Cargo project, its runtime requirements, and how to reach each tier.

Wire client

BrainClient over the BRN0 protocol — connect, ClientConfig, the two auth methods, act_as, multiplexing, the pool, and the id helpers.

On this page

ConstructionWhat try_new checksConventionsBorrowed inputsAsync runtimeError typeWhat the HTTP tier cannot do
Suggest an editOpen in Playground