Client access
The backend SDK exposes a small set of client-access helpers. Which one you use depends on whether you are in a framework adapter or in standalone JavaScript code, and whether you want a cached client, a fresh client, or a global singleton.
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 _users = client.users().fetch_users().send().await?; Ok(())}Signature
fn Client access( ) -> Result<(), wacht_rs::Error>Framework adapters
In Next.js, React Router, and TanStack Router, the main helpers are
wachtClient() and createWachtServerClient().wachtClient() is the default entrypoint. It returns a cached backend client when you call it without options. createWachtServerClient() always creates a fresh backend client and is the better fit when you need explicit config for one code path.Standalone JavaScript usage
In
@wacht/backend, you can create a client directly with new WachtClient(...), or you can initialize a global singleton with initClient() and read it later with getClient().The global pattern is useful in scripts, CLIs, jobs, and long-running services where one process-wide backend client is enough.
How to choose
Use
wachtClient() for ordinary framework-backed server calls. Use createWachtServerClient() when you need custom API keys, headers, base URLs, or a custom fetch implementation.Use
initClient() and getClient() only in standalone JavaScript code where the global-client pattern is actually helpful. If you need several clients in the same process, create WachtClient instances directly or use createClientStore() instead of relying on the singleton.Examples
Framework adapter: use the cached client
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(())}Framework adapter: create a fresh client
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.Client access(/* args */).send().await?; Ok(())}Standalone backend package: initialize once and reuse
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(())}