Node.js Bing Search API Client

4.1.1 · maintenance · verified Wed Apr 22

The `node-bing-api` library is a Node.js client for integrating with the Microsoft Cognitive Services Bing Web Search API. It provides synchronous and asynchronous access to various Bing search verticals, including Web, Composite, News, Video, Images, Related Search, and Spelling Suggestions. The current stable version is 4.1.1, with its last publication being over five years ago, indicating a maintenance release cadence rather than active feature development. The library is callback-centric by default, requiring the use of `util.promisify` for applications that prefer Promise-based asynchronous operations. A key differentiator is its direct mapping to the Bing API responses, providing raw body data, and its explicit support for features like market specification and adult content filtering. Users must provide a valid Azure Cognitive Services Bing Search API key for functionality.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `node-bing-api` client, perform a web search using both the default callback-based approach, and how to adapt it for Promise-based usage with `util.promisify` for modern asynchronous patterns. It emphasizes the need for an API key.

const util = require('util');
const BingFactory = require('node-bing-api');

// Ensure you set your BING_API_KEY as an environment variable
// You can get one from Azure Cognitive Services
const BING_API_KEY = process.env.BING_API_KEY ?? '';

if (!BING_API_KEY) {
  console.error('Error: BING_API_KEY environment variable is not set.');
  process.exit(1);
}

const Bing = BingFactory({ accKey: BING_API_KEY });

// Option 1: Using callbacks (default)
Bing.web("JavaScript library", {
    count: 5,
    offset: 0
  }, function(error, res, body){
    if (error) {
      console.error('Callback Error:', error);
      return;
    }
    if (body && body.webPages && body.webPages.value && body.webPages.value.length > 0) {
      console.log('--- Callback Results (first 2 web pages) ---');
      console.log(body.webPages.value[0]?.name + ' - ' + body.webPages.value[0]?.url);
      console.log(body.webPages.value[1]?.name + ' - ' + body.webPages.value[1]?.url);
    } else {
      console.log('No web page results found via callback.');
    }
  });

// Option 2: Using Promises with util.promisify
const searchBingWebPromise = util.promisify(Bing.web.bind(Bing));

async function performPromiseSearch() {
  try {
    const [res, body] = await searchBingWebPromise("TypeScript documentation", {
      count: 3,
      offset: 0
    });

    if (body && body.webPages && body.webPages.value && body.webPages.value.length > 0) {
      console.log('\n--- Promise Results (first web page) ---');
      console.log(body.webPages.value[0]?.name + ' - ' + body.webPages.value[0]?.url);
    } else {
      console.log('No web page results found via Promise.');
    }
  } catch (error) {
    console.error('\nPromise Error:', error);
  }
}

performPromiseSearch();

view raw JSON →