back to tutorials

How to build conversations exporting script with ChatBotKit Node SDK

Learn how to export conversations using the ChatBotKit Node SDK with this step-by-step tutorial. Improve your chatbot's performance by analyzing user interactions.

Here you will find a step-by-step tutorial on how to export conversations using the ChatBotKit Node SDK. This can be useful for analyzing user interactions and improving your chatbot's performance.

The tutorial includes instructions on setting up the project, creating a .env file, implementing the conversation export script, and running the script. If you're ready to get started, follow the steps below.

Step 1: Set up the project

  1. Create a new directory for your project and navigate to it using the command line.

  2. Initialize a new Node.js project by running the following command:

    npm init -y
  3. Install the required dependencies by running the following command:

    npm install dotenv @chatbotkit/[sdk](/docs/node-sdk)

Step 2: Create a .env file

  1. Create a new file in the project directory and name it .env.

  2. Open the .env file and add the following line:


    Replace your_api_secret with the actual API secret provided by ChatBotKit.

Step 3: Implement the conversation export script

  1. Create a new JavaScript file in the project directory, e.g., export-conversations.js.

  2. Copy the provided code into the export-conversations.js file.

  3. Import the required modules at the top of the file:

    import * as dotenv from 'dotenv' import fs from 'fs/promises' import { ConversationClient } from '@chatbotkit/sdk/conversation/index.js'
  4. Load the environment variables from the .env file by adding the following line at the beginning of the script:

  5. Define an asynchronous function named main() that will handle the conversation export:

    async function main() { // Code for conversation export goes here }
  6. Instantiate a new ConversationClient with the API secret obtained from the environment variables:

    const client = new ConversationClient({ secret: process.env.CHATBOTKIT_API_SECRET, })
  7. Create an empty array to store the conversations:

    const conversations = []
  8. Use a for-await-of loop to iterate over the conversations retrieved from the ChatbotKit Conversation API using the list().stream() method:

    for await (const { data: conversationData } of client.list().stream()) { // Code to process each conversation goes here }
  9. Inside the loop, create an empty object to store the conversation details and messages:

    const conversation = { id:, createdAt: conversationData.createdAt, messages: [], }
  10. Use another for-await-of loop to iterate over the messages of the current conversation using the message.list( method:

    for await (const { data: messageData } of client.message.list( { // Code to process each message goes here }
  11. Inside the inner loop, push each message's details to the conversation.messages array:

    conversation.messages.push({ id:, type: messageData.type, text: messageData.text, createdAt: messageData.createdAt, })
  12. After the inner loop ends, push the processed conversation object to the conversations array:

  13. Convert the conversations array to JSON format with proper indentation:

    const jsonContent = JSON.stringify(conversations, null, 2)
  14. Write the JSON content to a file named "conversations.json" using the fs.writeFile() method:

    await fs.writeFile('conversations.json', jsonContent)
  15. Call the main() function to start the conversation export process:


Step 4: Run the script

  1. Open a command prompt and navigate to the project directory.

  2. Run the script using the following command:

    node export-conversations.js
  3. The script will start exporting conversations one by one, displaying the progress in the console.

  4. Once the script completes, you will find a file named "conversations.json" in the project directory containing the exported conversations with their respective messages.

That's it! You have successfully implemented a script to export conversations using the ChatBotKit Node SDK. Feel free to modify the code or integrate it into your own applications as needed. A full copy of the code can be found here.