Management API#

Management API は Workspace、Environment、Client、エンドユーザーなどをサーバーから管理する API である。利用には Management API key または Dashboard session を使う。

接続と認証#

本番の base URL は https://api.zeroword.smartcrab.ai/v1 である。Management API key は sk_live_... 形式のみが発行される。Authorization: Bearer sk_live_… で送る。API key はサーバー側にだけ保存し、ブラウザーやモバイルアプリへ配布しない。

curl 'https://api.zeroword.smartcrab.ai/v1/workspaces?limit=10' \
  -H 'Authorization: Bearer sk_live_…'

API key の発行・失効#

Dashboard の Workspace メニュー「API キー」(/w/<workspaceId>/api-keys) から発行するか、POST /v1/api-keys を呼び出す (以降の endpoint 表では /v1 を省略する)。作成レスポンスの secret は一度だけ表示されるため、その場で安全な secret store に保存する。GET /api-keys と GET /api-keys/{apiKeyId} は secret を返さない。期限を expires_at で指定でき、POST /api-keys/{apiKeyId}/revoke で失効させる。期限のない key は expires_at: null になる。失効済みの key も GET /api-keys/{apiKeyId} で取得でき、revoked_at に失効時刻が入る。

API key は発行時に scopes を明示する。Environment を environment_id に指定するとその Environment に固定され、指定しなければ同じ Workspace 内で使える Workspace-scoped key となる。key の workspace_id は固定され、key 自身がその Workspace context になる。Environment-pinned key は指定 Environment 内の操作に限られ、Workspace / Project の操作や billing:* など Workspace 全体を対象とする scope は利用できない。Project 作成 (POST /projects) と新しい Environment 作成 (POST /environments) も不可である。Workspace 全体を対象とする scope を含む Environment-pinned key は発行できず、該当 request は 403 insufficient_scope となる。

Method / path 必須 field・query Scope
GET /api-keys query: workspace_id; 任意: environment_id, limit, cursor api_keys:read
GET /api-keys/{apiKeyId} path: apiKeyId (mky_...) api_keys:read; Environment-pinned key からは自 Environment の key 以外は 404
POST /api-keys body: workspace_id, name, 1 件以上の scopes; 任意: environment_id, expires_at api_keys:write
POST /api-keys/{apiKeyId}/revoke path: apiKeyId (mky_...) api_keys:write

API key 発行時と一覧取得時の workspace_id は認証 context の Workspace と一致させる。一致しない場合は 400 invalid_request、detail は workspace_id must match X-Workspace-Id となる。Dashboard session の API 呼び出しでは X-Workspace-Id: <workspaceId> が必須であり、選択中の Workspace として照合される。一方、API key の呼び出しでは key の固定 Workspace が使われ、X-Workspace-Id は不要であり無視される。Dashboard session では header を省略しない。

Scopes#

Scope は次のとおりである。必要最小限のものを発行する。:write は read を暗黙に含まない。secret rotation、credential 操作、user delete などは個別 scope を要求する。

  • Workspace: workspaces:read, workspaces:write, workspace_members:read
  • Project: projects:read, projects:write
  • Environment: environments:read, environments:write
  • Client: clients:read, clients:write, clients:rotate_secret
  • Connection: connections:read, connections:write
  • Domain: domains:read, domains:write
  • User: users:read, users:write, users:block, users:delete
  • User identities: user_identities:read, user_identities:write
  • User passkeys: user_passkeys:read, user_passkeys:write
  • User sessions: user_sessions:read, user_sessions:write
  • Import/export job: user_data_jobs:read, user_data_jobs:write
  • Webhook: webhooks:read, webhooks:write, webhooks:rotate_secret, webhooks:test
  • DLQ: dlq:read, dlq:write
  • Audit/usage: audit_events:read, usage:read
  • OAuth token introspection: introspect (POST /oauth2/introspect に X-Management-API-Key: <secret> として提示する。Environment-scoped key は issuer の Environment と environment_id が一致する場合に有効。environment_id を省略した Workspace-scoped key は同じ Workspace の Environment で使える。Scope 不足または Environment 不一致は {"active":false} を返す。Introspection を参照)
  • Billing: billing:read, billing:write
  • API key: api_keys:read, api_keys:write
  • Impersonation: impersonation:create (Dashboard session の Owner/Admin のみ利用可能)

Scope が足りない認証済みのリクエストは 403 / insufficient_scope となる。

共通仕様#

  • List route は limit / cursor による cursor pagination を使う。limit の既定値は 25、最大値は 100。続きがある場合は next_cursor、最後は null が返る。
  • OpenAPI で Idempotency-Key を受け付ける write route では、同じ Workspace、method、route、key、canonical JSON body の組み合わせで再送すると最初の response を返す。同じ key を method、route、body のいずれかが異なる request に使うと 409 idempotency_key_conflict となる。key は 1〜255 文字であり、指定した key が空または 256 文字以上の場合は 400 invalid_request となる。header の省略時は idempotency replay を行わない。@smartcrab/management-client は write ごとに UUID を生成し、WriteOptions.idempotencyKey で上書きできる。Environment settings、Theme、Impersonation の各 route は idempotency に対応しない。
  • ETag を返す単体 resource の更新・削除は If-Match で条件付きにできる。ETag 不一致は 412 precondition_failed。Theme の PUT も If-Match を使う。
  • 成功・エラーの全レスポンスに X-Request-Id が付く。ETag 対応レスポンスには ETag も付く。
  • Error body は RFC 9457 Problem Details (application/problem+json) で、type, title, status, code, request_id と、あれば detail を含む。一般的な例は認証失敗 401, Scope 不足 403, 不存在 404, state/unique conflict 409, ETag 不一致 412, 期限切れ download 410, rate limit 429 である。429 と 5xx は一時的な失敗として扱い、write を再試行するときは同じ Idempotency-Key を使う。
  • Resource 作成 quota は、platform user が Owner である Workspace 5 件、Workspace あたり Project 20 件、Project あたり Environment 10 件、Environment あたり Client 50 件 / Connection 20 件 / Webhook 20 件、Workspace あたり未失効かつ未期限切れの Management API key 50 件。削除・失効・期限切れから24時間以内の row は quota に算入し、24時間を過ぎたものは除外する。上限到達は HTTP 409 Problem Details (quota_exceeded) で、detail は "<resource> limit of N reached" 形式。Idempotency-Key の replay は quota check より優先し、作成済み resource の元の response を返す。
  • Management API の rate limit は、API key で認証した全 request に対して key あたり 1 分 600 件である。Dashboard session はこの per-key 上限に算入されない。別途 GET /users は search の有無にかかわらず request ごとに Workspace あたり 1 分 120 件であり、GET /users/{userId} は対象外。API key で認証した GET /users は両方の上限に算入される。上限到達時は 429 Problem Details (rate_limited) を返し、retry_after_seconds: 60 と Retry-After: 60 が含まれる。

全 path は base URL の /v1 に続けて指定する。表中の field は主要な絞り込み・作成 field であり、完全な request/response schema は specs/management-api.openapi.yaml にある。

Resource reference#

Workspaces / members#

GET /workspaces は Dashboard session では member になっている Workspace を返し、Workspace-scoped API key では key の Workspace だけを返す。POST /workspaces は Dashboard session 専用で、作成者が Owner になる。Management API key ではこの route を使えない。

Method / path 必須 field・query Scope / access
GET /workspaces 任意: limit, cursor, status workspaces:read
POST /workspaces body: name; 任意: slug Dashboard session のみ
GET /workspaces/{workspaceId} path: workspaceId workspaces:read
PATCH /workspaces/{workspaceId} body: name, slug のいずれか workspaces:write
GET /workspaces/{workspaceId}/members path: workspaceId; 任意: limit, cursor workspace_members:read

Workspace 更新で status は変更できない。Member response は platform_user_id, role, created_at, updated_at のみを含み、credential は含まない。

Projects#

Method / path 必須 field・query Scope
GET /projects query: workspace_id; 任意: limit, cursor projects:read
POST /projects body: workspace_id, name; 任意: slug projects:write
GET /projects/{projectId} path: projectId projects:read
PATCH /projects/{projectId} body: name, slug のいずれか projects:write
DELETE /projects/{projectId} path: projectId projects:write

query/body の workspace_id は認証 context の Workspace と一致させる。DELETE は Project の論理削除であり、non-deleted Environment が残る間は 409 project_has_live_environments を返す。成功 response は { "id": "…", "deleted": true }。

Environments / settings / theme / RP migration#

Environment の issuer, interaction_domain, rp_id は ID とプラットフォーム domain から導出され、create body では指定しない。

Method / path 必須 field・query Scope
GET /environments query: project_id; 任意: type, limit, cursor environments:read
POST /environments body: project_id, type (test / production), name environments:write
GET /environments/{environmentId} path: environmentId environments:read
PATCH /environments/{environmentId} body: name, status (active / suspended) のいずれか environments:write
DELETE /environments/{environmentId} path: environmentId environments:write
GET /environments/{environmentId}/settings — environments:read
PATCH /environments/{environmentId}/settings body: impersonation_enabled environments:write; If-Match / Idempotency-Key 非対応
GET /environments/{environmentId}/theme — environments:read
PUT /environments/{environmentId}/theme body: theme schema object; If-Match environments:write; Idempotency-Key 非対応
GET /environments/{environmentId}/theme/preview-css — environments:read
GET /environments/{environmentId}/rp-migration — environments:read
POST /environments/{environmentId}/rp-migration/disable-legacy body: confirm_rp_id environments:write

Environment の DELETE は status を deleted にする。PATCH では deleted にできない。RP migration の disable-legacy は不可逆で、confirm_rp_id は旧 RP ID に一致させる必要がある。Theme schema と RP ID・Hosted Login の関係は Hosted Login を参照する。

Clients#

Method / path 必須 field・query Scope
GET /clients query: environment_id; 任意: client_type, limit, cursor clients:read
POST /clients body: environment_id, client_type, name, token_endpoint_auth_method; redirect/origin/scope/resource などの設定と require_nonce (既定値 true) は任意 clients:write
GET /clients/{clientId} path: clientId clients:read
PATCH /clients/{clientId} body: name, redirect_uris, post_logout_redirect_uris, allowed_origins, allowed_scopes, allowed_resources, token_endpoint_auth_method, require_pkce, require_nonce, status, mobile_app のいずれか clients:write
DELETE /clients/{clientId} path: clientId clients:write
POST /clients/{clientId}/rotate-secret path: clientId clients:rotate_secret

client_type は web_bff, web_spa, react_native。token_endpoint_auth_method が none 以外の Client では client_secret を作成または rotation の response で一度だけ表示する。public client (none) には secret を発行しない。List/get/update response は secret を返さない。 PATCH の body field はすべて任意である。allowed_resources は HTTPS URL を最大 20 件まで指定できる。mobile_app は react_native Client の設定 object で、app_scheme、ios_bundle_id、ios_team_id、android_package_name、android_sha256_fingerprints、universal_link_host を持つ。null を指定すると設定を解除する。require_pkce の値にかかわらず、Authorization Server は全 Client に PKCE S256 を要求する。Client の require_nonce は create 時に true が既定で、response には常に含まれる。false にすると openid scope でも nonce を省略できるが、state、PKCE S256、登録済み redirect URI は引き続き必須である。

Connections: social providers / Managed と BYO#

Method / path 必須 field・query Scope
GET /connections query: environment_id; 任意: provider, limit, cursor connections:read
POST /connections body: environment_id, provider, mode, provider_key, provider_client_id, provider_client_secret; generic OIDC では issuer connections:write
GET /connections/{connectionId} path: connectionId connections:read
PATCH /connections/{connectionId} issuer, provider_client_id, provider_client_secret, scopes, profile_mapping, status, owner_confirmed のいずれか connections:write
DELETE /connections/{connectionId} path: connectionId connections:write

provider は google, apple, github, microsoft, generic_oidc。mode: "managed" は Zeroword の共有 OAuth application を使い、mode: "byo" は customer-owned provider app の Client ID/Secret を使う。provider_client_secret は create/update の write-only input であり、response に一切含まれない。generic_oidc は Workspace Owner が Dashboard で確認するか、connections:write Scope を持つ caller が PATCH /connections/{connectionId} に {"owner_confirmed": true} を送信するまで login に利用できない。issuer を変更すると確認状態が解除される。同じ PATCH で owner_confirmed: true を指定した場合は、その変更が明示的な確認となる。

Domains#

Method / path 必須 field・query Scope
GET /domains query: environment_id; 任意: status, limit, cursor domains:read
POST /domains body: environment_id, hostname; 任意: rp_enabled domains:write
GET /domains/{domainId} path: domainId domains:read
PATCH /domains/{domainId} body: rp_enabled domains:write
DELETE /domains/{domainId} path: domainId domains:write
POST /domains/{domainId}/retry-validation path: domainId domains:write

作成直後は pending / ownership_verified: false であり、response の verification_record_name と verification_record_value の TXT が一致して初めて Cloudflare for SaaS hostname と Worker route が作成される。未検証の Domain は DELETE / Environment 削除時に Cloudflare 呼び出しを行わない。hostname は2つ以上の LDH label を持つ DNS hostname とし、IP literal と Cloudflare zone (Production/Staging は smartcrab.ai) およびその配下は登録できない。上限は Environment あたり未削除5件、Workspace あたり未削除10件、Cell あたり provision 済み hostname 900件であり、超過時は 409 quota_exceeded を返す。別の verified Domain が hostname を保持する場合は 409 hostname_conflict となる。Domain の custom host、Hosted Login、RP ID への影響は Hosted Login を参照する。

Users / identities / passkeys / sessions#

全ての user route は environment_id query が必要である。GET /users は status / search で絞り込みできる。

Method / path 必須 field・query Scope
GET /users query: environment_id; 任意: status, search, limit, cursor users:read
POST /users body: environment_id, email; 任意: email_verified, name, avatar_url, locale, timezone, metadata users:write
GET /users/{userId} path: userId; query: environment_id users:read
PATCH /users/{userId} query: environment_id; profile / metadata fields users:write
DELETE /users/{userId} query: environment_id; If-Match 任意 users:delete
POST /users/{userId}/block query: environment_id; body の reason は任意 users:block
POST /users/{userId}/recovery query: environment_id; body: reason (trim 後 1〜1000 文字); Idempotency-Key 必須 Owner/Admin の fresh Dashboard session
POST /users/{userId}/unblock query: environment_id users:block
GET /users/{userId}/identities query: environment_id, limit, cursor user_identities:read
DELETE /users/{userId}/identities/{identityId} query: environment_id user_identities:write
GET /users/{userId}/passkeys query: environment_id, limit, cursor user_passkeys:read
DELETE /users/{userId}/passkeys/{passkeyId} query: environment_id user_passkeys:write
GET /users/{userId}/sessions query: environment_id, limit, cursor user_sessions:read
DELETE /users/{userId}/sessions/{sessionId} query: environment_id user_sessions:write
DELETE /users/{userId}/sessions query: environment_id user_sessions:write

User の DELETE は pending_deletion への移行である。PATCH /users/{userId} では email を変更できない。Identity / passkey / session response は secret credential や private key material を含まない。

POST /users/{userId}/recovery は Owner/Admin の Dashboard access-token session 専用で、auth_time が直近 300 秒以内である必要があり、refresh-only session は利用できない。API key と Support-only role は使用できない。environment_id の Environment と対象 User は呼び出し元の Workspace に属していなければならない。User に既登録の verified email OTP または linked social identity がない場合は 409 recovery_factor_not_enrolled を返す。

開始時に全 session / refresh family を失効し、必須の recovery.initiated audit event に actor、subject、reason、operation ID を記録する。メールアドレスや credential は変更・登録しない。User は既に登録済みの verified email OTP または linked social identity でサインインし、その後に新しい passkey を登録する。新しい OTP や social challenge は発行しない。成功時は固定案内を含む 200 {"initiated":true,"instructions":"Sign in using an already-enrolled verified email OTP or linked social identity, then register a new passkey."} を返す。

User import / export jobs#

Method / path 必須 field・query Scope
POST /user-import-jobs body: environment_id, format (json / csv), source_url (HTTPS) user_data_jobs:write
GET /user-import-jobs/{jobId} path: jobId user_data_jobs:read
GET /user-import-jobs/{jobId}/errors path: jobId user_data_jobs:read
POST /user-export-jobs body: environment_id, target_user_id user_data_jobs:write
GET /user-export-jobs/{jobId} path: jobId user_data_jobs:read
GET /user-export-jobs/{jobId}/download path: jobId user_data_jobs:read

POST response は job resource (pending, processing, completed, failed) を返す。Import source の fetch や export artifact 作成はこの request 中に実行されるため、返却時点ですでに completed または failed の場合がある。成功した export の download_url は期限付きである。Import error report と export download route は Management API key/session で認証し、user_data_jobs:read scope を要求する。

Webhooks / deliveries / test#

Method / path 必須 field・query Scope
GET /webhooks query: environment_id; 任意: limit, cursor webhooks:read
POST /webhooks body: environment_id, url, 1 件以上の event_types webhooks:write
GET /webhooks/{webhookId} path: webhookId webhooks:read
PATCH /webhooks/{webhookId} body: url, event_types, status のいずれか webhooks:write
DELETE /webhooks/{webhookId} path: webhookId webhooks:write
GET /webhooks/{webhookId}/deliveries path: webhookId; 任意: status, occurred_after, occurred_before, limit, cursor webhooks:read
POST /webhooks/{webhookId}/test body の event_type は任意 webhooks:test
POST /webhooks/{webhookId}/rotate-secret path: webhookId webhooks:rotate_secret

作成と secret rotation では signing secret を response で一度だけ表示する。List/update/delete/deliveries から secret は取得できない。Delivery history は 90 日保持され、body/header/signature は含まず status、attempt、response status code、duration などを返す。Event envelope、署名検証、再送については Webhooks を参照する。

event_types は既知の 14 型から選ぶ空でない配列で、未知の型は拒否される。POST /webhooks と PATCH /webhooks/{webhookId} で重複した型を指定すると 400 invalid_request となる。

DLQ records#

Dead-letter queue (DLQ) には、source queue で再試行を使い切った、または consumer が受け付けなかったメッセージが Workspace 単位で保存される。Dashboard の DLQ 画面と本 API から閲覧・再送できる (design.md §29.3)。

Method / path 必須 field・query Scope
GET /dlq/records 任意: queue, event_type, environment_id, status, failed_after, failed_before, limit, cursor dlq:read
GET /dlq/records/{recordId} path: recordId (dlq_...) dlq:read
POST /dlq/records/{recordId}/redrive path: recordId; 任意: Idempotency-Key ヘッダ。body なし dlq:write

Dashboard の role では、閲覧 (dlq:read) を Owner / Admin / Developer / Viewer、再送 (dlq:write) を Owner / Admin が行える。Developer は dlq:read のみ持ち、Billing / Audit を含む任意の source queue を再送できない。API key は dlq:write scope がある場合に許可する。

Environment-scoped API key には環境なしの record は見えず、再送もできない。impersonation 中の一覧は impersonated Environment に固定され、別 Environment を指定すると 403 になる。Environment に紐づかない Workspace record は impersonation 中に詳細取得・再送できない。

詳細の payload は秘密情報をマスクした元メッセージ本文である (design.md §32.6)。OTP、token、cookie、secret、code、private key、assertion、password、signature に当たるキーの値は [redacted] に置換し、メールアドレス形式の文字列はマスクする。保存本文が 64 KiB で切り詰められている場合、または JSON object でない場合は payload が null になる。payload_truncated は切り詰めの有無、redrivable は本文が同じ queue で再送可能な完全なメッセージかどうかを示す。

Record ID (dlq_...) は不透明な値である。他 Workspace の record は存在しない扱いとし、404 not_found を返す。

再送の成功応答は outcome で区別する。

HTTP outcome 意味
202 enqueued 元メッセージを source queue へ再投入した。元のイベント ID を保持するため、consumer の重複排除が効く。
200 already_delivered webhook event は配信済み (delivered) のため送信しない。
200 already_redriven 既に再送済みのため送信しない。再び dead-letter されるまで再投入されない。

再送のエラーは次のとおり。

HTTP code 意味
403 insufficient_scope dlq:write scope がない。Environment 固定 key と record の Environment が一致しない場合も含む。
403 impersonation_forbidden_action impersonation 中に Workspace-level の record (Environment なし) を詳細取得・再送しようとした。
404 not_found record が存在しない、または他 Workspace の record。
409 dlq_redrive_in_progress 別の再送が lease (120 秒) を保持している。時間をおいて再試行する。
409 dlq_record_not_redrivable 本文が切り詰められている、または source queue の message として不正。状態は変わらない。
503 dlq_redrive_unavailable Queue 送信または durable audit 保存を完了できない。Queue send が失敗した場合は再試行のため record を解放するが、Cloudflare Queues は at-least-once のため、エラー応答前に Queue が受理していれば重複投入されることがある。durable audit または完了 CAS の失敗後は redriving lease が期限切れになるまで待って再試行する。Webhook claim と各 consumer の event-id dedupe が副作用の重複を防ぐ。

成功した各再送 (既に再送済みの idempotent response を含む) は、本文を含めない redacted な監査 object を EVENTS_BUCKET の dlq-records/<workspaceId>/_audit/<eventId>.json に append する。object には認証済み actor、Workspace、source queue、record/source-event ID、時刻、outcome を含む。閲覧は privileged R2 access に限り、Management API からは公開しない。Environment-scoped record では、互換性のため audit-events Queue に dlq.redriven も送る。

Audit events#

Method / path 必須 field・query Scope
GET /audit-events query: environment_id; 任意: actor_type, action, subject_type, subject_id, result, created_after, created_before, limit, cursor audit_events:read

Audit event は追記専用で、Management API から update/delete できない。Response に内部 routing ID や credential は含まれない。

Usage / MAU#

Method / path 必須 field・query Scope
GET /usage/mau 任意: billing_month (YYYY-MM), environment_id usage:read

Workspace の MAU 集計、30,000 MAU free tier、threshold 到達状況、billing / grace-period state を返す。environment_id を指定するとその Environment の値に絞る。

Billing#

Method / path 必須 field・query Scope / access
GET /billing/notifications 任意: limit, cursor billing:read; Dashboard session は Owner / Billing
POST /billing/checkout-session body: success_url, cancel_url (HTTPS) billing:write
POST /billing/portal-session body: return_url (HTTPS) billing:write

GET /billing/notifications は Workspace の永続 inbox を新しい順に返す。API key は billing:read scope が必要であり、Dashboard session は Owner または Billing role のみ利用できる。limit は省略時 25、最大 100。続きの取得には response の next_cursor を cursor に指定し、最終 page は null となる。不正な cursor は 400 invalid_request。

成功 response は { "items": [...], "next_cursor": "..." }。各 item の field は notification_key, kind, billing_month, threshold_percent, current_mau, free_tier_limit, grace_ends_at, restriction_days, created_at である。created_at は通知作成時刻の ISO 8601 timestamp。例:

{
  "items": [
    {
      "notification_key": "…",
      "kind": "mau_threshold_reached",
      "billing_month": "2026-10",
      "threshold_percent": 95,
      "current_mau": 28500,
      "free_tier_limit": 30000,
      "grace_ends_at": null,
      "restriction_days": null,
      "created_at": "2026-10-16T12:00:00.000Z"
    }
  ],
  "next_cursor": null
}

mau_threshold_reached は対象月 (billing_month) の free-tier MAU limit に対する到達率 (threshold_percent: 80 / 95 / 100)、現在の MAU、limit を示す。grace_period_started は猶予終了日時を grace_ends_at で示し、grace_period_expired は猶予終了を示す。payment_failed / payment_recovered は支払い失敗 / 回復、payment_restricted_new_users は支払遅延14日での新規ユーザー作成制限、payment_restricted_config は30日での設定変更制限を示す。後者2つの restriction_days は escalation 時点の日数 (14 / 30)。該当しない detail field は null になる。payload を持たない legacy row も kind と created_at を返し、detail field は null となる。response に workspace_id、Stripe customer / subscription ID、credential、secret は含まれない。

Checkout response は checkout_url, expires_at、portal response は portal_url を返す。Stripe customer/subscription IDs は返さない。30,000 MAU を超えて継続利用するための支払い有効化に使う。

Impersonation#

Method / path 必須 field・query Scope / access
POST /impersonation?environment_id={environmentId} body: user_id, reason (10–1000 文字), duration_minutes (1–15) impersonation:create; Owner/Admin の Dashboard session のみ
POST /impersonation/stop — 認証済み request。active impersonation cookie を消去

Environment settings で impersonation を明示的に有効化してから開始する。API key では開始できない。Start response に actor、対象 user、理由、期限、禁止 Scope が含まれる。Impersonation の開始・終了は Dashboard session の操作であり、API key による Resource 管理とは別に扱う。

TypeScript client#

Node.js の Management API client、Result return convention、pagination helper、全 method は @smartcrab/management-client を参照する。