OpenClaw API Guide: Authentication and Examples

Call your OpenClaw assistant from your own code. This guide shows how to create a gateway token, switch on the OpenAI-compatible HTTP endpoints, and send your first request with curl, Python or JavaScript.

Base URL
http://127.0.0.1:18789
Auth
Authorization: Bearer <token>
Format
JSON, OpenAI-compatible
Streaming
Server-Sent Events

Updated · Based on the official OpenClaw gateway documentation.

01 · Overview

What the OpenClaw API is

Everything in OpenClaw runs through the gateway, a local server that listens on port 18789 by default. The Control UI, the CLI and the mobile apps talk to it over WebSocket. The same port also serves a small set of HTTP endpoints you can call from scripts, apps and other services.

The chat endpoints follow the OpenAI format, so most OpenAI client libraries work by changing the base URL. The difference is that the model field picks one of your OpenClaw agents, not a provider model. Your agent then uses whichever model, skills and memory you've configured.

EndpointWhat it doesDefaultCredential
POST/v1/chat/completionsSend messages to an agent, OpenAI Chat Completions formatOffGateway token
POST/v1/responsesOpenResponses format with session reuse and client toolsOffGateway token
GET/v1/modelsList the agents you can targetWith either aboveGateway token
POST/v1/embeddingsCreate embeddingsWith either aboveGateway token
POST/tools/invokeRun one tool directly, without a chat turnOnGateway token
POST/hooks/agent, /hooks/wakeLet other services trigger your assistantOffSeparate hook token
WSws://127.0.0.1:18789Control UI, CLI and apps; full protocolOnToken + device pairing
02 · Authentication

How OpenClaw API authentication works

The gateway supports four auth modes, set with gateway.auth.mode in ~/.openclaw/openclaw.json. For API access, use token mode.

tokenA long random secret. Send it as Authorization: Bearer <token>.Recommended
passwordSame header, but the value is your gateway password.
trusted-proxyAn identity-aware proxy in front of the gateway adds identity headers.
noneNo auth. Only for private networks you fully control.

Step 1: Create a gateway token

Let OpenClaw generate a strong token for you:

Terminal
openclaw doctor --generate-gateway-token

Or set your own value:

Terminal
openclaw config set gateway.auth.token "$(openssl rand -hex 32)"

Step 2: Keep the token out of your code

Store it in an environment variable. OpenClaw itself also reads OPENCLAW_GATEWAY_TOKEN, so the same name works on the gateway host and in your scripts.

Shell
export OPENCLAW_GATEWAY_TOKEN="paste-your-token-here"

Step 3: Send it with every request

HTTP header
Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN
Gateway token vs model API keys

Don't mix them up. The gateway token lets your code talk to OpenClaw. Model provider keys, like ANTHROPIC_API_KEY or OPENAI_API_KEY, let OpenClaw talk to an AI model. Put provider keys in ~/.openclaw/.env and check them with openclaw models status. See our models guide.

03 · Setup

Enable the HTTP API

The OpenAI-compatible endpoints are off by default. Turn on the ones you need in ~/.openclaw/openclaw.json, then restart the gateway.

~/.openclaw/openclaw.json (JSON5)
{
  gateway: {
    auth: { mode: "token" },
    http: {
      endpoints: {
        chatCompletions: { enabled: true },
        responses: { enabled: true },
      },
    },
  },
}

Check it works by listing your agents:

curl
curl http://127.0.0.1:18789/v1/models \
  -H "Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN"
04 · Example

Your first request

Send a message to your default agent. Pick a language:

curl
curl http://127.0.0.1:18789/v1/chat/completions \
  -H "Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openclaw/default",
    "messages": [
      {"role": "user", "content": "What is on my calendar tomorrow?"}
    ]
  }'

Agent turns can take a while when your assistant uses tools, so set a generous client timeout.

05 · Example

Stream the reply

Add "stream": true to receive the answer as Server-Sent Events while the agent is still writing.

curl
curl -N http://127.0.0.1:18789/v1/chat/completions \
  -H "Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openclaw/default",
    "stream": true,
    "messages": [{"role": "user", "content": "Draft a reply to my last email"}]
  }'
06 · Routing

Choose an agent and keep a conversation

The model field chooses which OpenClaw agent answers:

model valueGoes to
openclaw or openclaw/defaultYour default agent
openclaw/<agentId>A specific agent, for example openclaw/work
agent:<agentId>Same as above, kept for compatibility

Requests are stateless by default, so each call starts a fresh session. To keep context across calls, send the same user value each time and OpenClaw will route them to one stable session:

curl
curl http://127.0.0.1:18789/v1/chat/completions \
  -H "Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openclaw/work",
    "user": "crm-integration",
    "messages": [{"role": "user", "content": "Remember: the Q4 review is on Friday."}]
  }'
07 · Example

Use the Responses API

POST /v1/responses follows the OpenResponses format. It accepts input and instructions, supports your own function tools, and lets you continue a thread with previous_response_id.

curl
curl http://127.0.0.1:18789/v1/responses \
  -H "Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openclaw/default",
    "instructions": "Answer in two sentences.",
    "input": "Summarize my unread messages from today."
  }'

To follow up in the same session, pass the id from the previous reply as previous_response_id.

08 · Example

Invoke a tool directly

POST /tools/invoke runs a single tool without a chat turn. It is on by default and obeys the same tool policy as your agents. High-risk tools such as exec, shell, fs_write and fs_delete are always blocked here.

curl
curl http://127.0.0.1:18789/tools/invoke \
  -H "Authorization: Bearer $OPENCLAW_GATEWAY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"tool": "sessions_list", "action": "json", "args": {}}'

Optional fields: agentId, sessionKey, idempotencyKey and dryRun. Use "dryRun": true to check a call before it runs.

09 · Automation

Trigger OpenClaw with webhooks

Webhooks let services like GitHub, Stripe or a form tool start an agent turn. They use their own token, separate from the gateway token, so a leaked webhook secret can't be used to reach the rest of the API.

~/.openclaw/openclaw.json (JSON5)
{
  hooks: {
    enabled: true,
    token: "<long-random-hook-token>",
    path: "/hooks",
    allowedAgentIds: ["main"],
    allowRequestSessionKey: false,
  },
}

Send the hook token as Authorization: Bearer or x-openclaw-token. Tokens in the query string (?token=) are rejected.

Run an agent turn
curl http://127.0.0.1:18789/hooks/agent \
  -H "Authorization: Bearer $OPENCLAW_HOOK_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-10482" \
  -d '{
    "message": "New order #10482 received. Send me a one-line summary.",
    "name": "Shop orders",
    "agentId": "main",
    "deliver": true
  }'

# Response: {"ok": true, "runId": "..."}

Useful /hooks/agent fields: message (required), agentId, channel, to, deliver, sessionKey and waitForCompletion. Add an Idempotency-Key header so retries don't run twice.

10 · Troubleshooting

Common errors

401
Unauthorized

The token is missing or wrong. Check the Authorization: Bearer header and that you used the gateway token, not a model API key or the hook token.

403
Forbidden

Tool policy blocked the call, or the agent isn't allowed. Check tools.allow and, for webhooks, allowedAgentIds.

404
Not found

The endpoint isn't enabled in gateway.http.endpoints, or the tool you named isn't available.

400
Bad request

The JSON body is invalid or a required field is missing, like messages or message.

ECONNREFUSED
Can't connect

The gateway isn't running or is bound to a different address. Start it and check the port in your config.

More fixes on our troubleshooting page.

11 · Security checklist

A gateway token is a master key

OpenClaw's docs describe these endpoints as full operator access. Anyone holding a valid token can act as the owner of your assistant, so protect it like a password to your whole computer.

  • Keep the gateway on loopback, a tailnet or private network. Never expose it straight to the internet.
  • Use a long random token and rotate it if it might have leaked.
  • Give webhooks their own hook token and limit allowedAgentIds.
  • Only enable the HTTP endpoints you actually use.
  • Never commit tokens to Git or paste them into shared chats.
  • Use wss:// and TLS when connecting remotely.

Read the full OpenClaw security guide.

12 · FAQ

OpenClaw API questions

Does OpenClaw have an API?

Yes. The OpenClaw gateway serves OpenAI-compatible HTTP endpoints, including /v1/chat/completions and /v1/responses, plus /tools/invoke, inbound webhooks and a WebSocket protocol. All run on the gateway port, 18789 by default.

Where do I find my OpenClaw API key?

OpenClaw uses a gateway token rather than an API key. Generate one with openclaw doctor --generate-gateway-token, or set your own with openclaw config set gateway.auth.token. It is stored in ~/.openclaw/openclaw.json.

Can I use the OpenAI SDK with OpenClaw?

Yes. Point the SDK's base URL at http://127.0.0.1:18789/v1, pass your gateway token as the API key, and set the model to openclaw/default or openclaw/<agentId>.

Why do I get a 404 from /v1/chat/completions?

The endpoint is off by default. Set gateway.http.endpoints.chatCompletions.enabled to true in your config and restart the gateway.

Is it safe to expose the OpenClaw API to the internet?

No. A valid token gives full operator access. Keep the gateway on localhost, a private network or a tailnet, and use a VPN or tunnel for remote access.

Keep reading