Skip to content

Gateway API

The Crafted AI gateway exposes a single OpenAI-compatible endpoint:

POST /v1/chat/completions

It accepts the same request shape as https://api.openai.com/v1/chat/completions and returns the same response shape — including for Anthropic models, which the gateway translates transparently.

EnvironmentBase URL
Staginghttps://api-staging.craftedai.co/v1
Productionhttps://api.craftedai.co/v1 (coming soon)

Send your gateway key in the Authorization header as a Bearer token:

Authorization: Bearer cai_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Gateway keys are minted in the control panel and look like cai_…. They authenticate the request and identify the organisation, key, and any configured rate limits — no separate org header is needed.

Bodies are JSON, capped at 4 MB. The shape mirrors OpenAI’s chat.completions.create parameters.

Terminal window
curl "https://api-staging.craftedai.co/v1/chat/completions" \
-H "Authorization: Bearer $CRAFTED_AI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{ "role": "system", "content": "You are concise." },
{ "role": "user", "content": "Summarise SOLID in one sentence." }
],
"temperature": 0.2
}'

Set stream: true to receive Server-Sent Events. The gateway injects stream_options.include_usage = true automatically, so the final SSE chunk carries the same usage block you would see on a non-streamed response. This keeps usage tracking accurate without any client-side change.

const stream = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Tell me a joke.' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}

Errors follow the OpenAI envelope so existing SDK error-handling continues to work. The shape is:

{
"error": {
"message": "The model `gpt-foo` does not exist or is not configured for your organization.",
"type": "invalid_request_error",
"param": "model",
"code": "model_not_found"
}
}

Common error codes:

HTTPcodeMeaning
400invalid_request_errorBody failed validation.
400unsupported_parameterA field is recognised but not accepted (e.g. legacy params).
401invalid_api_keyMissing or unrecognised cai_… key.
404model_not_foundModel is not configured for your organisation, or doesn’t exist.
429rate_limit_exceededPer-key RPM or daily-token cap hit.
502upstream_unavailableThe upstream provider returned an unrecoverable error.
503rate_limiter_unavailableRate-limiter Redis outage; gateway fails closed for billing safety.

User-supplied tools[] are passed through to the upstream provider unchanged. Crafted AI plugin orchestration only activates when your organisation has plugins configured and the request omits tools.

When orchestration is active, the gateway can inject first-party and MCP-backed plugin tools, execute selected tool calls server-side, append tool results, and continue the model loop until a final assistant response is produced. Tool names are prefixed with the plugin slug, such as support-crm__lookup_customer.

For WhatsApp inbound replies, the reply worker calls the same gateway runtime with the WhatsApp app’s enabledPluginIds, so only the selected plugins are eligible for injection.

Read Plugins for the runtime rules and MCP integrations for installation.