{"id":17304,"library":"node-bing-api","title":"Node.js Bing Search API Client","description":"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.","status":"maintenance","version":"4.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/goferito/node-bing-api","tags":["javascript","bing","api","bing-api","search","cognitive"],"install":[{"cmd":"npm install node-bing-api","lang":"bash","label":"npm"},{"cmd":"yarn add node-bing-api","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-bing-api","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package primarily uses CommonJS `require()` syntax as of v4.1.1. The default export is a factory function that takes configuration and returns the Bing API client instance. ES Module `import` is not officially supported by the documentation.","wrong":"import Bing from 'node-bing-api';","symbol":"Bing","correct":"const BingFactory = require('node-bing-api');\nconst Bing = BingFactory({ accKey: process.env.BING_API_KEY ?? '' });"},{"note":"Since v2, the callback function is strictly the last parameter. Arguments like `options` must precede the callback.","wrong":"Bing.web(\"query\", callback, options);","symbol":"Bing.web","correct":"Bing.web(\"query\", options, callback);"},{"note":"To use Promise-based async/await with methods like `Bing.web`, `util.promisify` must be used. Crucially, `.bind(Bing)` is required to ensure the `this` context of the `Bing` instance is correctly preserved when the method is promisified.","wrong":"const searchBingWeb = util.promisify(Bing.web);","symbol":"promisify","correct":"const util = require('util');\nconst searchBingWeb = util.promisify(Bing.web.bind(Bing));"}],"quickstart":{"code":"const util = require('util');\nconst BingFactory = require('node-bing-api');\n\n// Ensure you set your BING_API_KEY as an environment variable\n// You can get one from Azure Cognitive Services\nconst BING_API_KEY = process.env.BING_API_KEY ?? '';\n\nif (!BING_API_KEY) {\n  console.error('Error: BING_API_KEY environment variable is not set.');\n  process.exit(1);\n}\n\nconst Bing = BingFactory({ accKey: BING_API_KEY });\n\n// Option 1: Using callbacks (default)\nBing.web(\"JavaScript library\", {\n    count: 5,\n    offset: 0\n  }, function(error, res, body){\n    if (error) {\n      console.error('Callback Error:', error);\n      return;\n    }\n    if (body && body.webPages && body.webPages.value && body.webPages.value.length > 0) {\n      console.log('--- Callback Results (first 2 web pages) ---');\n      console.log(body.webPages.value[0]?.name + ' - ' + body.webPages.value[0]?.url);\n      console.log(body.webPages.value[1]?.name + ' - ' + body.webPages.value[1]?.url);\n    } else {\n      console.log('No web page results found via callback.');\n    }\n  });\n\n// Option 2: Using Promises with util.promisify\nconst searchBingWebPromise = util.promisify(Bing.web.bind(Bing));\n\nasync function performPromiseSearch() {\n  try {\n    const [res, body] = await searchBingWebPromise(\"TypeScript documentation\", {\n      count: 3,\n      offset: 0\n    });\n\n    if (body && body.webPages && body.webPages.value && body.webPages.value.length > 0) {\n      console.log('\\n--- Promise Results (first web page) ---');\n      console.log(body.webPages.value[0]?.name + ' - ' + body.webPages.value[0]?.url);\n    } else {\n      console.log('No web page results found via Promise.');\n    }\n  } catch (error) {\n    console.error('\\nPromise Error:', error);\n  }\n}\n\nperformPromiseSearch();","lang":"javascript","description":"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."},"warnings":[{"fix":"Review the official Microsoft Bing Search API v7 documentation for any changes in request parameters or response structures. Update your API key if necessary, and carefully test existing integrations.","message":"Version 4 of `node-bing-api` migrated to the Microsoft Cognitive Services Bing Search API v7. New API registrations might only support this version, requiring existing users to potentially update their code and API keys.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure your Bing API key is provisioned for Cognitive Services and review the API documentation for any breaking changes between the legacy Bing Search API and Cognitive Services endpoints.","message":"Version 3 introduced support for the new Cognitive Services API. This was a significant backend change that could affect existing applications not configured for Cognitive Services.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Update all `Bing` method calls (e.g., `Bing.web`, `Bing.images`) to ensure the callback function is the absolute last argument. For example, `Bing.web(\"query\", { options }, callback);`.","message":"Version 2 changed the callback function signature, making it consistently the last parameter in all API method calls. Older code might have placed options or other parameters after the callback.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For Promise usage, always use `const promisedMethod = util.promisify(Bing.originalMethod.bind(Bing));` and `await promisedMethod(...);`. Do not directly call `.then()` on callback-based methods.","message":"The library is primarily callback-based. To use Promise-based `async/await` patterns, `util.promisify()` must be applied to each `Bing` method, specifically binding the method to the `Bing` instance (e.g., `util.promisify(Bing.web.bind(Bing))`) to preserve `this` context.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Obtain an API key from the Azure portal for Bing Search API. Ensure it's correctly passed during `node-bing-api` initialization via the `accKey` option.","message":"A valid Azure Cognitive Services Bing Search API key is mandatory for all operations. Attempts to use the library without a correct and active key will result in API call failures or empty responses.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consult the Bing API documentation for specific `count` and `offset` limits for each search vertical (Web, News, Images, etc.) and adjust your query parameters accordingly.","message":"Search methods have specific limits for `count` (number of results) and `offset` (pagination) that vary by search vertical. Exceeding these limits (e.g., `count: 50` for web search, `count: 15` for news search) may result in fewer results than requested or API errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Verify that your `accKey` is correct and active. Add robust error handling to check the `error` parameter in the callback and inspect `res.statusCode` before attempting to access properties on `body`.","cause":"This usually indicates that the API response `body` is malformed, empty, or does not contain the expected `webPages.value` property. Common causes include an invalid or expired API key, network issues, or an API error status code.","error":"TypeError: Cannot read properties of undefined (reading 'value')"},{"fix":"Ensure the initialization is `const Bing = require('node-bing-api')({ accKey: 'your-account-key' });`. Do not omit the `({ accKey: '...' })` part.","cause":"The `Bing` object was not correctly initialized. The `require('node-bing-api')` call returns a factory function that *must* be invoked with an options object (containing `accKey`) to return the client instance.","error":"TypeError: Bing.web is not a function"},{"fix":"You must use `util.promisify(Bing.web.bind(Bing))` to create a Promise-returning version of the method. Ensure you include `.bind(Bing)` to maintain the correct context.","cause":"This occurs when attempting to use `.then()` directly on a callback-based method (e.g., `Bing.web()`) without first transforming it into a Promise using `util.promisify`.","error":"Promise { <pending> } (or .then() never resolves)"},{"fix":"Double-check your `accKey` for typos. Ensure your Azure subscription is active and the Bing Search API resource is properly configured and enabled.","cause":"A 401 Unauthorized status code almost always indicates an issue with the API key, such as it being incorrect, expired, or lacking the necessary permissions for the requested operation.","error":"Request failed with status code 401"}],"ecosystem":"npm","meta_description":null}