|
| 1 | +# FastClaw Upstream App Integration API |
| 2 | + |
| 3 | +This is the integration contract for upstream applications that want to use |
| 4 | +FastClaw as their Agent runtime. |
| 5 | + |
| 6 | +Use this document when building a SaaS, bot, marketplace, or workflow product |
| 7 | +that owns its own users but delegates agent execution, tools, memory, usage |
| 8 | +metering, quota checks, and optional channels to FastClaw. |
| 9 | + |
| 10 | +## Which Interface To Use |
| 11 | + |
| 12 | +| Need | Interface | Notes | |
| 13 | +|---|---|---| |
| 14 | +| End-user chat with an agent | `/v1/chat/completions` | OpenAI-compatible, plus FastClaw extensions. This is the primary upstream app API. | |
| 15 | +| List callable agents for an API key | `GET /v1/agents` | Respects API-key scope. | |
| 16 | +| Provision one upstream end-user | `POST /v1/users` | Optional. Chat can lazy-provision via `user` or `X-Fastclaw-End-User`. | |
| 17 | +| Query usage / token spend | `GET /v1/usage` | For upstream billing dashboards. | |
| 18 | +| Set paid-plan limits | `/v1/quota` | For subscription and entitlement enforcement. | |
| 19 | +| Admin/dashboard automation | `/api/*` or `fastclaw ...` CLI | Cookie/admin oriented, broader surface, not the minimal upstream app contract. | |
| 20 | +| Coding-agent live preview runtime | `docs/coding-agent-runtime.md` | Project/runtime endpoints are documented separately. | |
| 21 | + |
| 22 | +For a normal upstream product, start with `/v1/*`. Do not build against |
| 23 | +dashboard internals unless the product is also administering FastClaw. |
| 24 | + |
| 25 | +## Authentication |
| 26 | + |
| 27 | +Use an API key: |
| 28 | + |
| 29 | +```http |
| 30 | +Authorization: Bearer fcak_... |
| 31 | +``` |
| 32 | + |
| 33 | +API key types: |
| 34 | + |
| 35 | +| Type | Intended use | |
| 36 | +|---|---| |
| 37 | +| `admin` | Platform automation. Can operate broadly. Keep server-side only. | |
| 38 | +| `user` | App backend acting as one FastClaw owner account. Can use that owner's agents. | |
| 39 | +| `agent` | App backend scoped to explicit agent IDs. Recommended for single-agent integrations. | |
| 40 | + |
| 41 | +Do not expose FastClaw API keys in browsers or mobile apps. The upstream app |
| 42 | +backend should call FastClaw and map its own auth/session model to FastClaw |
| 43 | +`user` or `X-Fastclaw-End-User`. |
| 44 | + |
| 45 | +## Identity Model |
| 46 | + |
| 47 | +FastClaw has its own `user_id` because memory, sessions, usage, quotas, and |
| 48 | +per-user preferences need a stable isolation key. |
| 49 | + |
| 50 | +For upstream apps, there are two supported patterns: |
| 51 | + |
| 52 | +1. Explicit provisioning: |
| 53 | + |
| 54 | + ```http |
| 55 | + POST /v1/users |
| 56 | + Authorization: Bearer fcak_... |
| 57 | + Content-Type: application/json |
| 58 | +
|
| 59 | + { |
| 60 | + "external_id": "upstream-user-123", |
| 61 | + "display_name": "Alice" |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | + Response: |
| 66 | + |
| 67 | + ```json |
| 68 | + { |
| 69 | + "user_id": "u_...", |
| 70 | + "external_id": "upstream-user-123" |
| 71 | + } |
| 72 | + ``` |
| 73 | + |
| 74 | +2. Lazy provisioning on chat: |
| 75 | + |
| 76 | + Pass either: |
| 77 | + |
| 78 | + - body field: `"user": "upstream-user-123"` |
| 79 | + - header: `X-Fastclaw-End-User: upstream-user-123` |
| 80 | + |
| 81 | + FastClaw will create or reuse the app-user row for that API key. |
| 82 | + |
| 83 | +Use the upstream user's stable internal ID, not email or display name. That |
| 84 | +keeps identity stable if the user changes their profile. |
| 85 | + |
| 86 | +## Chat API |
| 87 | + |
| 88 | +### `POST /v1/chat/completions` |
| 89 | + |
| 90 | +OpenAI-compatible endpoint with FastClaw extensions. |
| 91 | + |
| 92 | +Required: |
| 93 | + |
| 94 | +- `messages`: array with at least one `user` message |
| 95 | + |
| 96 | +Agent selection: |
| 97 | + |
| 98 | +- Preferred: body field `agent_id` |
| 99 | +- Alternative: `X-Fastclaw-Agent-ID` header |
| 100 | +- If omitted, FastClaw resolves the default accessible agent for the caller |
| 101 | + |
| 102 | +Session selection: |
| 103 | + |
| 104 | +- Header: `X-Fastclaw-Session-Key` |
| 105 | +- If omitted, FastClaw creates an API session key for that turn |
| 106 | + |
| 107 | +Request: |
| 108 | + |
| 109 | +```http |
| 110 | +POST /v1/chat/completions |
| 111 | +Authorization: Bearer fcak_... |
| 112 | +Content-Type: application/json |
| 113 | +X-Fastclaw-Session-Key: chat-upstream-user-123-default |
| 114 | +
|
| 115 | +{ |
| 116 | + "agent_id": "agt_...", |
| 117 | + "model": "ignored-by-fastclaw-agent-config", |
| 118 | + "stream": true, |
| 119 | + "user": "upstream-user-123", |
| 120 | + "messages": [ |
| 121 | + { "role": "user", "content": "帮我总结今天的订单异常" } |
| 122 | + ], |
| 123 | + "params": { |
| 124 | + "tenant_id": "tenant_1", |
| 125 | + "locale": "zh-CN" |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +FastClaw extensions: |
| 131 | + |
| 132 | +| Field | Type | Purpose | |
| 133 | +|---|---|---| |
| 134 | +| `agent_id` | string | Agent to call. Body wins over header. | |
| 135 | +| `user` | string | Upstream end-user ID. Body wins over `X-Fastclaw-End-User`. | |
| 136 | +| `params` | object | Per-turn structured context shown to the agent. Not persisted. | |
| 137 | +| `images` | string[] | Image URLs/data URLs shown to vision models and materialized into workspace. | |
| 138 | +| `imageUrls` | string[] | Alias for `images`. | |
| 139 | +| `attachments` | array | General files materialized into workspace. Use for PDFs, docs, zips, etc. | |
| 140 | + |
| 141 | +Attachment shape: |
| 142 | + |
| 143 | +```json |
| 144 | +{ |
| 145 | + "attachments": [ |
| 146 | + { |
| 147 | + "url": "https://example.com/report.pdf", |
| 148 | + "name": "report.pdf" |
| 149 | + } |
| 150 | + ] |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +Streaming responses follow OpenAI SSE shape: |
| 155 | + |
| 156 | +```text |
| 157 | +data: {"id":"chatcmpl-...","object":"chat.completion.chunk","choices":[{"delta":{"content":"..."},"index":0}]} |
| 158 | +
|
| 159 | +data: [DONE] |
| 160 | +``` |
| 161 | + |
| 162 | +Non-streaming responses follow OpenAI response shape: |
| 163 | + |
| 164 | +```json |
| 165 | +{ |
| 166 | + "id": "chatcmpl-...", |
| 167 | + "object": "chat.completion", |
| 168 | + "model": "agent", |
| 169 | + "choices": [ |
| 170 | + { |
| 171 | + "index": 0, |
| 172 | + "message": { "role": "assistant", "content": "..." }, |
| 173 | + "finish_reason": "stop" |
| 174 | + } |
| 175 | + ], |
| 176 | + "usage": { |
| 177 | + "prompt_tokens": 0, |
| 178 | + "completion_tokens": 0, |
| 179 | + "total_tokens": 0 |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +### Session Key Guidance |
| 185 | + |
| 186 | +Choose a deterministic session key from your product model: |
| 187 | + |
| 188 | +```text |
| 189 | +<app-name>:<upstream-user-id>:<conversation-id> |
| 190 | +``` |
| 191 | + |
| 192 | +Do not reuse one session key for unrelated conversations. Session keys control |
| 193 | +chat history, memory extraction context, and usage grouping. |
| 194 | + |
| 195 | +## Agents |
| 196 | + |
| 197 | +### `GET /v1/agents` |
| 198 | + |
| 199 | +Lists only agents accessible to the API key. |
| 200 | + |
| 201 | +```http |
| 202 | +GET /v1/agents |
| 203 | +Authorization: Bearer fcak_... |
| 204 | +``` |
| 205 | + |
| 206 | +Response: |
| 207 | + |
| 208 | +```json |
| 209 | +{ |
| 210 | + "agents": [ |
| 211 | + { |
| 212 | + "id": "agt_...", |
| 213 | + "name": "agt_...", |
| 214 | + "model": "openai/gpt-4.1-mini" |
| 215 | + } |
| 216 | + ] |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +For provisioning agents, cloning templates, installing skills, or configuring |
| 221 | +providers, use dashboard `/api/*` endpoints or the `fastclaw` CLI. Those are |
| 222 | +operator/admin workflows, not the minimal end-user chat API. |
| 223 | + |
| 224 | +## Usage And Quotas |
| 225 | + |
| 226 | +### `GET /v1/usage` |
| 227 | + |
| 228 | +Returns token usage for the authenticated FastClaw user, or for a specific |
| 229 | +app-user when `user_id` is provided. |
| 230 | + |
| 231 | +Query params: |
| 232 | + |
| 233 | +| Param | Required | Notes | |
| 234 | +|---|---|---| |
| 235 | +| `days` | no | Default `30`, max `90`. | |
| 236 | +| `user_id` | no | FastClaw user ID returned by `/v1/users`. | |
| 237 | + |
| 238 | +```http |
| 239 | +GET /v1/usage?user_id=u_...&days=30 |
| 240 | +Authorization: Bearer fcak_... |
| 241 | +``` |
| 242 | + |
| 243 | +Response: |
| 244 | + |
| 245 | +```json |
| 246 | +{ |
| 247 | + "userId": "u_...", |
| 248 | + "days": 30, |
| 249 | + "daily": [ |
| 250 | + { |
| 251 | + "day": "2026-06-26", |
| 252 | + "agentId": "agt_...", |
| 253 | + "model": "gpt-4.1-mini", |
| 254 | + "inputTokens": 100, |
| 255 | + "outputTokens": 50, |
| 256 | + "cacheReadTokens": 0, |
| 257 | + "cacheCreationTokens": 0, |
| 258 | + "requestCount": 1 |
| 259 | + } |
| 260 | + ], |
| 261 | + "totals": { |
| 262 | + "inputTokens": 100, |
| 263 | + "outputTokens": 50, |
| 264 | + "cacheReadTokens": 0, |
| 265 | + "cacheCreationTokens": 0, |
| 266 | + "requestCount": 1 |
| 267 | + } |
| 268 | +} |
| 269 | +``` |
| 270 | + |
| 271 | +### `PUT /v1/quota` |
| 272 | + |
| 273 | +Sets paid-plan limits for a FastClaw user. |
| 274 | + |
| 275 | +```http |
| 276 | +PUT /v1/quota |
| 277 | +Authorization: Bearer fcak_... |
| 278 | +Content-Type: application/json |
| 279 | +
|
| 280 | +{ |
| 281 | + "user_id": "u_...", |
| 282 | + "monthly_token_limit": 5000000, |
| 283 | + "monthly_request_limit": 10000, |
| 284 | + "reset_day": 1 |
| 285 | +} |
| 286 | +``` |
| 287 | + |
| 288 | +Limits of `0` mean no limit for that dimension. `reset_day` must be `1..28`; |
| 289 | +invalid values are normalized to `1`. |
| 290 | + |
| 291 | +### `GET /v1/quota` |
| 292 | + |
| 293 | +```http |
| 294 | +GET /v1/quota?user_id=u_... |
| 295 | +Authorization: Bearer fcak_... |
| 296 | +``` |
| 297 | + |
| 298 | +Returns the configured quota and, when usage metering is enabled, current |
| 299 | +status. |
| 300 | + |
| 301 | +### `DELETE /v1/quota` |
| 302 | + |
| 303 | +```http |
| 304 | +DELETE /v1/quota?user_id=u_... |
| 305 | +Authorization: Bearer fcak_... |
| 306 | +``` |
| 307 | + |
| 308 | +Removes explicit quota and reverts the user to unlimited FastClaw-side quota. |
| 309 | + |
| 310 | +## Recommended Upstream Flow |
| 311 | + |
| 312 | +1. Operator creates/configures an agent in FastClaw. |
| 313 | +2. Operator creates an `agent` API key scoped to that agent. |
| 314 | +3. Upstream backend stores the API key server-side. |
| 315 | +4. When a product user starts: |
| 316 | + - call `POST /v1/users`, or use lazy provisioning via chat `user` |
| 317 | + - store the returned `user_id` if you need usage/quota lookups |
| 318 | +5. For each conversation: |
| 319 | + - send `POST /v1/chat/completions` |
| 320 | + - set `agent_id` |
| 321 | + - set a deterministic `X-Fastclaw-Session-Key` |
| 322 | + - set `user` to the upstream stable user ID |
| 323 | +6. On subscription changes: |
| 324 | + - call `PUT /v1/quota` |
| 325 | +7. For billing dashboards: |
| 326 | + - call `GET /v1/usage` |
| 327 | + |
| 328 | +## Error Shape |
| 329 | + |
| 330 | +FastClaw returns JSON errors compatible with OpenAI-style clients: |
| 331 | + |
| 332 | +```json |
| 333 | +{ |
| 334 | + "error": { |
| 335 | + "message": "agent not found", |
| 336 | + "type": "not_found_error" |
| 337 | + } |
| 338 | +} |
| 339 | +``` |
| 340 | + |
| 341 | +Common statuses: |
| 342 | + |
| 343 | +| Status | Meaning | |
| 344 | +|---|---| |
| 345 | +| `400` | Bad request body, missing messages, missing user_id, etc. | |
| 346 | +| `401` | Missing/invalid API key. | |
| 347 | +| `404` | Agent not found or not accessible by this API key. | |
| 348 | +| `429` | Rate limited or quota exceeded. | |
| 349 | +| `503` | Usage/quota subsystem not configured for that endpoint. | |
| 350 | + |
| 351 | +## What To Give An Agent |
| 352 | + |
| 353 | +When asking an AI coding agent to integrate an upstream app with FastClaw, |
| 354 | +give it: |
| 355 | + |
| 356 | +1. This document. |
| 357 | +2. The FastClaw base URL. |
| 358 | +3. API key type and scope. |
| 359 | +4. Agent ID. |
| 360 | +5. Your upstream user ID field. |
| 361 | +6. Your conversation/session ID field. |
| 362 | +7. Whether you need usage/quota billing integration. |
| 363 | + |
| 364 | +If the agent supports Skills, install or load: |
| 365 | + |
| 366 | +```text |
| 367 | +skills/fastclaw-api-integration/SKILL.md |
| 368 | +``` |
| 369 | + |
| 370 | +That skill is the short operational version of this API contract. |
0 commit comments