Quickstart: Use a Bing Web Search JavaScript client library

The Bing Web Search client library makes it easy to integrate Bing Web Search into your Node.js application. In this quickstart, you'll learn how to instantiate a client, send a request, and print the response.

Want to see the code right now? Samples for the Bing Search client libraries for JavaScript are available on GitHub.

Prerequisites

Here are a few things that you'll need before running this quickstart:

Set up your development environment

Let's start by setting up the development environment for our Node.js project.

  1. Create a new directory for your project:

    mkdir YOUR_PROJECT
    
  2. Create a new package file:

    cd YOUR_PROJECT
    npm init
    
  3. Now, let's install some Azure modules and add them to the package.json:

    npm install --save azure-cognitiveservices-websearch
    npm install --save ms-rest-azure
    

Create a project and declare required modules

In the same directory as your package.json, create a new Node.js project using your favorite IDE or editor. For example: sample.js.

Next, copy this code into your project. It loads the modules installed in the previous section.

const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
const WebSearchAPIClient = require('azure-cognitiveservices-websearch');

Instantiate the client

This code instantiates a client and using the azure-cognitiveservices-websearch module. Make sure that you enter a valid subscription key for your Azure account before continuing.

let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY');
let webSearchApiClient = new WebSearchAPIClient(credentials);

Make a request and print the results

Use the client to send a search query to Bing Web Search. If the response includes results for any of the items in the properties array, the result.value is printed to the console.

webSearchApiClient.web.search('seahawks').then((result) => {
    let properties = ["images", "webPages", "news", "videos"];
    for (let i = 0; i < properties.length; i++) {
        if (result[properties[i]]) {
            console.log(result[properties[i]].value);
        } else {
            console.log(`No ${properties[i]} data`);
        }
    }
}).catch((err) => {
    throw err;
})

Run the program

The final step is to run your program!

Clean up resources

When you're done with this project, make sure to remove your subscription key from the program's code.

Next steps