Using bingWebSearch API With NodeJS
The Bing Web Search API offers several advantages for developers and businesses looking to incorporate search functionality into their applications or services. Here are some of the key benefits:
- Robust and Comprehensive Search Results
- Customization and Flexibility
- Easy Integration (via. RESTful API) and Scalability
- Advanced Features (like image, video, and news search)
- Microsoft Support and Reliability (Developer Resources)
- The help of ChatGPT!
Unlike Google and other search engines that are great at indexing sites and searching for keywords, Bing can be used to run “smart” searches by prompting it not only with keywords but also instructions about the type of results we’re looking for, filtering options, etc. Think of it as “prompt engineering specifically for a search”.
It took me a while to figure out how simple it is because Azure documentation is confusing and the menus are also not so straightforward, so I hope that the following will save you some pain!
This article will walk you through the steps you need to take, to use Bing programmatically.
First, you need to open an account in Azure, the next steps will show us how to enable Bing Web Search API in your Azure account.
We’ll start by creating a “bing resource”:
1. go to: https://portal.azure.com/#home
2. click on “Create a resource” (see screenshot above)
3. search for “Bing Search v7”: by the time you read this answer there may be newer versions of the search API! so make sure you’re looking for the most recent API version
4. fill in all the relevant details and create a new search resource
5. pay attention that you can customize your search to be specific for images, videos, etc. You should choose the instance-type accordingly.
Now, after you have a search resource, you can access the API key through the resource by going back to https://portal.azure.com/#home, clicking on the resource that you created, and then on “click here to manage keys”
Save the API key in your .env file, and we can use the code here to test it: here’s a bit improved version in Typescript:
const SUBSCRIPTION_KEY = process.env['AZURE_SUBSCRIPTION_KEY'];
if (!SUBSCRIPTION_KEY) {
throw new Error('Missing the AZURE_SUBSCRIPTION_KEY environment variable');
}
export async function bingWebSearch(query: string): Promise<any> {
const res = await fetch('https://api.bing.microsoft.com/v7.0/search?q=' + encodeURIComponent(query), {
method: 'GET',
headers: {
'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY as string,
'Content-Type': 'application/json; charset=UTF-8',
},
});
const response = await res.json();
if (response.webPages != null) {
for (const item of response.webPages.value) {
console.log(item);
}
const generalLink = response.webPages.webSearchUrl;
console.log(`bingWebSearch worked! we can also find all the results here: [${generalLink}]`);
} else {
console.log('bingWebSearch failed to find results');
}
return response;
}
And that’s it!
I’d love to hear about your experience using these APIs in the comment section. I also welcome you to follow Tikal’s Israeli tech radar publication for interesting reviews of the latest technology trends!
Enjoy!