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

FieldTypeWhat it does
keystringRequired. Your public key, pk_….
messagestringRequired. The customer's question, 1 to 2,000 characters.
historyarrayOptional. Earlier turns as {"role": "user" | "assistant", "content": "…"}, up to 20. The last 6 are used.
streambooleanOptional, default true. Stream the answer as NDJSON, or return one JSON object.
visitorstringOptional. Your own id for the person chatting.
channelstringOptional. 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}'
{
  "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.

EventWhat it does
metaSent first. sources: the page URLs used; thinking: whether the model reasons first.
tokenA piece of the answer in text. Append them in order.
doneSent last. seconds, outcome, model and tokens.
busyToo many messages or the system is busy. text is a message you can show; retry_after is in seconds.
errorSomething went wrong. text holds a message you can show.
handoffstatus 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);
  }
}

Outcomes

outcomeMeaning
answeredAnswered from your pages or taught answers.
unsureNot enough information. The AI said so instead of guessing. These show up in analytics so you can teach them.
off_topicThe question has nothing to do with your business.
busyToo many messages or the system is busy. Try again after retry_after seconds.
errorThe answer could not be completed.
handoffThe AI handed the chat to your team; it's waiting in the Inbox.
humanA teammate has taken over this conversation. The AI stays silent; show their messages from GET /v1/messages.
pausedThis 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 statusTypeWhen
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.
429rate_limitedToo many messages from one visitor or website. Retry-After: 10.
503overloadedThe 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_..."