Install the packages
These packages ship with OpenClaw release trains. During the initial rollout, npm
may return
E404 until the first package-bearing OpenClaw release is published;
install them only after the registry pages below resolve.@openclaw/gateway-protocolprovides schemas, runtime validators, TypeScript types, client identity and capability registries, structured error readers, and protocol version constants. Its npm tarball also includes the generatedprotocol.schema.jsonmachine-readable contract.@openclaw/gateway-clientis the reference connection implementation. Import the package root for the Node client and@openclaw/gateway-client/browserfor the browser-safe protocol, device-auth, and reconnect helpers.
Choose scopes and pair the device
A full interactive chat client that also renders approval prompts should requestrole: "operator" with these scopes:
Add
operator.questions only if the client handles interactive questions,
operator.pairing only if it manages paired devices or nodes, and
operator.admin only for administrative operations such as config.patch.
The operator scopes reference
defines the complete method and approval-time rules.
Do not create a per-client bearer token by hand-editing openclaw.json. Configure
the Gateway’s shared bootstrap authentication with openclaw configure --section gateway or the openclaw onboard --gateway-auth ... options, then let device
pairing mint the client token:
- Persist an Ed25519 device identity in the client.
- Wait for
connect.challenge, sign the challenge-bound device payload, and sendconnectwith the requested operator role, scopes, and the shared Gateway token or password for bootstrap authentication. - If the Gateway returns structured
PAIRING_REQUIREDdetails, show the request ID and pause or retry according toerror.details.recommendedNextStep. - On the Gateway host, review the request with
openclaw devices list, then approve that exact current request withopenclaw devices approve <requestId>. - Reconnect and persist
hello-ok.auth.deviceTokenwith the negotiated role and scopes. Use that device token for later connections.
Advertise client capabilities
connect.params.caps describes optional behavior the client can consume. It does
not grant authorization. Import names from GATEWAY_CLIENT_CAPS instead of
duplicating string literals:
approvals, exec-approvals, inline-widgets,
run-tool-bindings, session-scoped-events, plugin-approvals,
task-suggestions, terminal-offset-seq, tool-events, and ui-commands.
Advertise only capabilities the client actually implements.
Capability-gated agent tools are a separate use of the same declaration. If an
agent tool requires a client capability, the Gateway omits that tool unless the
originating client advertised every required capability.
Recover state after reconnect
Treat every successful reconnect as a new projection over durable history and current in-memory run state:- Re-establish
sessions.subscribeand the selected session’ssessions.messages.subscribesubscription. - Call
chat.historyfor the selectedsessionKeyand replace local persisted rows with the returnedmessagesprojection. - If
inFlightRunis present, adopt itsrunId, bufferedtext, and optionalplan. Adopt the run even whentextis empty. - Read
sessionInfo.hasActiveRunandsessionInfo.activeRunIds. Prefer exact membership inactiveRunIdswhen deciding whether a retained run still owns the streaming UI. A truehasActiveRunwith no listed ID can represent another active runtime projection. - Reconcile subsequent
agentevents bypayload.runIdandpayload.seq. Maintain the highest accepted sequence independently for each run, ignore an already-seen or lower sequence, and treat a forward gap as a reason to reload authoritative history.
seq, which orders events on the
current WebSocket connection. It resets with a new connection. The seq inside
an agent event payload is assigned per run and orders that run’s lifecycle,
assistant, plan, tool, and other stream events.
Use history metadata and stable anchors
Rows returned bychat.history can carry an __openclaw metadata envelope:
idis the transcript entry identity. Use it for anchored history requests, but not as a unique display-row key.seqis the positive transcript-record sequence. One stored record can project into more than one display row, so keep siblings with the sameidand sequence together.kindidentifies synthetic rows. A compaction boundary useskind: "compaction"and may includetokensBeforeandtokensAfterwhen a matching checkpoint recorded those metrics.
hasMore and nextOffset values. Numeric
offsets describe the current transcript projection, so do not persist them as
long-lived bookmarks across reset or compaction. Persist __openclaw.id instead.
To restore around a known row, call chat.history with messageId and the
sessionId that returned it. The Gateway can resolve that anchor from reset
archive history; anchored responses intentionally omit numeric paging metadata.
Subscribe instead of polling usage
Load the initial catalog withsessions.list, then call sessions.subscribe once
per connection. Merge sessions.changed events by sessionKey. Session change
payloads can carry live inputTokens, outputTokens, totalTokens,
totalTokensFresh, contextTokens, estimatedCostUsd, response-usage settings,
and active-run state.
Some change notifications are only invalidation signals. If an event omits the
row fields your view needs, refresh sessions.list. Do not poll usage.cost or
sessions.usage to keep a live session list current; reserve those methods for
on-demand aggregate or detailed reports.
Backfill exec approvals
A client withoperator.approvals should install its event listener as soon as
hello-ok completes, then call exec.approval.list to backfill requests that
predate the connection. Reconcile the list and live
exec.approval.requested / exec.approval.resolved events by approval ID so a
transition racing the list request is neither lost nor resurrected.
Track protocol versions
The current wire version is4. General operator and WebChat clients must
negotiate the exact current version with minProtocol: 4 and maxProtocol: 4.
Only authenticated node clients and lightweight probes have the N-1 acceptance
window, currently protocol 3 through 4.
Protocol changes are additive first. protocol.schema.json includes since
release-vintage metadata and required scope metadata for core methods, but a wire
version bump is still an explicit breaking event for third-party clients. Pin the
package versions you test, upgrade the client and Gateway together when the wire
version changes, and review the
OpenClaw changelog
before each upgrade.