OpenClaw Node.js Client

Node.js client for the OpenClaw Gateway WebSocket protocol. Build custom integrations.

openclaw-node is the first standalone Node.js client library for the OpenClaw Gateway WebSocket protocol, making it possible to connect any Node.js application to AI agents in just five lines of code. Built by the Pinchy team (heypinchy.com), it was extracted from their multi-user OpenClaw platform as a standalone utility — and as the creator candidly admits, it's also a 'Trojan Horse' that brings Pinchy brand awareness to the npm ecosystem. The client handles all the complexity of the OpenClaw Gateway protocol automatically: challenge-response handshake, token authentication, keepalive heartbeats, and automatic reconnection. It exposes a clean async/await API for streaming chat responses (via async iterators), synchronous one-shot messages, session management (list, history, send), and runtime configuration reading/patching. Streaming responses are typed — you get 'text', 'tool_use', 'tool_result', and 'done' chunks, making it easy to build rich UIs that show tool execution in real-time. The library requires Node.js 22+ for built-in WebSocket support, or Node.js 20-21 with the 'ws' package installed as a peer dependency. At ~430 weekly downloads, it serves developers building custom integrations, dashboards, chat UIs, and automation scripts that need to talk to OpenClaw programmatically rather than through messaging channels. The API surface covers chat, sessions, and config management — enough to build complete applications on top of OpenClaw. Ideal for developers building custom web apps, dashboards, or automation scripts that need to interact with OpenClaw agents. Also useful as a reference implementation for understanding the Gateway WebSocket protocol. Not a plugin you install into OpenClaw — it's a client library you use in your own applications.

Tags: sdk, client, developer

Use Cases

  • Building custom web dashboards that interact with OpenClaw agents
  • Automation scripts that send tasks to OpenClaw and process results programmatically
  • Custom chat UIs with real-time streaming and tool execution visibility
  • Integration bridges connecting OpenClaw to other platforms not covered by channel plugins
  • Testing and development tools for OpenClaw plugin/skill development
  • Multi-user platforms (like Pinchy) that wrap OpenClaw with additional features

Tips

  • Use chatSync() for simple scripts that just need a complete response, and chat() for UIs that want real-time streaming
  • Pass sessionKey to continue existing conversations — sessions persist on the gateway across connections
  • Use agentId to target specific agents if your gateway runs multiple agent configurations
  • The config.get() method returns a hash — use it as baseHash in config.patch() for optimistic locking
  • For production apps, handle the auto-reconnect events to show connection status in your UI
  • Study this library's source code as a reference implementation if you're building Gateway clients in other languages

Known Issues & Gotchas

  • This is NOT an OpenClaw plugin — don't try to install it with 'openclaw plugins install'. Use 'npm install openclaw-node' in your own project
  • Node.js 20-21 users MUST also install the 'ws' package: npm install openclaw-node ws
  • The client auto-reconnects by default (maxReconnectAttempts: 10) — disable with autoReconnect: false if you want manual control
  • Gateway auth token must match between client and server — if you get auth errors, check OPENCLAW_GATEWAY_TOKEN on both sides
  • Streaming responses are async iterators — you must use 'for await...of' syntax, not regular for loops
  • The 'done' chunk type signals stream completion — always handle it to know when the response is finished
  • chatSync() returns a plain string, not a stream — use chat() for real-time streaming

Alternatives

  • Direct WebSocket connection
  • OpenClaw REST API (Control UI)
  • OpenClaw Studio

Community Feedback

Almost forgot — we also extracted Pinchy's WebSocket bridge into a standalone npm package: openclaw-node. It's the first Node.js client for OpenClaw's Gateway protocol.

— Pinchy Blog

Let's be honest: it's a Trojan Horse. A genuinely useful utility that happens to live under the heypinchy GitHub org. Every npm install openclaw-node is a breadcrumb back here. Not sneaky — just strategic.

— Pinchy Blog

A Node.js client for OpenClaw — connect your app to AI agents in a few lines of code. OpenClaw is an open-source AI agent platform. This package lets you talk to those agents from Node.js.

— GitHub

Frequently Asked Questions

Is this an OpenClaw plugin?

No. openclaw-node is a client library you install in your own Node.js applications with 'npm install openclaw-node'. It connects TO OpenClaw, not inside it. Think of it like a database driver — you use it in your app to talk to OpenClaw.

What is Pinchy and why did they build this?

Pinchy (heypinchy.com) is a multi-user OpenClaw platform. They extracted their internal WebSocket bridge into this standalone package so other developers can build on OpenClaw too. The creator openly calls it a 'Trojan Horse' for Pinchy brand awareness — genuinely useful, strategically branded.

Why do I need Node.js 22+ or the 'ws' package?

Node.js 22 introduced a built-in WebSocket API. Earlier versions (20-21) don't have native WebSocket support, so you need the 'ws' package as a polyfill. The library detects which is available automatically.

Can I use this in the browser?

No. openclaw-node is designed for server-side Node.js only. For browser-based chat UIs, consider OpenClawChat or the built-in Control UI WebChat, which handle browser WebSocket connections with appropriate security.

Does this support all Gateway protocol features?

It covers the most common operations: chat (streaming and sync), session management (list, history, send), and configuration (get, patch, apply). More advanced features like direct tool invocation or file management are not exposed in the current API.

How does authentication work?

The client automatically handles the challenge-response handshake when connecting. If your gateway has auth enabled (OPENCLAW_GATEWAY_TOKEN), pass the same token to the client constructor. The protocol details are handled transparently.

Configuration Examples

Basic chat with streaming

import { OpenClawClient } from 'openclaw-node';

const client = new OpenClawClient({
  url: 'ws://localhost:18789',
  token: process.env.OPENCLAW_GATEWAY_TOKEN,
});

await client.connect();

for await (const chunk of client.chat('What is on my calendar today?')) {
  if (chunk.type === 'text') process.stdout.write(chunk.text);
  if (chunk.type === 'tool_use') console.log(`\n🔧 ${chunk.text}`);
}

await client.disconnect();

Session management

const sessions = await client.sessions.list({ limit: 10 });
console.log(sessions);

const history = await client.sessions.history('session-key', { limit: 20 });
for (const msg of history) {
  console.log(`[${msg.role}] ${msg.content}`);
}

Runtime config management

// Read current config with hash for optimistic locking
const { config, hash } = await client.config.get();
console.log(config);

// Patch config safely
await client.config.patch({ models: { default: 'anthropic/claude-sonnet-4-20250514' } }, hash);

Installation

npm install openclaw-node

Homepage: https://heypinchy.com