HOW TO use Bing Search v5 API with JavaScript

Taking off from the single basic JavaScript sample on the newly minted version 5 of Bing Search API, I put together a couple of samples of my own. You'll have to sign-up for a free key from Microsoft Cognitive Services to replace in the code samples to try them & also experiment with all the other APIs that are available

<!DOCTYPE html>
<html>
<head>
<title>Bing Search v5 - Simple, show fewer results</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
// more info: http://mvark.blogspot.com/2016/06/how-to-use-bing-search-v5-api-with.html
var total;
var results = '';
// In this sample, I'm trying to find the pages of my blog that Bing has indexed with the "site:" operator with the "q" parameter
//For more info on the other querystring parameters refer to the documentation - https://bingapis.portal.azure-api.net/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d
$(function() {
var params = {
// Request parameters
"q": "site:mvark.blogspot.com",
"count": "50",
"offset": "0",
"mkt": "en-us",
//"safesearch": "Moderate",
"freshness":"Week",
};
$.ajax({
url: "https://bingapis.azure-api.net/api/v5/search/?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","abc123"); //replace value with your own key
},
type: "GET",
// Request body
//data: "{body}",
})
.done(function(data) {
len = data.webPages.value.length
for (i=0; i<len; i++ )
{
results += "<p><a href='" + data.webPages.value[i].url + "'>" + data.webPages.value[i].name + "</a>: " + data.webPages.value[i].snippet + "</p>";
}
$('#content').html(results);
})
.fail(function() {
alert("error");
});
});
</script>
Results:
<div id="content">Fetching...</div>
</body>
</html>


<!DOCTYPE html>
<html>
<head>
<title>Bing Search v5 - show all results</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script>
// more info: http://mvark.blogspot.com/2016/06/how-to-use-bing-search-v5-api-with.html
var total;
var ofst = 0;
var pgcnt=50;
var results = '';
// Specify the keyword you want to search as the value for the querystring key "q".
// In this sample, I'm trying to find the pages of my blog that Bing has indexed with the "site:" operator
//For more info on the other parameters refer to the documentation - https://bingapis.portal.azure-api.net/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d
var burl = "https://bingapis.azure-api.net/api/v5/search/?q=site:mvark.blogspot.com&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month"; //https matters for header auth
$(function() {
function xhr_get(url) {
return $.ajax({
url: url,
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","abc123"); //replace value with your own key
},
type: "GET",
})
.done(function(data) {
total = data.webPages.totalEstimatedMatches; //count of total estimated matches may change drastically. why?
len = data.webPages.value.length
for (i=0; i<len; i++ )
{
results += "<p><a href='" + data.webPages.value[i].url + "'>" + data.webPages.value[i].name + "</a>: " + data.webPages.value[i].snippet + "</p>";
}
$('#content').html(results);
if (total > ofst) {
ofst += pgcnt;
burl = "https://bingapis.azure-api.net/api/v5/search/?q=site:mvark.blogspot.com&count=" + pgcnt + "&offset=" + ofst + "&mkt=en-us&freshness=Month";
xhr_get(burl);
}
else { console.log('No more results to show');
}
})
}
xhr_get(burl);
});
</script>
Results: <div id="content">Fetching...</div>
</body>
</html>
Bing Search API gets not just web search results but also image, news, & videos results for a given query and allows up to 1,000 free transactions per month across all Bing Search APIs

Google Custom Search API has a few additional steps & provides 100 search queries per day for free

This comparison of version 2 and 5 highlights the improvements -
Category Feature v2 v5
Web Search REST API x x
Related searches x x
Ranked Answers
(image, video, news, math, time)
x
Deep links (for first web page result) x
Adult intent signal x
Image Search REST API x x
Enhanced filters: image size, license, style x
Visually similar images and products x
Merchant and recipe sources
(for product and food images)
x
Trending images x
Entity recognition (for people) x
Computer-generated captions x
Video Search REST API x x
Video previews (motion thumbnails) x
Enhanced video metadata
(creator, resolution, view count)
x
Free / Paid video filter x
Trending videos x
News search REST API x x
News articles by category or market x x
Trending news (general or by category) x
Enhanced article metadata, including featured entities
Spell Check REST API x
Autosuggest REST API x
General Location and market awareness x x
Spelling suggestions x x
Safe search x x
Related searches x x
Continuous relevance improvements x
Ongoing support and feature improvements x

Related: Google vs. Microsoft Bing Search APIs: A Detailed Comparison

Comments

Popular posts from this blog

The Mercurial Grok AI Assistant Understands & Speaks Indian Languages

Things Near Me – Find & Learn About Landmarks Nearby

This Week I Learned - Week #12 2025