Skip to content

API Reference

The UnifiedLayer REST API is OpenAI-compatible with a unifiedlayer extension for sources, credits, and analytics. You can use it from any server, script, or frontend — no SDK required.


Base URL

https://www.unifiedlayer.space

All endpoints are prefixed with /api/v1/. If you are self-hosting UnifiedLayer, replace the base URL with your own domain.


Authentication

Every API request requires authentication. UnifiedLayer supports two modes:

App-Key AuthSession Auth
Best forServers, scripts, backend APIs, curlBrowsers, mobile apps, client-side React
Headers requiredx-client-id + x-app-id + x-api-keyAuthorization: Bearer <token> + x-app-id
API key in browser?Yes — do not use client-sideNo — token is scoped and temporary
Per-user trackingNoYes — tied to a userId
Token expiryNever7 days
RevocableOnly by regenerating API keyYes — DELETE /api/v1/auth/session

App-Key Auth headers

x-client-id:  <your Client ID>      — found on the Profile page
x-app-id:     <your App ID>         — found on the App detail page
x-api-key:    <your API Key>        — found on the App detail page

Session Auth headers

Authorization: Bearer ul_sess_<token>
x-app-id:      <your App ID>

Which one should I use?

Use App-Key Auth from servers and Route Handlers. Use Session Auth in the browser. See the Session endpoint docs for the full token creation flow.


Response Format

Every endpoint that generates an AI response returns an UnifiedLayerChatCompletion object. This is a strict superset of the OpenAI chat.completion format — existing OpenAI clients can parse it without modification.

json
{
  "id": "ul-cmpl-abc123",
  "object": "chat.completion",
  "created": 1700000000,
  "model": "meta-llama/llama-4-maverick-17b-128e-instruct",
  "thread_id": "thread_abc123",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! I can help you with that.",
        "tool_calls": null
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 10,
    "total_tokens": 25
  },
  "unifiedlayer": {
    "sources": [
      {
        "filename": "support-docs.pdf",
        "text": "Our office hours are Monday to Friday, 9am–6pm EST.",
        "score": 0.92
      }
    ],
    "creditsLeft": 4.75,
    "creditsUsed": 0.0003,
    "analyticsId": "analytics_abc"
  }
}

Field reference

FieldTypeDescription
idstringUnique completion ID, prefixed ul-cmpl-.
objectstringAlways "chat.completion".
creatednumberUnix timestamp of when the completion was generated.
modelstringThe model that generated the response.
thread_idstringThe thread this message belongs to (thread-based endpoints only).
choices[].message.rolestringAlways "assistant" for AI responses.
choices[].message.contentstring | nullThe text response. null if the model only called tools.
choices[].message.tool_callsarray | nullTool calls requested by the model. null if none.
choices[].finish_reasonstring"stop" — natural end; "tool_calls" — model wants to call tools.
usage.prompt_tokensnumberTokens consumed by the input (messages + context).
usage.completion_tokensnumberTokens consumed by the model's output.
usage.total_tokensnumberSum of prompt + completion tokens.
unifiedlayer.sourcesarrayRetrieved document chunks used for context (empty if no Knowledge Base is attached).
unifiedlayer.sources[].filenamestringName of the source document.
unifiedlayer.sources[].textstringThe passage that was retrieved.
unifiedlayer.sources[].scorenumberCosine similarity score (0–1). Higher is more relevant.
unifiedlayer.creditsLeftnumberYour remaining credit balance after this request.
unifiedlayer.creditsUsednumberCredits deducted for this specific request.
unifiedlayer.analyticsIdstringAnalytics record ID for this call (visible in the dashboard).

Credits System

API calls that reach the LLM deduct credits from your Client balance based on token usage. Credits are deducted proportionally: a request that consumes more tokens costs more.

Every response includes unifiedlayer.creditsLeft and unifiedlayer.creditsUsed so you can monitor consumption in real time. When your balance reaches zero, all API calls return 402 Payment Required.

Top up your balance from the Billing section of the dashboard.

WARNING

If you are building for multiple end users, monitor creditsLeft to detect when your balance is low. Consider setting up an alert or auto-top-up to avoid service interruptions.


Endpoints Overview

MethodPathDescription
POST/api/v1/auth/sessionCreate a per-user session token
DELETE/api/v1/auth/sessionRevoke a session token
POST/api/v1/chat/completionsOpenAI-compatible stateless chat
GET/api/v1/threadsList threads
POST/api/v1/threadsCreate a new thread
GET/api/v1/threads/:threadIdGet a thread
DELETE/api/v1/threads/:threadIdDelete a thread
GET/api/v1/threads/:threadId/messagesList messages in a thread
POST/api/v1/threads/:threadId/advanceSend a message and get AI response

Quick Start

App-Key Auth (server / curl)

bash
curl -X POST https://www.unifiedlayer.space/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "x-client-id: YOUR_CLIENT_ID" \
  -H "x-app-id: YOUR_APP_ID" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "messages": [
      { "role": "user", "content": "What is your refund policy?" }
    ]
  }'

Session Auth (browser / SDK flow)

Step 1 — Create a session from your server:

bash
curl -X POST https://www.unifiedlayer.space/api/v1/auth/session \
  -H "Content-Type: application/json" \
  -H "x-client-id: YOUR_CLIENT_ID" \
  -H "x-app-id: YOUR_APP_ID" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{ "userId": "user_12345" }'

Response:

json
{
  "sessionToken": "ul_sess_a3f8c2d1e4b7a9f0c3d2e1b4a7f0c3d2e1b4a7f0c3d2e1b4",
  "userId": "user_12345",
  "appId": "YOUR_APP_ID",
  "expiresAt": "2026-03-15T10:30:00.000Z"
}

Step 2 — Use the session token in the browser:

bash
curl -X POST https://www.unifiedlayer.space/api/v1/threads/thread_abc123/advance \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ul_sess_a3f8c2d1e4b7a9f0c3d2e1b4a7f0c3d2e1b4a7f0c3d2e1b4" \
  -H "x-app-id: YOUR_APP_ID" \
  -d '{ "message": { "role": "user", "content": "What is your refund policy?" } }'

Next Steps

  • Session Auth — Full session creation, usage, and revocation reference
  • Chat Completions — OpenAI-compatible stateless chat
  • Threads — Thread lifecycle and all thread endpoints
  • Error Reference — All error codes with example payloads and debugging tips

Released under the ISC License.