OpenClawSkills
GitHub
Gateway / 运用 • TutorialHeader.readTime

OpenAI Chat Completions(兼容API)

Gateway 从 OpenAI 兼容的 /v1/chat/completions HTTP 端点公开执行。

OpenClaw's Gateway can serve a small OpenAI-compatible Chat Completions endpoint.

This endpoint is disabled by default. Enable it in config first.

- ''POST /v1/chat/completions''

- 网关和同自端口(WS + HTTP 多重化):''http://<gateway-host>:<port>/v1/chat/completions''

Under the hood, requests are executed as a normal Gateway agent run (same codepath as ''openclaw agent''), so routing/permissions/config match your Gateway.

Tutorial.step

认证

Uses the Gateway auth configuration. Send a bearer token:

- ''Authorization: Bearer <token>''

注意:

- ''gateway.auth.mode="token"'' 的場合、''gateway.auth.token''(或 ''OPENCLAW_GATEWAY_TOKEN'')使用执行。

- ''gateway.auth.mode="password"'' 的場合、''gateway.auth.password''(或 ''OPENCLAW_GATEWAY_PASSWORD'')使用执行。

Tutorial.step

代理的选择

无需自定义标头:在 OpenAI ''model'' 字段中编码代理 ID:

- ''model: "openclaw:<agentId>"''(例:''"openclaw:main"''、''"openclaw:beta"'')

- ''model: "agent:<agentId>"''(别名)

或者通过标头指定特定的 OpenClaw 代理:

- ''x-openclaw-agent-id: <agentId>''(默认:''main'')

高度:

- ''x-openclaw-session-key: '''' 在会话路由完全在控制执行。

Tutorial.step

端点的启用

''gateway.http.endpoints.chatCompletions.enabled'' ''true'' 在设置执行:

Json5
{
  gateway: {
    http: {
      endpoints: {
        chatCompletions: { enabled: true },
      },
    },
  },
}
Tutorial.step

端点的禁用

''gateway.http.endpoints.chatCompletions.enabled'' ''false'' 在设置执行:

Json5
{
  gateway: {
    http: {
      endpoints: {
        chatCompletions: { enabled: false },
      },
    },
  },
}
Tutorial.step

会话动作

默认情况下,端点对每个请求是无状态的(每次调用都会生成新的会话密钥)。

如果请求包含 OpenAI ''user'' 字符串,Gateway 会从中派生一个稳定的会话密钥,因此重复调用可以共享代理会话。

Tutorial.step

流媒体 (SSE)

Set ''stream: true'' to receive Server-Sent Events (SSE):

- ''Content-Type: text/event-stream''

- 每个活动行是 ''data: <json>''

- Stream ends with ''data: [DONE]''

Tutorial.step

例

非流媒体:

Bash
curl -sS http://127.0.0.1:18789/v1/chat/completions \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  -H 'x-openclaw-agent-id: main' \
  -d {
    "model": "openclaw",
    "messages": [{"role":"user","content":"hi"}]
  }

流媒体:

Bash
curl -N http://127.0.0.1:18789/v1/chat/completions \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  -H 'x-openclaw-agent-id: main' \
  -d {
    "model": "openclaw",
    "stream": true,
    "messages": [{"role":"user","content":"hi"}]
  }