Skip to main content
POST
Chat Completion

Chat Completion

The main endpoint for AI chat completions with optional knowledge base retrieval (RAG), model redundancy/failover, per-call security policies, and streaming.

Endpoint

Description

The main endpoint for AI chat completions with optional knowledge base retrieval (RAG). It supports:
  • Two input forms — a single prompt string (legacy) or an OpenAI-style messages array.
  • Model redundancy — a caller-defined failover chain (primary + up to 2 fallbacks). See Redundancy & Failover.
  • Per-call security — SMLTP policy selection and an inline Prompt Shield override.
  • Streaming — Server-Sent Events (SSE).
  • Signed receipts — an SMLTP compliance receipt reference on responses routed through the gateway.
OpenAI SDK compatibilityIf you want to drop SecureAI into an existing OpenAI integration with zero code changes, use the OpenAI-compatible endpoint at /api/external/v1/chat/completions instead. This classic endpoint is the only one that supports RAG.

Authentication

Required: API Key

Headers

Request Body

Input parameters

Provide either prompt or messages — not both.

Model & redundancy parameters

Retrieval & generation parameters

Request Example

Response

Success Response (200)

Metadata Object

Streaming

Set "stream": true to receive Server-Sent Events. Each SSE line is data: <json> and the stream ends with data: [DONE]. Frames are typed via a type field:

Error Responses

400 Bad Request

401 Unauthorized

403 Forbidden

429 / 502 — Redundancy chain exhausted

When every model in a redundancy chain fails, the response reports each attempt. The status is 429 if all failures were rate limits, otherwise 502.

500 Internal Server Error

Example Usage

JavaScript/Node.js

Python

Notes

  • index is required. Send index: "Zero-Knowledge" for direct AI responses without RAG.
  • The user_id parameter bills the request to a different user account (admin-gated).
  • Temperature is clamped to 0–2; max_tokens is capped at 4000.
  • To validate a request against every policy without calling a model or spending points, use Policy Check.
  • For failover chain semantics (triggers, timeouts, streaming behavior, exhaustion status codes), see Redundancy & Failover.

Authorizations

Authorization
string
header
required

API key authentication using Bearer token format. Example: Authorization: Bearer sk-your-api-key-here

Body

application/json
prompt
string
required

The user's message/prompt

Example:

"What is the company's policy on remote work?"

model
string

The AI model to use for completion

Example:

"openai/gpt-4.1-mini"

index
string

Knowledge base to search for context (use 'Zero-Knowledge' for direct AI responses)

Example:

"my-knowledge-base"

smltp_policy
string

Security policy to apply

Example:

"internal"

temperature
number<float>
default:0.7

Controls randomness in the response (0 = deterministic, 2 = very random)

Required range: 0 <= x <= 2
Example:

0.7

max_tokens
integer
default:1000

Maximum number of tokens in the response

Required range: 1 <= x <= 4000
Example:

1000

stream
boolean
default:false

Whether to stream the response

Example:

false

conversation_id
string

Optional conversation ID for tracking

Example:

"conv-123"

system_message
string

Optional custom system message

Example:

"You are a helpful assistant."

use_rag
boolean
default:true

Whether to use RAG (knowledge base retrieval)

Example:

true

user_id
string

MongoDB ObjectId of the user to bill for this request. If not provided, the API key owner will be billed

Example:

"60a7c8f5e8b4f5001f7a8c23"

Response

Successful chat completion

success
boolean
Example:

true

id
string
Example:

"req-abc123"

object
string
Example:

"chat.completion"

created
integer
Example:

1705312200

model
string
Example:

"openai/gpt-4.1-mini"

choices
object[]
usage
object
metadata
object