Quickstart: Send a query to the Bing Local Business Search API using Node.js
Warning
On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.
Use this quickstart to learn how to send requests to the Bing Local Business Search API, which is an Azure Cognitive Service. Although this simple application is written in Node.js, the API is a RESTful Web service compatible with any programming language capable of making HTTP requests and parsing JSON.
This example application gets local response data from the API for a search query.
Prerequisites
- An Azure subscription - Create one for free
- The latest version of Node.js.
- The JavaScript Request Library.
- Once you have your Azure subscription, create a Bing Search resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
Code scenario
The following code defines and sends the request, which is implemented in the following steps:
- Declare variables to specify the endpoint by host and path.
- Specify the query, and add the query parameter.
- Create a handler function for the response.
- Define the Search function that creates the request and adds the
Ocp-Apim-Subscription-Key
header. - Run the search function.
'use strict';
let https = require('https');
// Replace the subscriptionKey string value with your valid subscription key.
let subscriptionKey = 'your-access-key';
let host = 'api.cognitive.microsoft.com/bing';
let path = '/v7.0/localbusinesses/search';
let mkt = 'en-US';
let q = 'hotel in Bellevue';
let params = '?q=' + encodeURI(q) + "&mkt=" + mkt;
let response_handler = function (response) {
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
let body_ = JSON.parse(body);
let body__ = JSON.stringify(body_, null, ' ');
console.log(body__);
});
response.on('error', function (e) {
console.log('Error: ' + e.message);
});
};
let Search = function () {
let request_params = {
method: 'GET',
hostname: host,
path: path + params,
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
}
};
let req = https.request(request_params, response_handler);
req.end();
}
Search();