REST API Integration
In addition to embedding the chatbot into your application using scripts, you can interact with your deployed chatbot using the REST API. This allows for more advanced integrations, including custom logic and streamed output.
Example API Request
The following example demonstrates how to interact with the REST API by sending a request and streaming the chatbot's response.
API Request
var VAKFlowID = "Your_VAKFlowID"; // Replace with your VAKFlow ID
var sessionID = "Random-Session-ID"; // Unique session identifier
var message = "User-message"; // The message you want to send to the AI assistant
fetch(
`https://api.vakx.io/v1/chat/stream?VAKFlowID=${VAKFlowID}&sessionID=${sessionID}&message=${encodeURIComponent(message)}`,
{ method: 'GET' }
).then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulatedContent = ''; // Accumulate content
return reader.read().then(function processText({ done, value }) {
if (done) return;
const decodedText = decoder.decode(value, { stream: true });
const lines = decodedText.trim().split("\n");
for (let line of lines) {
if (line.startsWith("data: ")) {
const jsonString = line.slice(6); // Remove the "data: " prefix
try {
const jsonData = JSON.parse(jsonString);
if (jsonData.content) {
const decodedContent = decodeURIComponent(escape(atob(jsonData.content)));
accumulatedContent += decodedContent;
console.log("Accumulated Content: ", accumulatedContent);
}
if (jsonData.is_stream_finished) {
reader.cancel(); // Close the stream
break;
}
} catch (error) {
console.error("Failed to parse JSON:", error);
}
}
}
return reader.read().then(processText);
});
});
Configuring Authorized Domains for Integration
To ensure these integration methods work outside of VAKStudio, you must first configure the chatbot by adding authorized domains. Without this configuration, the APIs will not function properly.