# Retention API

## GET /api/v1/admin/retention

`GET /api/v1/admin/retention` · operation `getMetadataRetention`

Requires metadata_retention and retention:manage, global admin only. Retention is opt-in; zero days or disabled flag preserves all data. Scope is expired usage, dependent valuation and corresponding finalized receipts only. Incomplete receipts, rate snapshots, audit, identities, configuration and price history survive. Every destructive policy requires explicit confirmation, current entity version and YAML fingerprint; named administrators are revalidated transactionally. Higher monotonic YAML revision resolves stale/conflicting replica bases. Reads/preview never execute purge.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private, no-store policy | `application/json`: [MetadataRetentionView](/docs/api/schemas/m/#metadataretentionview) |
| `401` | Console session required | — |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled or override unavailable | — |
| `409` | Policy/base version changed, stale base or equal-revision fingerprint conflict | — |
| `503` | PostgreSQL temporarily unavailable | — |


## PUT /api/v1/admin/retention

`PUT /api/v1/admin/retention` · operation `setMetadataRetention`

Requires metadata_retention and retention:manage, global admin only. Retention is opt-in; zero days or disabled flag preserves all data. Scope is expired usage, dependent valuation and corresponding finalized receipts only. Incomplete receipts, rate snapshots, audit, identities, configuration and price history survive. Every destructive policy requires explicit confirmation, current entity version and YAML fingerprint; named administrators are revalidated transactionally. Higher monotonic YAML revision resolves stale/conflicting replica bases. Reads/preview never execute purge.

**Authentication:** `adminSession`

**Request body** (required)

- `application/json`: object
  - `base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)
  - `version` (integer (int64), required, min 0)
  - `confirm_delete_expired_metadata` (boolean) — Must be true whenever resulting policy has positive retention days, including reversion
  - `policy` ([MetadataRetentionPolicy](/docs/api/schemas/m/#metadataretentionpolicy), required)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Committed policy; future batches use shared cadence, no immediate purge | — |
| `400` | Invalid retention policy or missing explicit destructive confirmation | — |
| `401` | Console session required | — |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled or override unavailable | — |
| `409` | Policy/base version changed, stale base or equal-revision fingerprint conflict | — |
| `503` | PostgreSQL temporarily unavailable | — |


## DELETE /api/v1/admin/retention

`DELETE /api/v1/admin/retention` · operation `revertMetadataRetention`

Requires metadata_retention and retention:manage, global admin only. Retention is opt-in; zero days or disabled flag preserves all data. Scope is expired usage, dependent valuation and corresponding finalized receipts only. Incomplete receipts, rate snapshots, audit, identities, configuration and price history survive. Every destructive policy requires explicit confirmation, current entity version and YAML fingerprint; named administrators are revalidated transactionally. Higher monotonic YAML revision resolves stale/conflicting replica bases. Reads/preview never execute purge.

**Authentication:** `adminSession`

**Request body** (required)

- `application/json`: object
  - `base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)
  - `version` (integer (int64), required, min 0)
  - `confirm_delete_expired_metadata` (boolean) — Must be true whenever resulting policy has positive retention days, including reversion

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Committed policy; future batches use shared cadence, no immediate purge | — |
| `400` | Invalid retention policy or missing explicit destructive confirmation | — |
| `401` | Console session required | — |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled or override unavailable | — |
| `409` | Policy/base version changed, stale base or equal-revision fingerprint conflict | — |
| `503` | PostgreSQL temporarily unavailable | — |


## GET /api/v1/admin/retention/preview

`GET /api/v1/admin/retention/preview` · operation `previewMetadataRetention`

Requires metadata_retention and retention:manage, global admin only. Retention is opt-in; zero days or disabled flag preserves all data. Scope is expired usage, dependent valuation and corresponding finalized receipts only. Incomplete receipts, rate snapshots, audit, identities, configuration and price history survive. Every destructive policy requires explicit confirmation, current entity version and YAML fingerprint; named administrators are revalidated transactionally. Higher monotonic YAML revision resolves stale/conflicting replica bases. Reads/preview never execute purge.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Read-only sample of at most 1000 eligible events; truncated indicates a lower bound, not a global count | `application/json`: [MetadataRetentionPreview](/docs/api/schemas/m/#metadataretentionpreview) |
| `400` | Preview accepts no caller-selected query filters | — |
| `401` | Console session required | — |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled or override unavailable | — |
| `409` | Policy/base version changed, stale base or equal-revision fingerprint conflict | — |
| `503` | PostgreSQL temporarily unavailable | — |


## GET /api/v1/admin/audit-retention

`GET /api/v1/admin/audit-retention` · operation `getAuditRetention`

T063. Requires audit_retention; global admin with retention:manage. Policy in effect for the identity and control audit tables, its source (yaml or console), the contractual floor (minimum_days) and optional ceiling (maximum_days), the last purge and the number of active legal holds. Reads never purge.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private, no-store policy view | `application/json`: [AuditRetentionView](/docs/api/schemas/a/#auditretentionview) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | retention_base_changed: stale or conflicting YAML revision across replicas | — |
| `503` | PostgreSQL temporarily unavailable | — |


## PUT /api/v1/admin/audit-retention

`PUT /api/v1/admin/audit-retention` · operation `setAuditRetention`

T063. Stores a console override under version CAS. The override may only tighten the YAML contract: minimum_days cannot go below the YAML value, and a YAML maximum_days cannot be raised or removed. retain_days must be 0 (preserve, only without a ceiling) or between minimum_days and maximum_days (or 3650). A positive period requires confirm_delete_expired_audit. Audited as audit_retention.set; the first batch runs one interval later. A later higher YAML revision that the override would loosen drops it (audit_retention.override_dropped).

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)
  - `version` (integer (int64), required, min 0)
  - `confirm_delete_expired_audit` (boolean)
  - `policy` ([AuditRetentionPolicy](/docs/api/schemas/a/#auditretentionpolicy), required)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Override stored | — |
| `400` | invalid_retention, retention_contract (would loosen the YAML contract) or confirmation_required | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | version_conflict or retention_base_changed | — |
| `503` | PostgreSQL temporarily unavailable | — |


## DELETE /api/v1/admin/audit-retention

`DELETE /api/v1/admin/audit-retention` · operation `revertAuditRetention`

T063. Removes the console override and returns to the YAML policy (audit_retention.revert). confirm_delete_expired_audit is required when the YAML policy has a positive period.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)
  - `version` (integer (int64), required, min 0)
  - `confirm_delete_expired_audit` (boolean)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Override removed | — |
| `400` | confirmation_required | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled or no override | — |
| `409` | version_conflict or retention_base_changed | — |
| `503` | PostgreSQL temporarily unavailable | — |


## GET /api/v1/admin/audit-retention/preview

`GET /api/v1/admin/audit-retention/preview` · operation `previewAuditRetention`

T063. Read-only bounded count (at most 10000 per table) of audit rows the current policy would purge, the oldest row and whether an active legal hold blocks the purge. Accepts no query parameters.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Bounded preview | `application/json`: [AuditRetentionPreview](/docs/api/schemas/a/#auditretentionpreview) |
| `400` | Preview accepts no caller-selected query filters | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | retention_base_changed | — |
| `503` | PostgreSQL temporarily unavailable | — |


## GET /api/v1/admin/legal-holds

`GET /api/v1/admin/legal-holds` · operation `listLegalHolds`

T063. Requires legal_holds; global admin with retention:manage. Active holds first, then the most recent releases (200 rows in all). Active holds suspend audit retention, metadata retention and per-end-user deletion even while legal_holds is off.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Holds | `application/json`: object |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | PostgreSQL temporarily unavailable | — |

Response `200` fields:

- `application/json`: object
  - `active` (integer, required, min 0, max 100)
  - `holds` (array of [LegalHold](/docs/api/schemas/l/#legalhold), required, max items 200)


## POST /api/v1/admin/legal-holds

`POST /api/v1/admin/legal-holds` · operation `placeLegalHold`

T063. Places a hold (audited as legal_hold.place). Waits for in-flight purge batches, so no batch deletes after the hold is stored. At most 100 active holds.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `reason` (string, required, min length 1, max length 500)
  - `reference` (string, max length 128) — Optional case or ticket reference

**Responses**

| Status | Description | Body |
|---|---|---|
| `201` | Hold in force | `application/json`: [LegalHold](/docs/api/schemas/l/#legalhold) |
| `400` | invalid_legal_hold | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | legal_hold_limit: 100 holds already active | — |
| `503` | PostgreSQL temporarily unavailable | — |


## POST /api/v1/admin/legal-holds/{id}/release

`POST /api/v1/admin/legal-holds/{id}/release` · operation `releaseLegalHold`

T063. Releases one active hold with a reason (legal_hold.release). Released holds stay listed as history; purges resume at their next scheduled run once no hold is active.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^lh_[a-f0-9]{32}$` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `reason` (string, required, min length 1, max length 500)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Hold released | — |
| `400` | invalid_legal_hold | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled or no active hold with this ID | — |
| `503` | PostgreSQL temporarily unavailable | — |


## GET /api/v1/admin/audit-chain

`GET /api/v1/admin/audit-chain` · operation `getAuditChain`

T063. Requires audit_tamper_evidence; global admin with audit:read. The audit hash chain head (sequence and SHA-256), its anchor (genesis or the newest purge checkpoint), the number of committed audit rows not sealed yet, the stored result of the last verification and the 20 newest checkpoints. Reads never seal or verify.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private, no-store chain state | `application/json`: [AuditChainStatus](/docs/api/schemas/a/#auditchainstatus) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | PostgreSQL temporarily unavailable | — |


## POST /api/v1/admin/audit-chain/verify

`POST /api/v1/admin/audit-chain/verify` · operation `verifyAuditChain`

T063. Recomputes every seal after the anchor in one snapshot and reports the first inconsistency. The result is stored on the chain (shown by GET) and audited as audit_chain.verify or audit_chain.tampered. Bounded by 60 s; for a longer chain run `gateway audit-verify`. A tampered chain is a 200 with status tampered, not an error. The optional body checks one (seq, hash) pair kept outside the database, such as the head of an audit_chain_sealed log line. With mcp_execution_chain (T168) the answer also carries mcp_executions, the verification of the MCP execution chain.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body**

- `application/json`: object
  - `expect` (object)
    - `seq` (integer (int64), required, min 0)
    - `hash` (string, required, pattern `^[a-f0-9]{64}$`)
  - `expect_anchor` (object) — A logged audit_chain_checkpoint pair the current anchor must equal
    - `seq` (integer (int64), required, min 0)
    - `hash` (string, required, pattern `^[a-f0-9]{64}$`)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Verification report | `application/json`: [AuditChainReport](/docs/api/schemas/a/#auditchainreport) |
| `400` | invalid_request or invalid_expect | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | PostgreSQL unavailable or the verification exceeded 60 s | — |


## GET /api/v1/admin/end-user-deletions

`GET /api/v1/admin/end-user-deletions` · operation `listEndUserDeletions`

T055. Requires end_user_deletion; global admin with retention:manage. The 100 most recent jobs, newest first. Jobs never return the end-user value.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Jobs | `application/json`: object |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | PostgreSQL temporarily unavailable | — |

Response `200` fields:

- `application/json`: object
  - `jobs` (array of [EndUserDeletionJob](/docs/api/schemas/e/#enduserdeletionjob), required, max items 100)


## POST /api/v1/admin/end-user-deletions

`POST /api/v1/admin/end-user-deletions` · operation `requestEndUserDeletion`

T055. Erases what is stored for one declared end user (exact X-Gateway-End-User / OpenAI user value) inside one exact team. dry_run returns the counts without changing anything. Otherwise confirm must be true: the request creates a durable job (or returns the open job for the same team and end user), runs bounded batches for up to 5 seconds and returns the job; the worker finishes the rest. Usage events and finalized receipts keep tokens, costs and fingerprints with end_user cleared (receipts after a 15-minute quiet period; never-finalized receipts once abandoned for 24 hours); content captures, evaluation captures, evaluation scores/comments and client addresses of those requests are deleted; budget counters, reservations, the ledger archive and budget alerts are kept. The job completes after a final sweep once the longest allowed request (stream max_duration or Realtime max_session, plus one minute, at least 15 minutes) has passed since its cutoff. The dry run is audited as end_user.delete.dry_run (team and count only). Refused with 409 while a legal hold is active. Audited as end_user.delete.request and end_user.delete.complete without the value.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `team` (string, required, min length 1, max length 128) — Exact team ID; never matches other teams
  - `end_user` (string, min length 1, max length 128) — Exact recorded value, case-sensitive, no control characters. Required unless customer_org and customer_member are sent
  - `customer_org` (string) — T158.005, with customer_member instead of end_user (needs customer_portal): erase one customer portal member of this organization. The request deletes the member record and its override requests at once; the job clears customer_member from usage events and receipts (same quiet periods), deletes the same captures and redacts the member from customer alert events; counters, reservations and budget alerts keyed by the member are kept as financial evidence
  - `customer_member` (string)
  - `dry_run` (boolean)
  - `confirm` (boolean) — Required (true) unless dry_run

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Dry-run counts, or the already-open job for the same team and end user | `application/json`: object or [EndUserDeletionJob](/docs/api/schemas/e/#enduserdeletionjob) |
| `201` | New job (often already completed) | `application/json`: [EndUserDeletionJob](/docs/api/schemas/e/#enduserdeletionjob) |
| `400` | invalid_team, invalid_end_user or confirmation_required | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required; write requires CSRF and same origin | — |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | legal_hold_active | — |
| `503` | PostgreSQL temporarily unavailable | — |

Response `200` fields:

- `application/json`: object or [EndUserDeletionJob](/docs/api/schemas/e/#enduserdeletionjob)
  - One of (1): object
    - `dry_run` (`true`, required)
    - `team` (string, required)
    - `counts` ([EndUserDeletionCounts](/docs/api/schemas/e/#enduserdeletioncounts), required)
    - `blocked_by_legal_hold` (boolean, required)
  - One of (2): [EndUserDeletionJob](/docs/api/schemas/e/#enduserdeletionjob)


## GET /api/v1/admin/end-user-deletions/{id}

`GET /api/v1/admin/end-user-deletions/{id}` · operation `getEndUserDeletion`

T055. One job's state and counts.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^eud_[a-f0-9]{32}$` |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Job | `application/json`: [EndUserDeletionJob](/docs/api/schemas/e/#enduserdeletionjob) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Global administrator required | — |
| `404` | Feature disabled or unknown job | — |
| `503` | PostgreSQL temporarily unavailable | — |

