AI Answers API reference
This page describes how to send a question to the AI Answers API and how to read the answer that comes back.
Before you start, turn the API on and copy the API address, tenant ID and project ID from the Access tab. If the API requires a key, you also need one of the project's API keys. See Set up API access for AI Answers.
Send a question
Send a POST request to the project's API address, with a JSON body. Use these headers:
Header | Value |
|---|---|
| |
|
|
The body has these fields. All of them are required, except history:
Field | Value |
|---|---|
| The tenant ID from the Access tab. |
| The project ID from the Access tab. |
| The language code of the content to answer from. For example, |
| The question, as plain text. |
| Earlier questions and answers in the conversation, oldest first. Each item has a |
The API doesn't store conversations. To ask a follow-up question, send the earlier questions and answers in history.
For example:
curl -N -X POST "<API address>" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <API key>" \
-d '{
"tenantId": "<tenant ID>",
"projectId": "<project ID>",
"language": "en",
"message": "Can I publish it to more than one channel?",
"history": [
{"role": "user", "content": "How do I publish a topic?"},
{"role": "assistant", "content": "To publish a topic, add it to a publication and select Publish."}
]
}'Read the answer
The answer comes back as a stream of server-sent events, so your application can show the text while it is still being written. Each event is a line that starts with data:, followed by a JSON object. Its type field says what kind of event it is:
Type | What it contains |
|---|---|
| The first event. |
| A piece of the answer in |
| The topics the answer is based on, in |
| Something went wrong while the answer was being written. Treat the answer as failed. |
| The last event. It can carry a |
When the answer is complete, the done event has no reason. When it has one, the reason tells you why the answer ended:
Reason | Meaning |
|---|---|
| Nothing in the published content answers the question. |
| The question was declined. Don't show any text that arrived before this event. |
| Answers can't be given right now. Try again later. |
For example:
data: {"type": "answer", "answerId": "<answer ID>"}
data: {"type": "text", "content": "To publish a topic, add it to "}
data: {"type": "text", "content": "a publication and select Publish."}
data: {"type": "sources", "sources": [{"url": "en/publish/publish-a-publication", "title": "Publish a publication"}]}
data: {"type": "done"}An answer usually takes 5 to 20 seconds to finish streaming.
Errors
Errors are returned as an HTTP status code before any events are sent:
Status | Meaning |
|---|---|
| The API key is missing or not accepted. |
| Forbidden. |
| The API is turned off for this project, or nothing has been published from it yet. |