Skip to main content

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.

MethodPathSummary
POST/rag/ingestIngest a document into a vector store
POST/v1/rag/ingestIngest a document into a vector store
POST/rag/queryAnswer a question with retrieval augmented generation
POST/v1/rag/queryAnswer 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):

FieldTypeRequiredDescription
file_urlstring or nullNoFile Url
file_idstring or nullNoFile Id
ingest_optionsobjectYesIngest Options

Request body (multipart/form-data, object):

FieldTypeRequiredDescription
filestringYesThe document to ingest
requeststringYesThe 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:

StatusDescriptionBody
200The ingest job with the vector store and file it wrote.application/json object
400The 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
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The 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
408The provider did not answer before the request timed out.application/json ProxyError
413The 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. Docsapplication/json object
422The 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 errorapplication/json ProxyError or HTTPValidationError
429Too 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 errorapplication/json ProxyError
500The 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
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The 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. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstringNoId
statusenum: "completed", "in_progress", "failed"NoStatus
vector_store_idstringNoVector Store Id
file_idstring or nullNoFile Id
errorstring or nullNoError

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):

FieldTypeRequiredDescription
file_urlstring or nullNoFile Url
file_idstring or nullNoFile Id
ingest_optionsobjectYesIngest Options

Request body (multipart/form-data, object):

FieldTypeRequiredDescription
filestringYesThe document to ingest
requeststringYesThe 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:

StatusDescriptionBody
200The ingest job with the vector store and file it wrote.application/json object
400The 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
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The 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
408The provider did not answer before the request timed out.application/json ProxyError
413The 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. Docsapplication/json object
422The 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 errorapplication/json ProxyError or HTTPValidationError
429Too 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 errorapplication/json ProxyError
500The 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
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The 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. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstringNoId
statusenum: "completed", "in_progress", "failed"NoStatus
vector_store_idstringNoVector Store Id
file_idstring or nullNoFile Id
errorstring or nullNoError

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):

FieldTypeRequiredDescription
modelstringYesModel
messagesarray of anyYesMessages
retrieval_configobjectYesConfiguration for vector store retrieval.
rerankobject or nullNo
streamboolean or nullNoStream

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:

StatusDescriptionBody
200The grounded answer in the chat completion shape.application/json object
400The 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
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The 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
408The provider did not answer before the request timed out.application/json ProxyError
413The 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. Docsapplication/json object
422The 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 errorapplication/json ProxyError or HTTPValidationError
429Too 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 errorapplication/json ProxyError
500The 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
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The 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. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstringYesId
createdintegerYesCreated
modelstring or nullNoModel
objectstringYesObject
system_fingerprintstring or nullNoSystem Fingerprint
choicesarray of objectYesChoices

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):

FieldTypeRequiredDescription
modelstringYesModel
messagesarray of anyYesMessages
retrieval_configobjectYesConfiguration for vector store retrieval.
rerankobject or nullNo
streamboolean or nullNoStream

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:

StatusDescriptionBody
200The grounded answer in the chat completion shape.application/json object
400The 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
401No valid key was sent, or the key has expired or is blocked.application/json ProxyError
403The key is valid but may not use this model, route or feature. A guardrail block is a 400, not a 403.application/json ProxyError
404The 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
408The provider did not answer before the request timed out.application/json ProxyError
413The 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. Docsapplication/json object
422The 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 errorapplication/json ProxyError or HTTPValidationError
429Too 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 errorapplication/json ProxyError
500The 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
502The provider, or a proxy in front of it, answered with something that is not a valid response.application/json ProxyError
503The 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. Docsapplication/json ProxyError

Response fields (200):

FieldTypeRequiredDescription
idstringYesId
createdintegerYesCreated
modelstring or nullNoModel
objectstringYesObject
system_fingerprintstring or nullNoSystem Fingerprint
choicesarray of objectYesChoices