AI API

Chat completions and embeddings on the project's own AI key, with per-app and per-user budgets.

2 endpointsHTTP API referenceGuide: In-app AI on your own key

On this page
  1. Authentication
  2. Endpoints
  3. POST /ai/chat
  4. POST /ai/embed

Base URL: your app's origin ($APP_ORIGIN): the dev site (https://dev-<slug>.apps.appbricx.com), the published host or a custom domain. Errors answer { ok: false, error: { code, message } } unless noted. The host decides the environment: the dev site reads and writes the dev database, the published host and custom domains production's.

Authentication

App key
A project key identifies the app. No user session is needed. Authorization: Bearer $APP_KEY

Endpoints

Chat completion

POST/__appbricx/ai/chat

One chat completion with the project's own AI key (the AI_API_KEY secret, else ANTHROPIC_API_KEY, else OPENAI_API_KEY). Streams server-sent events by default; send stream: false for one JSON answer. The project's system prompt is prepended.

Auth
App key: Authorization: Bearer $APP_KEY

Body

NameTypeDescription
messagesrequiredArray<{ role, content }>1 to 64 messages; role is system, user or assistant.
streamoptionalbooleanDefault true (SSE). false answers JSON.
max_tokensoptionalnumberUpper bound on the answer (the project cap applies).

Response

Streaming: data: {"type":"text_delta","data":"…"} events, then {"type":"done","data":{ usage }} and [DONE].

Response
{
  "ok": true,
  "content": "The floor 3 printer jams when printing on A3.",
  "usage": {
    "prompt_tokens": 21,
    "completion_tokens": 11
  },
  "elapsed_ms": 840
}

Errors

StatusCodeWhen
401UNAUTHORIZEDMissing or invalid Authorization bearer.
429RATE_LIMITEDThe project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user).
403TOOL_NOT_ALLOWEDThe key may not call ai.chat.
400PARAMS_INVALIDmessages missing, too many, or malformed.
413PAYLOAD_TOO_LARGEBody over 1 MB.
400INPUT_TOO_LARGEPrompt over the project's input cap.
402BUDGET_EXCEEDEDPer-user or project token budget spent.
403MODEL_NOT_ALLOWEDThe model isn't on the project's allow-list.
503AI_DISABLED_FOR_PROJECTAI is off for this project.
503PROVIDER_ERRORThe provider failed.
curl
curl -X POST "$APP_ORIGIN/__appbricx/ai/chat" \
  -H "Authorization: Bearer $APP_KEY" \
  -H "content-type: application/json" \
  -d '{"messages":[{"role":"user","content":"Summarize: the printer on floor 3 jams on A3 paper."}],"stream":false,"max_tokens":120}'
fetch
const res = await fetch(`${APP_ORIGIN}/__appbricx/ai/chat`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${APP_KEY}`,
    "content-type": "application/json",
  },
  body: JSON.stringify({
    "messages": [
      {
        "role": "user",
        "content": "Summarize: the printer on floor 3 jams on A3 paper."
      }
    ],
    "stream": false,
    "max_tokens": 120
  }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? data.error ?? res.statusText);

Source: internal/svc/runtime/data/owner_http.go:50

Embeddings

POST/__appbricx/ai/embed

Embedding vectors for a batch of texts with the configured embedding provider.

Auth
App key: Authorization: Bearer $APP_KEY

Body

NameTypeDescription
textsrequiredstring[]1 to 100 texts, each up to 8192 characters.

Response

Response
{
  "ok": true,
  "vectors": [
    [
      0.012,
      -0.034,
      "…"
    ],
    [
      0.021,
      0.004,
      "…"
    ]
  ],
  "model": "text-embedding-3-small",
  "dimensions": 1536,
  "elapsed_ms": 210
}

Errors

StatusCodeWhen
401UNAUTHORIZEDMissing or invalid Authorization bearer.
429RATE_LIMITEDThe project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user).
403TOOL_NOT_ALLOWEDThe key may not call ai.embed.
400PARAMS_INVALIDtexts missing, too many or too long.
402BUDGET_EXCEEDEDBudget spent.
503AI_DISABLED_FOR_PROJECTAI is off.
503EMBEDDING_NOT_CONFIGUREDNo embedding provider.
503PROVIDER_ERRORThe provider failed.
curl
curl -X POST "$APP_ORIGIN/__appbricx/ai/embed" \
  -H "Authorization: Bearer $APP_KEY" \
  -H "content-type: application/json" \
  -d '{"texts":["printer jams on A3","projector won't turn on"]}'
fetch
const res = await fetch(`${APP_ORIGIN}/__appbricx/ai/embed`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${APP_KEY}`,
    "content-type": "application/json",
  },
  body: JSON.stringify({
    "texts": [
      "printer jams on A3",
      "projector won't turn on"
    ]
  }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? data.error ?? res.statusText);

Source: internal/svc/runtime/data/owner_http.go:51