Quickstart: Send a search request to Bing Entity Search REST API using Node.js
Use this quickstart to make your first call to Bing Entity Search API and view the JSON response. This simple JavaScript application sends a news search query to the API, and displays the response. The source code for this sample is available on GitHub.
Although this application is written in JavaScript, the API is a RESTful Web service compatible with most programming languages.
Prerequisites
The latest version of Node.js.
Create and initialize the application
Create a new JavaScript file in your favorite IDE or editor, and set the strictness and HTTPS requirements.
'use strict'; let https = require ('https');
Create variables for the API endpoint, your subscription key, and search query.
let subscriptionKey = 'ENTER YOUR KEY HERE'; let host = 'api.bing.microsoft.com'; let path = '/v7.0/entities'; let mkt = 'en-US'; let q = 'italian restaurant near me';
Append your market and query parameters to a string called
query
. Be sure to url-encode your query withencodeURI()
.let query = '?mkt=' + mkt + '&q=' + encodeURI(q);
Handle and parse the response
Define a function named
response_handler()
that takes an HTTP call,response
, as a parameter.Within this function, define a variable to contain the body of the JSON response.
let response_handler = function (response) { let body = ''; };
Store the body of the response when the
data
flag is called.response.on('data', function (d) { body += d; });
When an
end
flag is signaled, parse the JSON, and print it.response.on ('end', function () { let json = JSON.stringify(JSON.parse(body), null, ' '); console.log (json); }); ```
Send a request
Create a function called
Search()
to send a search request.Within this function, create a JSON object containing your request parameters. Use
Get
for the method, and add your host and path information. Add your subscription key to theOcp-Apim-Subscription-Key
header.Use
https.request()
to send the request with the response handler created previously, and your search parameters.let Search = function () { let request_params = { method : 'GET', hostname : host, path : path + query, headers : { 'Ocp-Apim-Subscription-Key' : subscriptionKey, } }; let req = https.request (request_params, response_handler); req.end (); }
Call the
Search()
function.
Example JSON response
A successful response is returned in JSON, as shown in the following example:
{
"_type": "SearchResponse",
"queryContext": {
"originalQuery": "italian restaurant near me",
"askUserForLocation": true
},
"places": {
"value": [
{
"_type": "LocalBusiness",
"webSearchUrl": "https://www.bing.com/search?q=sinful+bakery&filters=local...",
"name": "Liberty's Delightful Sinful Bakery & Cafe",
"url": "https://www.contoso.com/",
"entityPresentationInfo": {
"entityScenario": "ListItem",
"entityTypeHints": [
"Place",
"LocalBusiness"
]
},
"address": {
"addressLocality": "Seattle",
"addressRegion": "WA",
"postalCode": "98112",
"addressCountry": "US",
"neighborhood": "Madison Park"
},
"telephone": "(800) 555-1212"
},
. . .
{
"_type": "Restaurant",
"webSearchUrl": "https://www.bing.com/search?q=Pickles+and+Preserves...",
"name": "Munson's Pickles and Preserves Farm",
"url": "https://www.princi.com/",
"entityPresentationInfo": {
"entityScenario": "ListItem",
"entityTypeHints": [
"Place",
"LocalBusiness",
"Restaurant"
]
},
"address": {
"addressLocality": "Seattle",
"addressRegion": "WA",
"postalCode": "98101",
"addressCountry": "US",
"neighborhood": "Capitol Hill"
},
"telephone": "(800) 555-1212"
},
. . .
]
}
}