Developers

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 userId: "abcd-1234", // optional property to identify the user 10 conversationId: "", // start with a specific conversation ID - useful if you want to resume a previous conversation 11 languageCode: "es-US", // optional language code for standard bots that do not run on US English 12 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. 13}; 14 15// Start the conversation 16const convo = createConversation(config); 17 18// Subscribe to changes in the list of responses; the newest response is sent as a second argument 19convo.subscribe((responses, newResponse) => { 20 console.log(responses); 21}); 22 23// Send a message from the user's end 24convo.sendText("hello");