How to Use the Unofficial Bing Chat API with Node.js

Feb 7, 2023 | Educational

Welcome to our guide on leveraging the unofficial Bing Chat API using Node.js! This API provides a fascinating way to interact with Microsoft’s Bing Chat, giving you an experience reminiscent of ChatGPT just turned up a notch. Are you ready to dive in? Let’s get started!

Intro

This Node.js package serves as a wrapper around the Bing Chat by Microsoft. However, be mindful that this is a reverse-engineered hack, and its longevity is uncertain. It’s not intended for production use, so approach with discretion. Follow the progress on Twitter at @transitive_bs.

Demo

To visualize the capabilities of this API, check out the demo below:

Example conversation

30 seconds of conversation demo

Installation

To get rolling, you’ll need to install the package. Here’s how:

bash
npm install bing-chat

Ensure you are using node version 18 so that the fetch API is available.

Usage

Before you can start chatting with Bing, you need either access to Bing Chat or a valid _U cookie from someone who does. Here’s how you can set it up:

ts
import BingChat from 'bing-chat';

async function example() {
    const api = new BingChat({
        cookie: process.env.BING_COOKIE
    });

    const res = await api.sendMessage('Hello World!');
    console.log(res.text);
}

This code initializes the Bing Chat API, sends a message, and prints out the response. It’s akin to going to a café, placing your order with the barista (i.e., sending a message), and then waiting while your drink gets prepared (awaiting the response). Once ready, you get to savor what you ordered (viewing the response text).

Advanced Features

Let’s explore some advanced capabilities.

Streaming Responses

You can also set up streaming for real-time responses from Bing:

ts
const res = await api.sendMessage('Write a 500 word essay on frogs.', {
    onProgress: (partialResponse) => console.log(partialResponse.text)
});

console.log(res.text);

Your conversation might not last long; Bing Chat sessions expire in about 20 minutes. If you’re interested in varying your conversation style, you can change the AI’s variant as shown below:

ts
const res = await api.sendMessage('Write a 500 word essay on frogs.', {
    variant: 'Creative' // Options are Balanced, Precise, or Creative
});

Projects

If you create an exciting integration using this API, feel free to make a pull request and share it with the community!

Compatibility

  • This package is ESM-only.
  • It supports Node version 18.
  • Fetch should be installed globally.
  • If you’re building a website with this chat, it’s recommended to use it from your backend API.

Troubleshooting

If you encounter issues, here are some nifty troubleshooting tips:

  • Ensure you’re using Node version 18, as earlier versions may not support fetch.
  • Double-check your _U cookie; an invalid or expired cookie will lead to authentication failures.
  • Take note of the session expiration—Bing Chat conversations don’t hold on for long!

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

That’s a wrap on using the unofficial Bing Chat API with Node.js! With this setup, you’re ready to get started on your projects and interactions with Bing Chat.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox