Initialize the client

The backend client is created a little differently depending on where your server code runs, but the client surface after that point is the same. The framework adapters give you wachtClient() and createWachtServerClient(), while the standalone backend package gives you WachtClient, initClient(), and the global client store helpers.

Usage

The following example shows Rust backend usage with `wacht_rs`.

use wacht_rs::{Result, WachtClient};pub async fn example() -> Result<()> {    let client = WachtClient::builder()        .api_key(&std::env::var("WACHT_API_KEY")?)        .build()?;    let _health = client.health().check().send().await?;    Ok(())}

Signature

fn Initialize the client(  ) -> Result<WachtServerClient, wacht_rs::Error>

Return value

result: WachtServerClient;
Rust return type from the wacht-rs builder `.send()` call.

Framework adapters

In Next.js, React Router, and TanStack Router, the server package exposes createWachtServerClient() and wachtClient(). createWachtServerClient() always creates a fresh backend client. wachtClient() caches and reuses a default client when you call it without options.
That makes wachtClient() the right default for most server code paths, while createWachtServerClient() is the better fit when you need an alternate API key, a different base URL, or request-specific headers.

Standalone backend usage

In the standalone backend package, you can instantiate new WachtClient(...) directly, or you can initialize a global client with initClient() and then use the exported resource modules through getClient() behind the scenes.
That global style is useful in scripts and long-running JavaScript services where you want one process-wide client. If you need more than one backend client at the same time, use createClientStore() and register named clients instead of mutating the global singleton.

Runtime requirements

The backend client expects a JavaScript runtime with fetch, or a fetch implementation passed explicitly through config. That keeps the same client usable across Node.js, Bun, edge-style runtimes, and other server-side JavaScript environments.
wachtClient() in the framework adapters resolves WACHT_API_KEY for you when you do not pass apiKey, and it throws early if no API key can be found. The cached path is only used when you call it without options.

Examples

Create a named client with explicit config

use wacht_rs::{Result, WachtClient};pub async fn example() -> Result<()> {    let client = WachtClient::builder()        .api_key(&std::env::var("WACHT_API_KEY")?)        .build()?;    let _users = client.users().fetch_users().send().await?;    Ok(())}

Initialize the standalone backend package once

use wacht_rs::{Result, WachtClient};pub async fn example() -> Result<()> {    let client = WachtClient::builder()        .api_key(&std::env::var("WACHT_API_KEY")?)        .build()?;    let _result = client.Initialize the client(/* args */).send().await?;    Ok(())}

Keep multiple named backend clients

use wacht_rs::{Result, WachtClient};pub async fn example() -> Result<()> {    let client = WachtClient::builder()        .api_key(&std::env::var("WACHT_API_KEY")?)        .build()?;    let _result = client.Initialize the client(/* args */).send().await?;    Ok(())}

On this page