Quickstart: Perform a news search with the Bing News Search SDK for Node.js

Use this quickstart to begin searching for news with the Bing News Search SDK for Node.js. While Bing News Search has a REST API compatible with most programming languages, the SDK provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub.

Prerequisites

To set up a console application using the Bing News Search SDK:

  1. Run npm install ms-rest-azure in your development environment.
  2. Run npm install azure-cognitiveservices-newssearch in your development environment.

You must have a Cognitive Services API account with access to the Bing Search APIs. If you don't have an Azure subscription, you can create an account for free. Before continuing, You will need the access key provided after activating your free trial, or a paid subscription key from your Azure dashboard.

Create and initialize the application

  1. Create an instance of the CognitiveServicesCredentials. Create variables for your subscription key, and a search term.

    const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
    let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY');
    let search_term = 'Winter Olympics'
    
  2. instantiate the client:

    const NewsSearchAPIClient = require('azure-cognitiveservices-newssearch');
    let client = new NewsSearchAPIClient(credentials);
    

Send a search query

  1. Use the client to search with a query term, in this case "Winter Olympics":

    client.newsOperations.search(search_term).then((result) => {
        console.log(result.value);
    }).catch((err) => {
        throw err;
    });
    

The code prints result.value items to the console without parsing any text. The results, if any per category, will include:

  • _type: 'NewsArticle'
  • _type: 'WebPage'
  • _type: 'VideoObject'
  • _type: 'ImageObject'

Next steps