API reference
Use the API to put the AI in your own app, chat system or backend. It is the same endpoint the widget and the Discord bot use. Public endpoints accept requests from any origin (CORS).
Authentication
Every request carries your public key, pk_…, from Dashboard → Channels. It can only ask questions, so it's safe in a browser. Base URL: https://nightshift.vn.
POST /v1/chat
| Field | Type | What it does |
|---|---|---|
key | string | Required. Your public key, pk_…. |
message | string | Required. The customer's question, 1 to 2,000 characters. |
history | array | Optional. Earlier turns as {"role": "user" | "assistant", "content": "…"}, up to 20. The last 6 are used. |
stream | boolean | Optional, default true. Stream the answer as NDJSON, or return one JSON object. |
visitor | string | Optional. Your own id for the person chatting. |
channel | string | Optional. api (default), widget or discord, used in analytics. |
Simple request
With "stream": false you get the full answer in one response:
curl -X POST https://nightshift.vn/v1/chat \
-H "Content-Type: application/json" \
-d '{"key": "pk_...", "message": "Do you ship to Da Nang?", "stream": false}'const res = await fetch("https://nightshift.vn/v1/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ key: "pk_...", message: "Do you ship to Da Nang?", stream: false }),
});
if (res.status === 429 || res.status === 503) {
const wait = Number(res.headers.get("Retry-After") || 10);
console.log(`Busy, try again in ${wait} s`);
} else {
const { answer, sources, outcome } = await res.json();
console.log(answer, outcome, sources);
}import requests
r = requests.post("https://nightshift.vn/v1/chat", json={
"key": "pk_...",
"message": "Do you ship to Da Nang?",
"stream": False,
})
if r.status_code in (429, 503):
print("Busy, try again in", r.headers.get("Retry-After", "10"), "s")
else:
r.raise_for_status()
print(r.json()["answer"]){
"answer": "Yes, we ship to Da Nang. Orders arrive in 2–4 days.",
"sources": ["https://your-shop.com/shipping"],
"outcome": "answered"
}Streaming (NDJSON)
By default the response is application/x-ndjson: one JSON object per line, so you can show the answer while it is being written.
| Event | What it does |
|---|---|
meta | Sent first. sources: the page URLs used; thinking: whether the model reasons first. |
token | A piece of the answer in text. Append them in order. |
done | Sent last. seconds, outcome, model and tokens. |
busy | Too many messages or the system is busy. text is a message you can show; retry_after is in seconds. |
error | Something went wrong. text holds a message you can show. |
handoff | status is needs_human (the AI handed over) or human (a teammate is handling this chat, so the AI does not answer). |
{"type": "meta", "sources": ["https://your-shop.com/shipping"], "taught": [], "thinking": false}
{"type": "token", "text": "Yes, we ship "}
{"type": "token", "text": "to Da Nang."}
{"type": "done", "outcome": "answered", "model": "Pinky", "tokens": 812}const res = await fetch("https://nightshift.vn/v1/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ key: "pk_...", message: "Hi!", history: [] }),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "", answer = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop(); // keep a half-received line
for (const line of lines) {
if (!line) continue;
const ev = JSON.parse(line);
if (ev.type === "token") answer += ev.text;
if (ev.type === "busy") console.log(ev.text, ev.retry_after);
if (ev.type === "done") console.log(answer, ev.outcome);
}
}import json, requests
with requests.post("https://nightshift.vn/v1/chat",
json={"key": "pk_...", "message": "Hi!"}, stream=True) as r:
for line in r.iter_lines():
if not line:
continue
ev = json.loads(line)
if ev["type"] == "token":
print(ev["text"], end="", flush=True)
elif ev["type"] == "busy":
print(ev["text"], "retry in", ev["retry_after"], "s")Outcomes
outcome | Meaning |
|---|---|
answered | Answered from your pages or taught answers. |
unsure | Not enough information. The AI said so instead of guessing. These show up in analytics so you can teach them. |
off_topic | The question has nothing to do with your business. |
busy | Too many messages or the system is busy. Try again after retry_after seconds. |
error | The answer could not be completed. |
handoff | The AI handed the chat to your team; it's waiting in the Inbox. |
human | A teammate has taken over this conversation. The AI stays silent; show their messages from GET /v1/messages. |
paused | This channel is paused in your dashboard, so the AI doesn't answer. |
Errors
Errors return a JSON body with a type your code can check and a message you can show. Busy responses include a Retry-After header.
| HTTP status | Type | When |
|---|---|---|
400 | — | The message is empty. |
401 | — | The key is wrong or the website was removed. |
422 | — | A field is missing or too long; detail says which. |
429 | rate_limited | Too many messages from one visitor or website. Retry-After: 10. |
503 | overloaded | The system is busy. Wait for Retry-After seconds. |
{
"error": {
"type": "rate_limited",
"message": "You're sending messages a bit fast. Please wait a moment and try again 🙏",
"retry_after": 10
}
}Rate limits
15 messages per minute per visitor (by real IP address for traffic from the internet) and 240 per minute per website. Over the limit: HTTP 429 with Retry-After, or a busy event when streaming.
Talking to a person
Send POST /v1/handoff with {"key", "visitor"} when the customer asks for a person; the chat goes to the top of the Inbox. Poll GET /v1/messages?key=…&visitor=…&after=ID every few seconds to receive teammate messages and the current status. Use the same visitor id in /v1/chat.
curl -X POST https://nightshift.vn/v1/handoff \
-H "Content-Type: application/json" \
-d '{"key": "pk_...", "visitor": "v_8f2k1c9a"}'
curl "https://nightshift.vn/v1/messages?key=pk_...&visitor=v_8f2k1c9a&after=0"GET /v1/site
What a chat window needs to render: the AI name, your site host, whether to show “Powered by Nightshift” (branding), up to 3 suggested questions from taught answers and up to 2 contact links.
curl "https://nightshift.vn/v1/site?key=pk_..."{
"bot_name": "Acme Support",
"host": "your-shop.com",
"branding": false,
"suggestions": ["How long does shipping take?"],
"human": [{"kind": "email", "url": "mailto:[email protected]"}]
}