//DOCS API

HTTP endpoints, auth, identity headers, and response conventions.
GitHub Docs

API Reference

Base URL in local dev: http://localhost:4100.

The API is split into:

  • OpenAI-compatible gateway: /v1/*.
  • Punk management/runtime API: /api/v1/*.
  • Health check: /health.
  • Static dashboard: /.

Authentication

If PUNK_API_KEY is not set, Punk runs in open dev mode:

  • Headerless protected requests are allowed.
  • The default tenant is used.
  • Requests are treated as admin.
  • Private web fetches are allowed for local demos.

If PUNK_API_KEY is set:

  • Protected /v1/ and /api/ routes require Authorization: Bearer <token>.
  • The bootstrap token is admin for the default tenant.
  • Tenant API keys created through /api/v1/keys are hashed at rest.

Identity Headers

HeaderPurpose
X-Punk-AppLogical app id. Optional but recommended.
X-Punk-AgentAgent id/name for trust and audit.
X-Punk-SubjectPseudonymous user/subject; also a cache-key safety dimension.

If an API key is pinned to an app, the pinned app overrides X-Punk-App.

Response Headers

HeaderMeaning
x-punk-run-idRun id for trace lookup, feedback, and replay bundle export.
x-punk-routeSelected route such as live, exact_cache, artifact, or blocked.

These headers are exposed through CORS.

Gateway

POST /v1/chat/completions

OpenAI-compatible chat completions.

Minimum body:

{
  "model": "gpt-4o",
  "messages": [{ "role": "user", "content": "hello" }]
}

Behavior:

  • Uses live OpenAI-compatible provider when OPENAI_API_KEY is set.
  • Uses deterministic mock provider otherwise.
  • Preserves OpenAI-shaped response body.
  • Supports streaming through gateway clients.
  • Records run, traces, route explanation, cost, latency, policy, and cache/artifact decisions.

Health

GET /health

Returns gateway health and current provider information.

{
  "ok": true,
  "version": "0.1.0",
  "provider": "mock",
  "plasmate": false
}

API Keys

Admin required.

MethodPathPurpose
POST/api/v1/keysCreate a tenant API key; token returned once.
GET/api/v1/keysList tenant API keys.
POST/api/v1/keys/:id/revokeRevoke a key.

Create key body:

{
  "name": "staging observe",
  "mode": "observe",
  "appId": "support-agent",
  "admin": false
}

Modes:

  • observe: record what Punk would have done, but return live response and do not block.
  • optimize: allow caches, artifacts, and policy enforcement.

Settings

MethodPathPurpose
GET/api/v1/settingsList tenant settings; secrets are redacted as [set].
PUT/api/v1/settingsUpdate one setting; admin required.

Known settings:

KeyValue
retention_daysPositive number.
redactionBoolean.
webhook_urlPublic HTTP(S) URL or null.
webhook_secretString or null; never echoed back.
approval_exception_ttl_hoursPositive number.
canary_enabledBoolean.

Runs

MethodPathPurpose
GET/api/v1/runsList runs. Query: limit, offset, route, status.
GET/api/v1/runs/:idGet run, trace events, and side effects.
GET/api/v1/runs/:id/integrityVerify trace integrity hash chain.
GET/api/v1/runs/:id/replay-bundleExport replay evidence for a run.
POST/api/v1/runs/:id/feedbackSubmit rating/correction.

Feedback body:

{
  "type": "rating",
  "rating": -1,
  "correction": "The correct classification is billing."
}

Negative feedback on artifact-served runs creates a failed live evaluation for that artifact.

Patterns

MethodPathPurpose
GET/api/v1/patternsList discovered patterns.
GET/api/v1/patterns/:idPattern detail with artifacts and example runs.
POST/api/v1/patterns/:id/synthesizeSynthesize candidate artifact; admin required.
POST/api/v1/patterns/:id/ignoreMark pattern negative and cache that decision; admin required.

Artifacts

MethodPathPurpose
GET/api/v1/artifactsList artifacts.
GET/api/v1/artifacts/:idArtifact detail, evaluations, and pattern.
POST/api/v1/artifacts/:id/promotePromote artifact; admin required.
POST/api/v1/artifacts/:id/rollbackRetire artifact; admin required.
POST/api/v1/artifacts/:id/quarantineQuarantine artifact; admin required.
POST/api/v1/artifacts/:id/replayRe-run replay suite; admin required.

Replay body:

{
  "runIds": ["run_..."]
}

If runIds is omitted, Punk uses artifact provenance and holdout runs.

Approvals

MethodPathPurpose
GET/api/v1/approvalsList approvals. Query: status.
POST/api/v1/approvals/:id/approveApprove; admin required.
POST/api/v1/approvals/:id/rejectReject; admin required.

Decision body:

{
  "reason": "Approved for this deployment window."
}

Learning

MethodPathPurpose
GET/api/v1/learning/reportLast learning report and history.
POST/api/v1/learning/tickRun learning loop now; admin required.

Cache

MethodPathPurpose
GET/api/v1/cache/statsCache hit/miss stats.
POST/api/v1/cache/invalidateInvalidate cache entries; admin required.

Invalidate body:

{
  "cacheType": "som"
}

Omit cacheType to clear all cache types for the tenant.

Governance And Audit

MethodPathPurpose
GET/api/v1/auditList audit events. Query: limit, decision.
GET/api/v1/agentsList agents and trust state.
GET/api/v1/policiesList loaded policies.

Jobs

MethodPathPurpose
GET/api/v1/jobsList jobs and stats. Query: status, limit.

Semantic Web

MethodPathPurpose
GET/api/v1/web/snapshotsList stored SOM snapshots.
POST/api/v1/web/fetchFetch a URL and compile/retrieve SOM.

Fetch body:

{
  "url": "https://example.com",
  "bypassCache": false
}

Response includes:

  • som
  • source: plasmate, builtin, or cache
  • cached
  • htmlBytes
  • somBytes
  • tokensSavedEstimate
  • diff when a previous snapshot exists
  • context: compact prompt-ready rendering

SDK Trace And Tool Cache

MethodPathPurpose
POST/api/v1/traceAppend an SDK trace event to a run.
POST/api/v1/tool-cache/checkCheck read-only tool-result cache.
POST/api/v1/tool-cache/storeStore read-only tool result.

Most users should call these through @punk/sdk instead of raw HTTP.

Errors

Errors are JSON objects with an error key. Provider-compatible chat validation errors use OpenAI-style error objects.

Common statuses:

  • 400: invalid request body or unsupported setting.
  • 401: missing or invalid bearer token.
  • 403: policy block, private URL blocked, or admin key required.
  • 404: tenant-scoped record not found.
  • 429: rate limit exceeded; retry-after header included.
  • 502: upstream web fetch failure.