Headless API
Getting started
@nlxai/chat-core is the lowest-level package used to handle bot communication in a completely headless, platform- and UI-agnostic way.
This package can be used to:
- communicate with a bot on a website without rendering a widget.
- communicate with a bot on a Node.js server, passing along responses to a third-party chat system.
Sample code
After installing the package, you can start a basic headless conversation using the following code snippet:
1import { createConversation } from "@nlxai/chat-core"; 2 3// Create some configuration 4const config = { 5 botUrl: "", // obtain from NLX deployments page 6 headers: { 7 "nlx-api-key": "", // obtain from NLX deployments page 8 }, 9 conversationId: "", // start with a specific conversation ID - useful if you want to resume a previous conversation 10 languageCode: "es-US", // optional language code for standard bots that do not run on US English 11 environment: "production", // optional environment name for multi-environment bots to control which data request environment should be used. "production" or "development" are the only supported values. 12}; 13 14// Start the conversation 15const convo = createConversation(config); 16 17// Subscribe to changes in the list of responses; the newest response is sent as a second argument 18convo.subscribe((responses, newResponse) => { 19 console.log(responses); 20}); 21 22// Send a message from the user's end 23convo.sendText("hello");