Skip to content

Quickstart

This guide takes you from zero to a streaming AI response in four steps. By the end, you will have authenticated, created an AI session, sent a message, and received a streamed reply.

All examples are available in cURL, JavaScript, Python, and Postman. Replace placeholder values like YOUR_API_KEY with real values from previous steps.


Environment Variables

Set these before running any example on this page:

bash
export BASE_URL="https://api.chainabit.com/api/v1"
export API_KEY="chb_sk_your_key_here"

Don't have an API key yet? API keys require a Chainer subscription. See Developer Access for how to generate one from Hub settings.

If you prefer session-based JWT auth, see Authentication.


Step 1: Create an AI Session

A session is a persistent conversation thread. Create one with a title to identify it.

bash
curl -X POST "$BASE_URL/ai/sessions" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"title": "My First Session"}'
javascript
const response = await fetch(`${BASE_URL}/ai/sessions`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ title: 'My First Session' }),
});
const { data } = await response.json();
const SESSION_ID = data.id;
python
import requests

response = requests.post(
    f"{BASE_URL}/ai/sessions",
    json={"title": "My First Session"},
    headers={"Authorization": f"Bearer {API_KEY}"},
)
data = response.json()["data"]
SESSION_ID = data["id"]

Response 201 Created

json
{
  "data": {
    "id": "sess_01HQ3K5N2P4R7T9V1X3Z5A7C9E",
    "title": "My First Session",
    "status": "active",
    "createdAt": "2026-05-04T10:00:00.000Z"
  },
  "meta": null,
  "error": null
}

Copy the id — you will use it in the next step.

bash
export SESSION_ID="sess_01HQ3K5N2P4R7T9V1X3Z5A7C9E"

Step 2: Send a Message

Send a message into the session. The API accepts plain text content.

bash
curl -X POST "$BASE_URL/ai/sessions/$SESSION_ID/messages" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"content": "What can I build with the Chainabit API?"}'
javascript
const response = await fetch(
  `${BASE_URL}/ai/sessions/${SESSION_ID}/messages`,
  {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ content: 'What can I build with the Chainabit API?' }),
  }
);
const { data } = await response.json();
const MESSAGE_ID = data.id;
python
import requests

response = requests.post(
    f"{BASE_URL}/ai/sessions/{SESSION_ID}/messages",
    json={"content": "What can I build with the Chainabit API?"},
    headers={"Authorization": f"Bearer {API_KEY}"},
)
data = response.json()["data"]
MESSAGE_ID = data["id"]

Response 201 Created

json
{
  "data": {
    "id": "msg_01HQ3K7R2S5T8V2Y4A6C8E1G3I",
    "sessionId": "sess_01HQ3K5N2P4R7T9V1X3Z5A7C9E",
    "role": "user",
    "content": "What can I build with the Chainabit API?",
    "status": "pending",
    "createdAt": "2026-05-04T10:00:05.000Z"
  },
  "meta": null,
  "error": null
}
bash
export MESSAGE_ID="msg_01HQ3K7R2S5T8V2Y4A6C8E1G3I"

Step 3: Stream the Response

Open a server-sent event (SSE) stream to receive the AI reply in real time.

bash
curl "$BASE_URL/ai/sessions/$SESSION_ID/messages/$MESSAGE_ID/stream" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Accept: text/event-stream"
javascript
const eventSource = new EventSource(
  `${BASE_URL}/ai/sessions/${SESSION_ID}/messages/${MESSAGE_ID}/stream`,
  { headers: { Authorization: `Bearer ${API_KEY}` } }
);

eventSource.addEventListener('message.delta', (e) => {
  const { delta } = JSON.parse(e.data);
  process.stdout.write(delta);
});

eventSource.addEventListener('message.completed', () => {
  eventSource.close();
});
python
import requests

with requests.get(
    f"{BASE_URL}/ai/sessions/{SESSION_ID}/messages/{MESSAGE_ID}/stream",
    headers={"Authorization": f"Bearer {API_KEY}", "Accept": "text/event-stream"},
    stream=True,
) as r:
    for line in r.iter_lines():
        if line:
            print(line.decode())

SSE Output

event: message.delta
data: {"delta": "You can build"}

event: message.delta
data: {"delta": " AI-agent workflows,"}

event: message.delta
data: {"delta": " integrate connectors, and automate pipelines."}

event: message.completed
data: {"messageId": "msg_01HQ3K7R2S5T8V2Y4A6C8E1G3I", "finishReason": "stop"}

Collect message.delta events to assemble the full response. Close the stream on message.completed.


What's Next

Built with purpose.