> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# The client

> Connect to an app, run its queries and mutations, and read their results

Use the generated TypeScript client in browsers, servers, and scripts. Its
`app` export gives the callable references and its `connect` carries the
reviewed pins and runtime value schemas.

```ts apps/main/src/client.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, connect } from "../../../.bijection/_generated/client";

export { app };
export const client = connect(import.meta.env.VITE_BIJECTION_URL);
```

See [Typed clients and the contract](https://docs.bijection.com/clients/typescript.md) for setup.

## Run a query

```ts apps/main/src/latest.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, client } from "./client";

export async function latestNotes() {
  const listed = await client.query(app.notes.recentNotes);
  if (!listed.ok) throw new Error(listed.message);
  return listed.value;
}
```

`client.query(fn, args)` runs the query once. Leave out the arguments when the
query declares none.

## Watch a query

```ts apps/main/src/follow.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, client } from "./client";

export async function followNotes(signal: AbortSignal) {
  const live = client.watch(app.notes.recentNotes);
  signal.addEventListener("abort", () => live.stop());
  for await (const listed of live) {
    if (listed.ok) console.log(listed.value.map((note) => note.text));
    else console.error(listed.code, listed.message);
  }
}
```

`client.watch(fn, args)` runs the query and answers a new `Result` each time
its value changes. A watch at a specified world or valid instant yields once
and ends. Call `stop()` to end one watch or `await client.close()` to end
all watches from the client and its framed clients.

## Run a mutation

```ts apps/main/src/add.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, client } from "./client";

export async function addNote(text: string) {
  const added = await client.mutate(app.notes.add, { text });
  if (added.ok) return added.value;
  if (added.code === "Invalid") return added.message;
  throw new Error(`${added.code}: ${added.message}`);
}
```

`client.mutate(fn, args, { once, optimistic, seen })` runs the mutation as one
transaction. Its identity is `once`: the same `once` and arguments answer the
original result instead of running twice. When you give none, the client mints
one for the call. See [Retries and read-your-writes](https://docs.bijection.com/clients/requests.md).

## Results

Every client verb answers a `Result`. Check `result.ok` before reading
the value or handling a refusal.

* `value` is what the function returned. A committed mutation also carries its
  commit `version`. Pass it as `seen` to [read your own writes](https://docs.bijection.com/clients/requests.md#read-your-own-writes-version-and-seen).
* `code` is one of the function's declared `refuses` codes, or a host code.
  It is typed: `added.code` above is a `HostCode`, since `add` declares no
  `refuses`; with `refuses: { EmptyNote: null }` it is `"EmptyNote"` or a `HostCode`.
* `reason` is the precise cause when the host names one.
* `message` is the sentence the caller is entitled to read. Show it as it is.
* `guard` is the path of the guard that refused, on `Forbidden`.

The host codes are a closed set:

| Code | What it means for a client |
| - | - |
| `Unknown` | The reply was lost. For a mutation, retry with the refusal’s `once`. |
| `Disallowed` | The caller's surviving grants hold no role in the function's `allow`, or the frame does not clear a label the call needs. |
| `Forbidden` | A guard matched the act. `guard` names it. |
| `Invalid` | The arguments break their declared types, or a `once` was reused with other arguments. |
| `Absent` | A strict write assumed a present fact and found none. |
| `Exists` | `insert` found a fact, or a `unique` set is taken. |
| `Conflict` | The world moved past what the call relied on, or the called function’s contract changed. |
| `Erased` | The addressed unit was erased. |
| `Expired` | A deadline passed first. |
| `Withdrawn` | The task was withdrawn. |
| `Limit` | A time, size or budget bound was exceeded. |
| `Failed` | It definitely did not happen: the handler threw, or the performer failed. |

A function open only to `role.anyone` or `role.signedIn` is entered through
the class grant the program issues at install, which clears only the public
label. If its `under` requires more clearance, entry refuses
`Disallowed` with reason `NotCleared`. Admit such a function through a role
that clears the label.

## Authenticate

`connect(url, { token })` takes an auth adapter for a browser session, or a
fixed token for a server or a script. A client without `token` calls as the
signed-out principal.

```ts apps/main/src/server.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { connect } from "../../../.bijection/_generated/client";

export function clientFor(installationUrl: string, token: string) {
  return connect(installationUrl, { token });
}
```

Make one client per identity. See [Connect and authenticate](https://docs.bijection.com/clients/react/configuration.md)
for the WorkOS AuthKit, Clerk and Bijection Auth adapters.

## Connect options

| Option | Meaning |
| - | - |
| `token` | The caller's credential: an auth adapter, or a fixed token. |
| `contract` | Required on the raw connector. Pass reviewed per-path digests or choose `"discover"` to call the serving interface. Generated clients supply the digests. |
| `headers` | Headers added to every call, or a function read on every call. A browser cannot set them on the live connection. |
| `fetch` | The `fetch` calls use. |
| `httpProof` | Sign each HTTP call and watch with the key bound to a Bijection Auth session. |

`client.connection()` reports the live connection that watches and
optimistic edits use: `{ connected, everConnected, connections, retries }`,
opening it if it is not open. Show it as an offline banner: `connected`
false twice in a fact with the same `connections` count is a lost connection,
not a reconnect.

For a script that deliberately follows the serving interface, use the raw
connector with `contract: "discover"`. Pass `api("orders.place")` to call a
path dynamically. Its answer is a decoded wire value, with stored Files
represented by their keys.

`connect({ transport, contract: "discover" })` runs the same client over a `ClientTransport` you
supply instead of a URL, for example inside an MCP view. A transport that
sets `frames: true` receives each call's frame, and one with `trial` serves
`client.trial`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.