Policy engine API
This page documents the 20 gateway endpoints tagged policy_engine in the gateway's OpenAPI document. Every example targets a gateway on http://localhost:4000 and authenticates with the SPANWAY_API_KEY environment variable, which holds the master key or a virtual key that has access to the route. Request and response fields are taken from the schema the running gateway publishes at /openapi.json, and the same document is bundled with this site as openapi.json.
| Method | Path | Summary |
|---|---|---|
| POST | /policies | Create a policy |
| POST | /policies/attachments | Attach a policy to teams, keys, models or tags |
| POST | /policies/attachments/estimate-impact | Estimate how many keys and teams an attachment would reach |
| GET | /policies/attachments/list | List policy attachments |
| GET | /policies/attachments/{attachment_id} | Read one policy attachment |
| DELETE | /policies/attachments/{attachment_id} | Delete a policy attachment |
| PUT | /policies/attachments/{attachment_id}/mode | Switch an attachment between enforce and audit |
| GET | /policies/compare | Compare two policy versions |
| GET | /policies/list | List policies and their versions |
| DELETE | /policies/name/{policy_name}/all-versions | Delete every version of a policy |
| GET | /policies/name/{policy_name}/versions | List the versions of a policy |
| POST | /policies/name/{policy_name}/versions | Create a draft version of a policy |
| POST | /policies/resolve | Simulate which policies and guardrails a request would get |
| POST | /policies/simulate | Replay recent traffic through a policy |
| POST | /policies/test-pipeline | Run a guardrail pipeline on sample messages |
| GET | /policies/{policy_id} | Read one policy version |
| PUT | /policies/{policy_id} | Change a draft policy version |
| DELETE | /policies/{policy_id} | Delete one policy version |
| GET | /policies/{policy_id}/resolved-guardrails | List the guardrails a policy resolves to |
| PUT | /policies/{policy_id}/status | Publish or promote a policy version |
Endpoints
POST /policies
Create a policy
Create a policy as version 1 in production, so it applies wherever it is attached right away. inherit names a
parent whose guardrails are taken over, guardrails_add and guardrails_remove adjust them, condition.model
limits the policy to models matching a regular expression, and pipeline runs the guardrails in a fixed order
with an action per outcome. Proxy admins only. The change is written to the audit log.
Example Request:
curl -X POST "http://localhost:4000/policies" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"policy_name": "global-baseline",
"description": "Base guardrails for all requests",
"guardrails_add": ["pii_masking", "prompt_injection"],
"guardrails_remove": []
}'
Example Response:
{
"policy_id": "123e4567-e89b-12d3-a456-426614174000",
"policy_name": "global-baseline",
"inherit": null,
"description": "Base guardrails for all requests",
"guardrails_add": ["pii_masking", "prompt_injection"],
"guardrails_remove": [],
"condition": null,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
Request body (application/json, schema PolicyCreateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
condition | PolicyConditionRequest or null | No | Condition for when this policy applies. |
description | string or null | No | Human-readable description of the policy. |
guardrails_add | array of string or null | No | List of guardrail names to add. |
guardrails_remove | array of string or null | No | List of guardrail names to remove (from inherited). |
inherit | string or null | No | Name of parent policy to inherit from. |
pipeline | object or null | No | Optional guardrail pipeline for ordered execution. Contains 'mode' and 'steps'. |
policy_name | string | Yes | Unique name for the policy. |
Example request:
curl -X POST "http://localhost:4000/policies" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"condition": {
"model": "claude-sonnet-.*"
},
"description": "Extra checks for frontier models",
"guardrails_add": [
"prompt-injection"
],
"inherit": "global-baseline",
"policy_name": "frontier-safety"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The policy version as stored. | application/json PolicyDBResponse |
| 400 | A policy with this name exists. | application/json ErrorDetail |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change a policy. Admin viewers can only read | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
condition | object or null | No | Policy condition. |
created_at | string or null | No | When the policy was created. |
created_by | string or null | No | Who created the policy. |
definition_location | enum: "db", "config" | No | Where this policy is defined: 'db' (database) or 'config' (config.yaml). |
description | string or null | No | Policy description. |
guardrails_add | array of string | No | Guardrails to add. |
guardrails_remove | array of string | No | Guardrails to remove. |
inherit | string or null | No | Parent policy name. |
is_latest | boolean | No | True if this is the latest version by version_number. |
parent_version_id | string or null | No | Policy ID this version was cloned from. |
pipeline | object or null | No | Optional guardrail pipeline. |
policy_id | string | Yes | Unique ID of the policy. |
policy_name | string | Yes | Name of the policy. |
production_at | string or null | No | When this version was promoted to production. |
published_at | string or null | No | When this version was published. |
updated_at | string or null | No | When the policy was last updated. |
updated_by | string or null | No | Who last updated the policy. |
version_number | integer | No | Version number of this policy. |
version_status | string | No | One of: draft, published, production. |
POST /policies/attachments
Attach a policy to teams, keys, models or tags
Make a policy apply to traffic. scope: "*" applies it to every request, otherwise teams, keys, models and
tags are combined, so an attachment with teams and models applies only to those models for those teams.
priority orders attachments, lower first, and default makes one a fallback that applies only when no other
attachment matches. Proxy admins only. The change is written to the audit log.
Example Request:
curl -X POST "http://localhost:4000/policies/attachments" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"policy_name": "global-baseline",
"scope": "*"
}'
Example with team-specific attachment:
curl -X POST "http://localhost:4000/policies/attachments" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"policy_name": "healthcare-compliance",
"teams": ["healthcare-team", "medical-research"]
}'
Example Response:
{
"attachment_id": "123e4567-e89b-12d3-a456-426614174000",
"policy_name": "global-baseline",
"scope": "*",
"teams": [],
"keys": [],
"models": [],
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
Request body (application/json, schema PolicyAttachmentCreateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
default | boolean | No | Apply this attachment only when no non-default attachment matches the request. |
keys | array of string or null | No | Key aliases or patterns this attachment applies to. |
mode | enum: "enforce", "audit" | No | enforce blocks or redacts. audit only records what the guardrails would have done. |
models | array of string or null | No | Model names or patterns this attachment applies to. |
policy_name | string | Yes | Name of the policy to attach. |
priority | integer or null | No | Explicit execution order, lower runs first. Prioritised attachments run before those without one. |
scope | string or null | No | Use '*' for global scope (applies to all requests). |
tags | array of string or null | No | Tag patterns this attachment applies to. Supports wildcards (e.g., health-*). |
teams | array of string or null | No | Team aliases or patterns this attachment applies to. |
Example request:
curl -X POST "http://localhost:4000/policies/attachments" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"models": [
"claude-sonnet-*"
],
"policy_name": "frontier-safety",
"priority": 10,
"teams": [
"research"
]
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyAttachmentDBResponse |
| 400 | A team, key alias or model without a wildcard does not exist. | application/json ErrorDetail |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change an attachment. Admin viewers can only read | application/json ErrorDetail |
| 404 | No policy has this name. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
attachment_id | string | Yes | Unique ID of the attachment. |
created_at | string or null | No | When the attachment was created. |
created_by | string or null | No | Who created the attachment. |
default | boolean | No | Apply this attachment only when no non-default attachment matches the request. |
definition_location | enum: "db", "config" | No | Where this attachment is defined: 'db' (database) or 'config' (config.yaml). |
keys | array of string | No | Key patterns. |
mode | enum: "enforce", "audit" | No | enforce blocks or redacts. audit only records what the guardrails would have done. |
models | array of string | No | Model patterns. |
policy_name | string | Yes | Name of the attached policy. |
priority | integer or null | No | Explicit execution order, lower runs first. Prioritised attachments run before those without one. |
scope | string or null | No | Scope of the attachment. |
tags | array of string | No | Tag patterns. |
teams | array of string | No | Team patterns. |
updated_at | string or null | No | When the attachment was last updated. |
updated_by | string or null | No | Who last updated the attachment. |
POST /policies/attachments/estimate-impact
Estimate how many keys and teams an attachment would reach
Count the keys and teams an attachment with this scope would apply to, before you create it, with a sample of their aliases. Team patterns also count the keys of the matched teams, and keys and teams without an alias are counted separately. The Estimate Impact button of the Admin UI calls this route.
Docs (https://docs.spanway.ai/docs/proxy/guardrails/policy_simulation)
Request body (application/json, schema PolicyAttachmentCreateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
default | boolean | No | Apply this attachment only when no non-default attachment matches the request. |
keys | array of string or null | No | Key aliases or patterns this attachment applies to. |
mode | enum: "enforce", "audit" | No | enforce blocks or redacts. audit only records what the guardrails would have done. |
models | array of string or null | No | Model names or patterns this attachment applies to. |
policy_name | string | Yes | Name of the policy to attach. |
priority | integer or null | No | Explicit execution order, lower runs first. Prioritised attachments run before those without one. |
scope | string or null | No | Use '*' for global scope (applies to all requests). |
tags | array of string or null | No | Tag patterns this attachment applies to. Supports wildcards (e.g., health-*). |
teams | array of string or null | No | Team aliases or patterns this attachment applies to. |
Example request:
curl -X POST "http://localhost:4000/policies/attachments/estimate-impact" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"models": [
"claude-sonnet-*"
],
"policy_name": "frontier-safety",
"teams": [
"research*"
]
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json AttachmentImpactResponse |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so attachments cannot be read. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
affected_keys_count | integer | No | Number of keys that would be affected (named + unnamed). |
affected_teams_count | integer | No | Number of teams that would be affected (named + unnamed). |
sample_keys | array of string | No | Sample of affected key aliases (up to 10). |
sample_teams | array of string | No | Sample of affected team aliases (up to 10). |
unnamed_keys_count | integer | No | Number of affected keys without an alias. |
unnamed_teams_count | integer | No | Number of affected teams without an alias. |
GET /policies/attachments/list
List policy attachments
List every attachment from the database and the config file, each saying which policy applies to which teams, keys, models or tags.
Config-defined attachments are returned with definition_location "config" and a synthetic attachment_id ("config-<index>").
Example Request:
curl -X GET "http://localhost:4000/policies/attachments/list" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Example Response:
{
"attachments": [
{
"attachment_id": "123e4567-e89b-12d3-a456-426614174000",
"policy_name": "global-baseline",
"scope": "*",
"teams": [],
"keys": [],
"models": [],
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
],
"total_count": 1
}
Example request:
curl -X GET "http://localhost:4000/policies/attachments/list" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyAttachmentListResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 500 | The database raised an error. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
attachments | array of PolicyAttachmentDBResponse | No | List of policy attachments. |
total_count | integer | No | Total number of attachments. |
GET /policies/attachments/{attachment_id}
Read one policy attachment
Read one attachment by its attachment_id.
Example Request:
curl -X GET "http://localhost:4000/policies/attachments/123e4567-e89b-12d3-a456-426614174000" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
attachment_id | path | string | Yes |
Example request:
curl -X GET "http://localhost:4000/policies/attachments/attachment_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyAttachmentDBResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 404 | No attachment has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
attachment_id | string | Yes | Unique ID of the attachment. |
created_at | string or null | No | When the attachment was created. |
created_by | string or null | No | Who created the attachment. |
default | boolean | No | Apply this attachment only when no non-default attachment matches the request. |
definition_location | enum: "db", "config" | No | Where this attachment is defined: 'db' (database) or 'config' (config.yaml). |
keys | array of string | No | Key patterns. |
mode | enum: "enforce", "audit" | No | enforce blocks or redacts. audit only records what the guardrails would have done. |
models | array of string | No | Model patterns. |
policy_name | string | Yes | Name of the attached policy. |
priority | integer or null | No | Explicit execution order, lower runs first. Prioritised attachments run before those without one. |
scope | string or null | No | Scope of the attachment. |
tags | array of string | No | Tag patterns. |
teams | array of string | No | Team patterns. |
updated_at | string or null | No | When the attachment was last updated. |
updated_by | string or null | No | Who last updated the attachment. |
DELETE /policies/attachments/{attachment_id}
Delete a policy attachment
Remove an attachment so its policy no longer applies to that traffic. The policy and its versions are kept. Proxy admins only. The change is written to the audit log.
Example Request:
curl -X DELETE "http://localhost:4000/policies/attachments/123e4567-e89b-12d3-a456-426614174000" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Example Response:
{
"message": "Attachment 123e4567-e89b-12d3-a456-426614174000 deleted successfully"
}
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
attachment_id | path | string | Yes |
Example request:
curl -X DELETE "http://localhost:4000/policies/attachments/attachment_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The attachment is gone. The policy itself is kept. | application/json PolicyDeletedResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change an attachment. Admin viewers can only read | application/json ErrorDetail |
| 404 | No attachment has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
message | string | Yes | Which policy or attachment was deleted. |
warning | string or null | No | Set when the deletion changed what the gateway enforces, for example production went. |
PUT /policies/attachments/{attachment_id}/mode
Switch an attachment between enforce and audit
Turn an attachment from audit into enforce, or back. In audit mode the policy's guardrails still run on every
matching request but only record what they would have done (each verdict carries audit_policy in the
guardrail information of the spend log), so you can watch a new policy on live traffic before it blocks
anything. Switch to enforce when the verdicts look right. The change applies to new requests at once and is
written to the audit log with the old and new attachment.
Example curl:
curl -X PUT http://0.0.0.0:4000/policies/attachments/123e4567-e89b-12d3-a456-426614174000/mode \
-H "Authorization: Bearer sk-admin" -H "Content-Type: application/json" \
-d '{"mode": "enforce"}'
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
attachment_id | path | string | Yes |
Request body (application/json, schema PolicyAttachmentModeUpdateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
mode | enum: "enforce", "audit" | Yes | enforce blocks or redacts. audit only records what the guardrails would have done. |
Example request:
curl -X PUT "http://localhost:4000/policies/attachments/attachment_id-123/mode" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"mode": "enforce"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The attachment with its new mode. | application/json PolicyAttachmentDBResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | Only proxy admins can change how a policy is enforced. | application/json ErrorDetail |
| 404 | No attachment has this id. Config attachments cannot change. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 503 | The gateway has no database. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
attachment_id | string | Yes | Unique ID of the attachment. |
created_at | string or null | No | When the attachment was created. |
created_by | string or null | No | Who created the attachment. |
default | boolean | No | Apply this attachment only when no non-default attachment matches the request. |
definition_location | enum: "db", "config" | No | Where this attachment is defined: 'db' (database) or 'config' (config.yaml). |
keys | array of string | No | Key patterns. |
mode | enum: "enforce", "audit" | No | enforce blocks or redacts. audit only records what the guardrails would have done. |
models | array of string | No | Model patterns. |
policy_name | string | Yes | Name of the attached policy. |
priority | integer or null | No | Explicit execution order, lower runs first. Prioritised attachments run before those without one. |
scope | string or null | No | Scope of the attachment. |
tags | array of string | No | Tag patterns. |
teams | array of string | No | Team patterns. |
updated_at | string or null | No | When the attachment was last updated. |
updated_by | string or null | No | Who last updated the attachment. |
GET /policies/compare
Compare two policy versions
Show the fields that differ between two policy versions, given their policy_id values as version_a and
version_b. The Compare versions button in the Admin UI calls this route.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
version_a | query | string | Yes | |
version_b | query | string | Yes |
Example request:
curl -X GET "http://localhost:4000/policies/compare?version_a=string&version_b=string" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyVersionCompareResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 404 | No policy version has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
field_diffs | object | No | Field name -> {version_a: val, version_b: val} for differing fields. |
version_a | PolicyDBResponse | Yes | First version. |
version_b | PolicyDBResponse | Yes | Second version. |
GET /policies/list
List policies and their versions
List every policy version from the database together with the policies in the config file. Filter with
version_status set to draft, published or production.
Config-defined policies are returned with definition_location "config" and are treated as production versions. On a name conflict with a production DB policy, only the DB policy is returned, mirroring runtime resolution where only production DB versions override config. A draft or published DB version does not hide the config policy, since the config version is still the one being enforced.
Query params:
- version_status: Optional. One of "draft", "published", "production". If omitted, all versions are returned.
Example Request:
curl -X GET "http://localhost:4000/policies/list" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
curl -X GET "http://localhost:4000/policies/list?version_status=production" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Example Response:
{
"policies": [
{
"policy_id": "123e4567-e89b-12d3-a456-426614174000",
"policy_name": "global-baseline",
"version_number": 1,
"version_status": "production",
"inherit": null,
"description": "Base guardrails for all requests",
"guardrails_add": ["pii_masking"],
"guardrails_remove": [],
"condition": null,
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
}
],
"total_count": 1
}
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
version_status | query | string or null | No |
Example request:
curl -X GET "http://localhost:4000/policies/list" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyListDBResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The database raised an error. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
policies | array of PolicyDBResponse | No | List of policies. |
total_count | integer | No | Total number of policies. |
DELETE /policies/name/{policy_name}/all-versions
Delete every version of a policy
Delete every stored version of the policy with this name, including the production version, so the gateway
stops applying it. Attachments that name the policy are left in place. When a config file policy has the same
name, the answer carries a warning that the config version is enforced again. Proxy admins only. The change
is written to the audit log.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_name | path | string | Yes |
Example request:
curl -X DELETE "http://localhost:4000/policies/name/policy_name-123/all-versions" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Every version is gone and the gateway stops applying the policy. | application/json PolicyDeletedResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change a policy. Admin viewers can only read | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
message | string | Yes | Which policy or attachment was deleted. |
warning | string or null | No | Set when the deletion changed what the gateway enforces, for example production went. |
GET /policies/name/{policy_name}/versions
List the versions of a policy
List every version of the policy with this name, newest first, with each version's status. Use it to find the
policy_id of the draft to edit or the version to promote.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_name | path | string | Yes |
Example request:
curl -X GET "http://localhost:4000/policies/name/policy_name-123/versions" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyVersionListResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
policy_name | string | Yes | Name of the policy. |
total_count | integer | No | Total number of versions. |
versions | array of PolicyDBResponse | No | All versions ordered by version_number desc. |
POST /policies/name/{policy_name}/versions
Create a draft version of a policy
Open a new draft of the policy by copying every field of the production version, or of the version named by
source_policy_id. The draft gets the next version number and changes nothing until it is published and
promoted. Proxy admins only.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_name | path | string | Yes |
Request body (application/json, schema PolicyVersionCreateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
source_policy_id | string or null | No | Policy ID to clone from. If None, clone from current production version. |
Example request:
curl -X POST "http://localhost:4000/policies/name/policy_name-123/versions" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"source_policy_id": "0b3f6c2d-8e4a-4f1b-9c7d-5a6e8f9b0c1d"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The new draft. | application/json PolicyDBResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change a policy. Admin viewers can only read | application/json ErrorDetail |
| 404 | The policy has no version to copy from. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
condition | object or null | No | Policy condition. |
created_at | string or null | No | When the policy was created. |
created_by | string or null | No | Who created the policy. |
definition_location | enum: "db", "config" | No | Where this policy is defined: 'db' (database) or 'config' (config.yaml). |
description | string or null | No | Policy description. |
guardrails_add | array of string | No | Guardrails to add. |
guardrails_remove | array of string | No | Guardrails to remove. |
inherit | string or null | No | Parent policy name. |
is_latest | boolean | No | True if this is the latest version by version_number. |
parent_version_id | string or null | No | Policy ID this version was cloned from. |
pipeline | object or null | No | Optional guardrail pipeline. |
policy_id | string | Yes | Unique ID of the policy. |
policy_name | string | Yes | Name of the policy. |
production_at | string or null | No | When this version was promoted to production. |
published_at | string or null | No | When this version was published. |
updated_at | string or null | No | When the policy was last updated. |
updated_by | string or null | No | Who last updated the policy. |
version_number | integer | No | Version number of this policy. |
version_status | string | No | One of: draft, published, production. |
POST /policies/resolve
Simulate which policies and guardrails a request would get
Work out which policy attachments match a request with this team alias, key alias, model and tags, and which
guardrails it would run, without sending a request. Every field is optional. The answer names each matched
policy and what matched it, such as team:research or scope:*. The Policy Simulator tab of the Admin UI calls
this route. Pass force_sync=true to read the attachments from the database instead of the in-memory copy.
Docs (https://docs.spanway.ai/docs/proxy/guardrails/policy_simulation)
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
force_sync | query | boolean | No | Force a DB sync before resolving. Default uses in-memory cache. |
Request body (application/json, schema PolicyResolveRequest):
| Field | Type | Required | Description |
|---|---|---|---|
key_alias | string or null | No | Key alias to resolve for. |
model | string or null | No | Model name to resolve for. |
tags | array of string or null | No | Tags to resolve for. |
team_alias | string or null | No | Team alias to resolve for. |
Example request:
curl -X POST "http://localhost:4000/policies/resolve" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"key_alias": "research-batch",
"model": "claude-sonnet-5-5",
"tags": [
"health-eu"
],
"team_alias": "research"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicyResolveResponse |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so attachments cannot be read. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
effective_guardrails | array of string | No | Final list of guardrails that would be applied. |
matched_policies | array of PolicyMatchDetail | No | Details about each matched policy and why it matched. |
POST /policies/simulate
Replay recent traffic through a policy
Try a policy on real traffic before it goes live. The gateway takes the newest stored requests of one team or
one virtual key from the last few hours, runs each through the policy's request guardrails in audit mode and
returns, per guardrail, how many requests it would have blocked, redacted or passed. Nothing is blocked, no
model is called and no budget or rate limit counter moves. Replays need stored request bodies
(general_settings.store_prompts_in_spend_logs: true), requests without one are counted in
requests_without_content. Each run writes an invoked audit row on the policy table.
Example curl:
curl -X POST http://0.0.0.0:4000/policies/simulate \
-H "Authorization: Bearer sk-admin" -H "Content-Type: application/json" \
-d '{"policy_name": "pii-draft", "team_id": "team-support", "hours": 24, "sample_size": 50}'
Request body (application/json, schema PolicySimulateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
guardrails | array of string or null | No | Draft policy as a list of guardrail names. Set one of the three policy fields. |
hours | integer | No | How far back to sample, in hours. |
key_hash | string or null | No | Replay traffic of this hashed virtual key (the token column). Set this or team_id. |
policy_id | string or null | No | Stored policy version to replay, drafts included, by its policy_id. |
policy_name | string or null | No | Production policy to replay. Set one of policy_name, policy_id or guardrails. |
sample_size | integer | No | Most recent requests to replay, newest first. |
team_id | string or null | No | Replay traffic of this team. Set this or key_hash. |
Example request:
curl -X POST "http://localhost:4000/policies/simulate" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Successful Response | application/json PolicySimulateResponse |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | Only proxy admins can replay stored traffic. | application/json ErrorDetail |
| 404 | The named policy or policy version does not exist. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 503 | The gateway has no database, so there is no stored traffic. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
guardrails | array of GuardrailSimulationCounts | Yes | Per guardrail block, redact and pass counts. |
policy_name | string | Yes | Policy that was replayed, or 'draft' for a guardrail list. |
requests_blocked | integer | Yes | Replayed requests at least one guardrail would have blocked. |
requests_considered | integer | Yes | Stored requests found in the window. |
requests_replayed | integer | Yes | Requests that had stored content and were replayed. |
requests_without_content | integer | Yes | Requests skipped because the spend log holds no request body (prompt storage was off). |
skipped_guardrails | array of SkippedGuardrail | No | Guardrails of the policy that the replay cannot run. |
POST /policies/test-pipeline
Run a guardrail pipeline on sample messages
Run a pipeline definition against sample messages without saving it, and report what each step did, how long it took and the final decision. The guardrails really run, so a guardrail that calls a paid service is billed. The Test Pipeline panel of the Admin UI calls this route.
Example Request:
curl -X POST "http://localhost:4000/policies/test-pipeline" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"pipeline": {
"mode": "pre_call",
"steps": [
{"guardrail": "pii-guard", "on_pass": "next", "on_fail": "block"}
]
},
"test_messages": [{"role": "user", "content": "My SSN is 123-45-6789"}]
}'
Request body (application/json, schema PipelineTestRequest):
| Field | Type | Required | Description |
|---|---|---|---|
pipeline | object | Yes | Pipeline definition with 'mode' and 'steps'. |
test_messages | array of object | Yes | Test messages to run through the pipeline, e.g. [{'role': 'user', 'content': '...'}]. |
Example request:
curl -X POST "http://localhost:4000/policies/test-pipeline" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"pipeline": {
"mode": "pre_call",
"steps": [
{
"guardrail": "pii-mask",
"on_fail": "block",
"on_pass": "next"
}
]
},
"test_messages": [
{
"content": "My card is 4111 1111 1111 1111",
"role": "user"
}
]
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The decision and one result per step. | application/json PipelineTestResponse |
| 400 | The pipeline definition is not valid. | application/json ErrorDetail |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | A guardrail raised an error the pipeline could not handle. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
error_message | string or null | No | The reason a step blocked, when one did. |
modified_data | object or null | No | The request after the steps that rewrite content, when one did. |
modify_response_message | string or null | No | The custom response text of a modify_response step, when one fired. |
step_results | array of PipelineStepResult | Yes | One entry per step, in the order they ran. |
terminal_action | string | Yes | What the pipeline decided for the request: allow, block or modify_response. |
GET /policies/{policy_id}
Read one policy version
Read one policy version by its policy_id.
Example Request:
curl -X GET "http://localhost:4000/policies/123e4567-e89b-12d3-a456-426614174000" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_id | path | string | Yes |
Example request:
curl -X GET "http://localhost:4000/policies/policy_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The policy version as stored. | application/json PolicyDBResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 404 | No policy version has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
condition | object or null | No | Policy condition. |
created_at | string or null | No | When the policy was created. |
created_by | string or null | No | Who created the policy. |
definition_location | enum: "db", "config" | No | Where this policy is defined: 'db' (database) or 'config' (config.yaml). |
description | string or null | No | Policy description. |
guardrails_add | array of string | No | Guardrails to add. |
guardrails_remove | array of string | No | Guardrails to remove. |
inherit | string or null | No | Parent policy name. |
is_latest | boolean | No | True if this is the latest version by version_number. |
parent_version_id | string or null | No | Policy ID this version was cloned from. |
pipeline | object or null | No | Optional guardrail pipeline. |
policy_id | string | Yes | Unique ID of the policy. |
policy_name | string | Yes | Name of the policy. |
production_at | string or null | No | When this version was promoted to production. |
published_at | string or null | No | When this version was published. |
updated_at | string or null | No | When the policy was last updated. |
updated_by | string or null | No | Who last updated the policy. |
version_number | integer | No | Version number of this policy. |
version_status | string | No | One of: draft, published, production. |
PUT /policies/{policy_id}
Change a draft policy version
Change the fields you send on a draft version and keep the rest. Published and production versions are
read only, so open a draft with POST /policies/name/{policy_name}/versions first. Proxy admins only. The
change is written to the audit log.
Example Request:
curl -X PUT "http://localhost:4000/policies/123e4567-e89b-12d3-a456-426614174000" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"description": "Updated description",
"guardrails_add": ["pii_masking", "toxicity_filter"]
}'
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_id | path | string | Yes |
Request body (application/json, schema PolicyUpdateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
condition | PolicyConditionRequest or null | No | Condition for when this policy applies. |
description | string or null | No | Human-readable description of the policy. |
guardrails_add | array of string or null | No | List of guardrail names to add. |
guardrails_remove | array of string or null | No | List of guardrail names to remove (from inherited). |
inherit | string or null | No | Name of parent policy to inherit from. |
pipeline | object or null | No | Optional guardrail pipeline for ordered execution. Contains 'mode' and 'steps'. |
policy_name | string or null | No | New name for the policy. |
Example request:
curl -X PUT "http://localhost:4000/policies/policy_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"description": "Extra checks for frontier models",
"guardrails_add": [
"prompt-injection",
"pii-mask"
]
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The draft as stored. | application/json PolicyDBResponse |
| 400 | The version is published or in production. | application/json ErrorDetail |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change a policy. Admin viewers can only read | application/json ErrorDetail |
| 404 | No policy version has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
condition | object or null | No | Policy condition. |
created_at | string or null | No | When the policy was created. |
created_by | string or null | No | Who created the policy. |
definition_location | enum: "db", "config" | No | Where this policy is defined: 'db' (database) or 'config' (config.yaml). |
description | string or null | No | Policy description. |
guardrails_add | array of string | No | Guardrails to add. |
guardrails_remove | array of string | No | Guardrails to remove. |
inherit | string or null | No | Parent policy name. |
is_latest | boolean | No | True if this is the latest version by version_number. |
parent_version_id | string or null | No | Policy ID this version was cloned from. |
pipeline | object or null | No | Optional guardrail pipeline. |
policy_id | string | Yes | Unique ID of the policy. |
policy_name | string | Yes | Name of the policy. |
production_at | string or null | No | When this version was promoted to production. |
published_at | string or null | No | When this version was published. |
updated_at | string or null | No | When the policy was last updated. |
updated_by | string or null | No | Who last updated the policy. |
version_number | integer | No | Version number of this policy. |
version_status | string | No | One of: draft, published, production. |
DELETE /policies/{policy_id}
Delete one policy version
Delete one version of a policy by its policy_id. Deleting the production version stops the policy from being
enforced until another version is promoted, and the answer says so in warning. Proxy admins only. The change
is written to the audit log.
Example Request:
curl -X DELETE "http://localhost:4000/policies/123e4567-e89b-12d3-a456-426614174000" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Example Response:
{
"message": "Policy 123e4567-e89b-12d3-a456-426614174000 deleted successfully"
}
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_id | path | string | Yes |
Example request:
curl -X DELETE "http://localhost:4000/policies/policy_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The version is gone. A warning says when it was the production version. | application/json PolicyDeletedResponse |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change a policy. Admin viewers can only read | application/json ErrorDetail |
| 404 | No policy version has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
message | string | Yes | Which policy or attachment was deleted. |
warning | string or null | No | Set when the deletion changed what the gateway enforces, for example production went. |
GET /policies/{policy_id}/resolved-guardrails
List the guardrails a policy resolves to
Walk the inheritance chain of this policy version and return the guardrails it ends up running, after the
parent's guardrails, guardrails_add and guardrails_remove are applied. The version you name is resolved even
when it is a draft, and its parents are resolved at their production versions. Answers 400 when a parent has no
production version or the chain loops.
Example Request:
curl -X GET "http://localhost:4000/policies/123e4567-e89b-12d3-a456-426614174000/resolved-guardrails" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Example Response:
{
"policy_id": "123e4567-e89b-12d3-a456-426614174000",
"policy_name": "healthcare-compliance",
"resolved_guardrails": ["pii_masking", "prompt_injection", "toxicity_filter"]
}
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_id | path | string | Yes |
Example request:
curl -X GET "http://localhost:4000/policies/policy_id-123/resolved-guardrails" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The final guardrail list. | application/json PolicyResolvedGuardrailsResponse |
| 400 | The inheritance chain is broken, for example the parent does not exist or inherits in a cycle. | application/json ErrorDetail |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 404 | No policy version has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
policy_id | string | Yes | ID of the policy version that was resolved. |
policy_name | string | Yes | Name of the policy. |
resolved_guardrails | array of string | No | Every guardrail the policy runs once inheritance, additions and removals are applied. |
PUT /policies/{policy_id}/status
Publish or promote a policy version
Move a policy version along: a draft becomes published, a published version becomes production and the
previous production version drops back to published. A production version cannot be set back to published
directly, promote another version instead. Only the production version is applied to traffic. Proxy admins only.
The change, and the demotion of the previous production version, are written to the audit log with the value
before and after.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
policy_id | path | string | Yes |
Request body (application/json, schema PolicyVersionStatusUpdateRequest):
| Field | Type | Required | Description |
|---|---|---|---|
version_status | string | Yes | New status: 'published' or 'production'. |
Example request:
curl -X PUT "http://localhost:4000/policies/policy_id-123/status" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"version_status": "production"
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The policy version as stored. | application/json PolicyDBResponse |
| 400 | The transition is not allowed, for example a draft straight to production. | application/json ErrorDetail |
| 401 | No key was sent, or the key is not a proxy admin key. A non-admin key gets the same status with the message Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. | application/json ProxyError |
| 403 | An admin viewer key tried to change a policy. Admin viewers can only read | application/json ErrorDetail |
| 404 | No policy version has this id. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | The gateway runs without a database, so policies cannot be stored. | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
condition | object or null | No | Policy condition. |
created_at | string or null | No | When the policy was created. |
created_by | string or null | No | Who created the policy. |
definition_location | enum: "db", "config" | No | Where this policy is defined: 'db' (database) or 'config' (config.yaml). |
description | string or null | No | Policy description. |
guardrails_add | array of string | No | Guardrails to add. |
guardrails_remove | array of string | No | Guardrails to remove. |
inherit | string or null | No | Parent policy name. |
is_latest | boolean | No | True if this is the latest version by version_number. |
parent_version_id | string or null | No | Policy ID this version was cloned from. |
pipeline | object or null | No | Optional guardrail pipeline. |
policy_id | string | Yes | Unique ID of the policy. |
policy_name | string | Yes | Name of the policy. |
production_at | string or null | No | When this version was promoted to production. |
published_at | string or null | No | When this version was published. |
updated_at | string or null | No | When the policy was last updated. |
updated_by | string or null | No | Who last updated the policy. |
version_number | integer | No | Version number of this policy. |
version_status | string | No | One of: draft, published, production. |