Rag API
This page documents the 4 gateway endpoints tagged rag 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 | /rag/ingest | Ingest a document into a vector store |
| POST | /v1/rag/ingest | Ingest a document into a vector store |
| POST | /rag/query | Answer a question with retrieval augmented generation |
| POST | /v1/rag/query | Answer a question with retrieval augmented generation |
Endpoints
POST /rag/ingest
Ingest a document into a vector store
Add a document to a vector store in one call, so models can answer questions from it later. The gateway reads the document, runs OCR on PDFs when an OCR model is set, splits it into chunks, embeds them and writes them to the store, creating a new store unless vector_store_id names one. Any key may call it with access to the store, the embedding model and the OCR model, and the internal user viewer role may only add to an existing store. Upload the file as a multipart form with its options as JSON in the request field, or send a JSON body with file_url or file_id. Each step is logged and billed under the same request id. Docs
Request body (application/json, object):
| Field | Type | Required | Description |
|---|---|---|---|
file_url | string or null | No | File Url |
file_id | string or null | No | File Id |
ingest_options | object | Yes | Ingest Options |
Request body (multipart/form-data, object):
| Field | Type | Required | Description |
|---|---|---|---|
file | string | Yes | The document to ingest |
request | string | Yes | The rest of the request as JSON, at least ingest_options with a vector_store block |
Example request:
curl -X POST "http://localhost:4000/rag/ingest" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"file_url": "https://example.com/handbook.pdf",
"ingest_options": {
"vector_store": {
"custom_llm_provider": "openai",
"vector_store_id": "vs_hr_policies"
},
"chunking_strategy": {
"chunk_size": 800,
"chunk_overlap": 100
},
"embedding": {
"model": "text-embedding-3-large"
}
}
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The ingest job with the vector store and file it wrote. | application/json object |
| 400 | The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which. | application/json ProxyError |
| 401 | No valid key was sent, or the key has expired or is blocked. | application/json ProxyError |
| 403 | The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403. | application/json ProxyError |
| 404 | The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...). | application/json ProxyError |
| 408 | The provider did not answer before the request timed out. | application/json ProxyError |
| 413 | The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docs | application/json object |
| 422 | The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError or HTTPValidationError |
| 429 | Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError |
| 500 | The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing. | application/json ProxyError |
| 502 | The provider, or a proxy in front of it, answered with something that is not a valid response. | application/json ProxyError |
| 503 | The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docs | application/json ProxyError |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
id | string | No | Id |
status | enum: "completed", "in_progress", "failed" | No | Status |
vector_store_id | string | No | Vector Store Id |
file_id | string or null | No | File Id |
error | string or null | No | Error |
POST /v1/rag/ingest
Ingest a document into a vector store
Add a document to a vector store in one call, so models can answer questions from it later. The gateway reads the document, runs OCR on PDFs when an OCR model is set, splits it into chunks, embeds them and writes them to the store, creating a new store unless vector_store_id names one. Any key may call it with access to the store, the embedding model and the OCR model, and the internal user viewer role may only add to an existing store. Upload the file as a multipart form with its options as JSON in the request field, or send a JSON body with file_url or file_id. Each step is logged and billed under the same request id. Docs
Request body (application/json, object):
| Field | Type | Required | Description |
|---|---|---|---|
file_url | string or null | No | File Url |
file_id | string or null | No | File Id |
ingest_options | object | Yes | Ingest Options |
Request body (multipart/form-data, object):
| Field | Type | Required | Description |
|---|---|---|---|
file | string | Yes | The document to ingest |
request | string | Yes | The rest of the request as JSON, at least ingest_options with a vector_store block |
Example request:
curl -X POST "http://localhost:4000/v1/rag/ingest" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"file_url": "https://example.com/handbook.pdf",
"ingest_options": {
"vector_store": {
"custom_llm_provider": "openai",
"vector_store_id": "vs_hr_policies"
},
"chunking_strategy": {
"chunk_size": 800,
"chunk_overlap": 100
},
"embedding": {
"model": "text-embedding-3-large"
}
}
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The ingest job with the vector store and file it wrote. | application/json object |
| 400 | The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which. | application/json ProxyError |
| 401 | No valid key was sent, or the key has expired or is blocked. | application/json ProxyError |
| 403 | The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403. | application/json ProxyError |
| 404 | The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...). | application/json ProxyError |
| 408 | The provider did not answer before the request timed out. | application/json ProxyError |
| 413 | The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docs | application/json object |
| 422 | The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError or HTTPValidationError |
| 429 | Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError |
| 500 | The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing. | application/json ProxyError |
| 502 | The provider, or a proxy in front of it, answered with something that is not a valid response. | application/json ProxyError |
| 503 | The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docs | application/json ProxyError |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
id | string | No | Id |
status | enum: "completed", "in_progress", "failed" | No | Status |
vector_store_id | string | No | Vector Store Id |
file_id | string or null | No | File Id |
error | string or null | No | Error |
POST /rag/query
Answer a question with retrieval augmented generation
Answer a question from your own documents. The gateway takes the last user message as the query, searches the vector store in retrieval_config, optionally reranks the matches, and sends the best chunks with the messages to the model. Any key may call it with access to the store, the rerank model and the model. Nothing is stored beyond the usual spend log, which records the search, rerank and completion under one request id. Docs
Request body (application/json, object):
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model |
messages | array of any | Yes | Messages |
retrieval_config | object | Yes | Configuration for vector store retrieval. |
rerank | object or null | No | |
stream | boolean or null | No | Stream |
Example request:
curl -X POST "http://localhost:4000/rag/query" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"messages": [
{
"role": "user",
"content": "How many weeks of parental leave do we offer?"
}
],
"retrieval_config": {
"vector_store_id": "vs_hr_policies",
"custom_llm_provider": "openai",
"top_k": 8
},
"rerank": {
"enabled": true,
"model": "rerank-v3.5",
"top_n": 3,
"return_documents": true
},
"stream": false
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The grounded answer in the chat completion shape. | application/json object |
| 400 | The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which. | application/json ProxyError |
| 401 | No valid key was sent, or the key has expired or is blocked. | application/json ProxyError |
| 403 | The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403. | application/json ProxyError |
| 404 | The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...). | application/json ProxyError |
| 408 | The provider did not answer before the request timed out. | application/json ProxyError |
| 413 | The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docs | application/json object |
| 422 | The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError or HTTPValidationError |
| 429 | Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError |
| 500 | The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing. | application/json ProxyError |
| 502 | The provider, or a proxy in front of it, answered with something that is not a valid response. | application/json ProxyError |
| 503 | The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docs | application/json ProxyError |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Id |
created | integer | Yes | Created |
model | string or null | No | Model |
object | string | Yes | Object |
system_fingerprint | string or null | No | System Fingerprint |
choices | array of object | Yes | Choices |
POST /v1/rag/query
Answer a question with retrieval augmented generation
Answer a question from your own documents. The gateway takes the last user message as the query, searches the vector store in retrieval_config, optionally reranks the matches, and sends the best chunks with the messages to the model. Any key may call it with access to the store, the rerank model and the model. Nothing is stored beyond the usual spend log, which records the search, rerank and completion under one request id. Docs
Request body (application/json, object):
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model |
messages | array of any | Yes | Messages |
retrieval_config | object | Yes | Configuration for vector store retrieval. |
rerank | object or null | No | |
stream | boolean or null | No | Stream |
Example request:
curl -X POST "http://localhost:4000/v1/rag/query" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"messages": [
{
"role": "user",
"content": "How many weeks of parental leave do we offer?"
}
],
"retrieval_config": {
"vector_store_id": "vs_hr_policies",
"custom_llm_provider": "openai",
"top_k": 8
},
"rerank": {
"enabled": true,
"model": "rerank-v3.5",
"top_n": 3,
"return_documents": true
},
"stream": false
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The grounded answer in the chat completion shape. | application/json object |
| 400 | The request is invalid, for example a missing or unsupported parameter, a prompt longer than the model's context window, content a guardrail or the provider refused, or no deployment that satisfies the data policy of the key, team or organization (No deployment of model 'gpt-5.5' satisfies the data policy: ...). error.message says which. | application/json ProxyError |
| 401 | No valid key was sent, or the key has expired or is blocked. | application/json ProxyError |
| 403 | The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403. | application/json ProxyError |
| 404 | The route or resource does not exist, or this key cannot see it. An unknown model name is a 400 (Invalid model name passed in model=...). | application/json ProxyError |
| 408 | The provider did not answer before the request timed out. | application/json ProxyError |
| 413 | The request body is larger than max_request_size_mb (100 MB by default), or an upload is larger than its own limit. The gateway refused it before reading the key. Docs | application/json object |
| 422 | The request body does not have the expected shape, or a budget (key, user, team, organization or end user) is used up: error.type is budget_exceeded. The budget status is spanway_settings.budget_exceeded_status_code, 422 by default. With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError or HTTPValidationError |
| 429 | Too many requests. A provider limit or a gateway limit (requests or tokens per minute, or parallel requests) refused the call, and error.type says which. A gateway limit also sends the RateLimit headers and x-spanway-rate-limited-by. Docs With access requests licensed, a key or team limit refusal also carries error.request_increase, which names the access request to file for a higher limit. Ask from an error | application/json ProxyError |
| 500 | The gateway or the provider failed unexpectedly, or the gateway could not reach the provider. Retry, and quote the request id if it keeps failing. | application/json ProxyError |
| 502 | The provider, or a proxy in front of it, answered with something that is not a valid response. | application/json ProxyError |
| 503 | The provider is overloaded or unavailable. With Redis rate limiting, a 503 also means the gateway could not check the rate limit counters and refused the request, because fail_closed_rate_limit_enforcement is on. Docs | application/json ProxyError |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Id |
created | integer | Yes | Created |
model | string or null | No | Model |
object | string | Yes | Object |
system_fingerprint | string or null | No | System Fingerprint |
choices | array of object | Yes | Choices |