Prompt library API
A tenant prompt is a per-organization, admin-managed, versioned prompt template: a display name, an optional description, the prompt template body, an optional category, and a monotonic version counter. Every create and every edit snapshots the full body, so any past version can be restored (restore mints a new version whose body equals the chosen one — no history is destroyed). This is the organization-wide library managed by an authorized admin; it is separate from a user's personal prompts (the /chat-commands surface). In the UI it is surfaced as Workspace › Library › Prompts, where each row also offers a Use in chat action that inserts the prompt into a new chat.
Base URL: https://<your-gateway-host>/admin/v1
Every endpoint requires the GOVERNANCE_MANAGE permission: the roles admin, tenant_admin and ki_manager hold it, and a custom role can grant it. A caller without it is rejected 403; an unauthenticated caller 401. The client is never the authorization boundary — every call is re-gated and tenant-scoped server-side.
Four-eyes approval. If the organization has enabled config approval, a create/update/delete/restore by a Fachadmin (
tenant_admin/ki_manager) is not applied immediately — it returns202 { "status": "pending_approval", "approval_id": "…" }and is held until a second, different administrator approves it. See the Config approvals API. This is off by default; when off, these endpoints apply immediately as described below. Approving a held change whose organization was deleted while it was pending (users kept) ends409 apply_failedwith reasontenant_not_foundand applies nothing; the other outcomes are listed in the Config approvals API.
Tenant scope
Tenant scope is decided server-side; the client value never widens it.
| Caller | Scope |
|---|---|
admin |
Must target one tenant with ?tenant_id=<id>. |
tenant_admin / ki_manager |
Always their own tenant. A ?tenant_id naming another tenant is ignored (pinned to the caller's own). |
These routes are inherently per-tenant: an admin who omits ?tenant_id is rejected 400 { "error": "tenant_id required", "code": "tenant_id_required" }; a repeated ?tenant_id=a&tenant_id=b is 400 { "error": "tenant_id must be a single value", "code": "bad_param" }; an admin ?tenant_id that is not one canonical tenant UUID (8-4-4-4-12 hex groups, either case) is 400 { "error": "tenant_id must be a tenant id", "code": "bad_param" }; a non-admin whose account carries no tenant is refused 403. A prompt (or a prompt version) that belongs to another tenant reads as 404 not-found — the boundary never leaks another tenant's existence.
GET /tenant-prompts
List the tenant's live prompts (soft-deleted prompts are excluded), ordered by name. Returns a JSON array; description and category are null when unset. version may serialize as a string. Each row also carries owner_name — a display label for the prompt's author (COALESCE(user.name, user.email) resolved server-side from created_by), which is null when the author was erased (created_by is anonymized to NULL, never leaking another user) — and updated_at, surfaced for the library's Owner and Updated columns. owner_name is a display label only: the tenant fence stays on tenant_id, and an author is always same-tenant, so no cross-tenant identity can appear.
[
{ "id": "…", "name": "Complaint reply", "description": "Standard tone", "template": "Answer politely…", "category": "Support", "version": "2", "owner_name": "curator@acme.example", "created_at": 1753000000, "updated_at": 1753000100 }
]
POST /tenant-prompts/search
Body: {"q": "<query>"} (JSON); the tenant scope stays in ?tenant_id= like on every tenant-prompts route. The query travels in the body — never in the URL, because the web server writes request lines into its error log. An absent or empty q → 400 q_required; a q that is not a string, not valid UTF-8 or longer than 1024 characters → 400 invalid_q; a body that is not a JSON object → 400 malformed_body. The permission and tenant checks run first.
Performs a server-side full-text search (MATCH … IN BOOLEAN MODE) over name + description + template, tenant-scoped. The query is escaped to a safe boolean expression: tokens shorter than 3 bytes are dropped and operator characters (+ - * " ( ) ~ < > @) can never reach the engine, so operator injection is impossible and an unbalanced quote can never break the query. An empty query or a query whose every token was dropped returns [] (no error, no unfiltered list). Search rows carry the same fields as the plain list except owner_name — the search query does not resolve the author label, so the library's Owner column is blank in search mode.
Deprecated: GET /tenant-prompts?q=<query> runs the same search until 11 January 2027 with Deprecation / Sunset headers; without q it is the plain list (not deprecated). A repeated or valueless ?q is now 400 invalid_q (it used to return the full list). See Search and lookup values travel in the request body.
POST /tenant-prompts
Create a prompt at version 1 (and its v1 snapshot). Body (JSON object):
| Field | Accepted | Rejected → 400 |
|---|---|---|
name |
string, 1–255 characters, valid UTF-8 (required) | missing / empty / blank (nothing visible) / non-string / > 255 chars / malformed UTF-8 |
template |
string, 1–65535 bytes, valid UTF-8 (required) | missing / empty / blank (nothing visible) / non-string / > 65535 bytes / malformed UTF-8 |
description |
string ≤ 65535 bytes valid UTF-8, or null / "" → stored NULL (optional) |
non-string / > 65535 bytes / malformed UTF-8 |
category |
string ≤ 64 characters valid UTF-8 (trimmed), or null / "" / whitespace → stored NULL (optional) |
non-string / > 64 chars / malformed UTF-8 |
Every field-validation 400 carries a top-level code next to the error text, one per field: name_required / template_required (missing, empty or blank: only whitespace, Unicode spaces, zero-width / format or control characters, the braille blank U+2800 or a few extender marks such as tatweel U+0640), invalid_name / invalid_template / invalid_description / invalid_category (non-string or malformed UTF-8), and name_too_long / template_too_long / description_too_long / category_too_long (over the limit). Branch on code, not on the error text.
Success → 201 { "id": "<new-id>" }. version, created_by, created_at, updated_at are server-set and never accepted from the client.
PATCH /tenant-prompts/<id>
Update a prompt's mutable fields (mints a new version + snapshot). Only the fields present in the body are changed; each present field is validated exactly as in POST above. description / category present as null (or empty) clear the column to NULL; absent leaves it unchanged.
A present but empty, blank (nothing visible) or null name / template is rejected 400 invalid_name / invalid_template (on an edit the field is present, so it is invalid rather than missing); the other codes are as in POST. A body that is valid JSON but not an object ([], "x", null) is 400 malformed_body on create and update. An empty body (no updatable field) is rejected 400 { "error": "no updatable fields", "code": "nothing_to_update" }. A prompt that does not exist in the caller's tenant (unknown id / another tenant / already soft-deleted) → 404 { "error": "prompt not found" }. Success → 200 { "ok": true }.
DELETE /tenant-prompts/<id>
Soft-delete a prompt (it stops resolving on every read path; its version history is retained until the organization is purged). Unknown / another tenant / already deleted → 404 { "error": "prompt not found" }. Success → 200 { "deleted": true }.
GET /tenant-prompts/<id>/versions
The prompt's version history, newest first, tenant-scoped through the parent prompt. Returns a JSON array of { "version", "name", "description", "template", "category", "created_at" }. A prompt not in the caller's tenant (or soft-deleted) → 404 (every live prompt has ≥ 1 version, so an empty history is never a valid response — it is reported as not-found).
GET /tenant-prompts/<id>/versions/<version>
One historical snapshot. version must be a positive integer written canonically: decimal digits only, no leading zero, sign, whitespace, exponent, hex or fraction; at most 10 digits; 1..2147483647. Any other value (01, 0x1, 1e0, +1, 1.0, a padded or non-numeric value, 0, an out-of-range number) → 400 { "error": "invalid version", "code": "invalid_version" }. An unknown version, or a prompt not in the caller's tenant → 404 { "error": "version not found" }.
POST /tenant-prompts/<id>/versions/<version>/restore
Restore a chosen version: re-applies its snapshot through the single update path, minting a new version whose body equals the chosen one (no history is lost). A NULL snapshot description / category is faithfully restored back to NULL. version validation and not-found behaviour are as for the single-version read above. Success → 200 { "ok": true }.
GET /me/tenant-prompts
The prompt library as any signed-in member sees it: the live prompts of the caller's own organization, for the composer's Prompt library picker (+ menu). Unlike every route above it does not need GOVERNANCE_MANAGE. The tenant is derived server-side from the session; a query string (including ?tenant_id) is ignored. There is no request input.
The body is an object: prompts is the list and truncated says whether the organization has more live prompts than were returned. Each row carries only id, name, description, template and category (description and category are null when unset), ordered by name. Only live prompts are listed: soft-deleted prompts and changes still held by four-eyes approval are not included. The list is not paginated and returns at most the first 200 prompts by name (a template can be up to 64 KiB, and the picker reads the list each time it opens); truncated is true when more exist, and the picker then says that it shows only the first 200. The full library stays available to admins through GET /tenant-prompts.
{
"prompts": [
{ "id": "…", "name": "Complaint reply", "description": "Standard tone", "template": "Answer politely…", "category": "Support" }
],
"truncated": false
}
| Status | Meaning |
|---|---|
200 |
The list. { "prompts": [], "truncated": false } when the account has no organization (the tenant is absent or null — for example a platform administrator without a tenant) or the organization has been deleted. |
401 |
Not signed in, or the session's tenant value is malformed (an empty string, a number, an object): refused as unauthenticated, never treated like "no organization". |
403 |
impersonation_forbidden while an administrator views the workspace as another user (see Impersonation). |
503 |
Transient storage fault on the read; retry. |
500 |
Any other database fault; the database text is never returned. |
Storage faults
A create, update or restore writes the prompt row and its version snapshot as one server-retried transaction (a DELETE is a server-retried transaction too). A transient conflict (or no database connection) that wrote nothing → 503 { "error": "temporarily unavailable; retry later" } — send the request again. This includes the rare InnoDB full-text id ordering conflict (errno 182) between concurrent writers: it is retried server-side up to 4 attempts and, if it still fails, answers the same 503 with nothing written — a write is never silently dropped. A DELETE that wrote nothing (a transient conflict, or no database connection) answers the same 503, and so does a failed read (search, list, versions, a single version, and the pre-reads of DELETE and restore). Any other database fault on a write (including DELETE) → 500 { "error": "internal error", "request_id": "<id>" }; the database's own text is never returned. See Storage faults.
Notes
- Full-gauntlet isolation. Cross-tenant access is impossible: every CRUD query is scoped
tenant_id = ?and every version query is scoped by a JOIN through the parent prompt (parent.tenant_id = ? AND parent.deleted_at IS NULL). Atenant_admin/ki_managercan never read, edit, restore, or delete another organization's prompt or version. - DSGVO / GDPR. When a user is erased, the
created_byauthor pointer on every prompt and version snapshot is anonymized (set toNULL) while the organization's library is preserved. When an organization is purged, its prompts and all version snapshots are removed entirely (foreign-key cascade), with the count recorded in the deletion protocol.