Integrating the Bing News Search API
Blog|by Jamie Maguire|7 June 2019
In our last blog post we introduced the Bing Video Search API. Staying within the Bing ecosystem, we now take a look at another API that belongs to this family – the News Search API.
What is the Bing News Search API?
The Bing News Search API provides you with a similar experience to that of Bing News. It ships with several endpoints that help you find and surface relevant news articles based on your queries.
Key Features
You get access to 4 main features out of the box, here you can see a list of these features:
Get general news: Find news by sending a search query to the Bing News Search API, and getting back a list of relevant news articles.
Today’s top news: Get the top news stories for the day, across all categories.
News by category: News by category.
Headline news: Search for top headlines across all categories.
Get Trending News: Gets the news topics currently trending on social networks.
You can find out more information about these features here.
Additionally, you may also wish to augment your Bing News Search experience by introducing the Bing Auto Suggest API to display suggested search terms as you type.
Endpoints
Now that we’ve introduced the main features that ship with the Bing News Search API, let’s look at the endpoints that deliver the rich news searching functionality!
Sitting within the Cognitive Services namespace, you issue GET requests to one of the following API endpoints:
Sitting within the Cognitive Services namespace, you issue GET requests to one of the following API endpoints:
News: Returns the top news articles by category. For example, you can request the top sports or entertainment articles.
Search: Returns news articles based on the user’s search query. If the search query is empty, the call returns the top news articles.
Trending Topics: Returns trending news topics that are currently trending on social networks.
Each endpoint will then return a JSON payload that contains relevant news results which your application can then parse.
At the time of writing, Trending Topics is only available in en-US and zh=CN markets. You can find out more information about these information such as the requests and response objects each endpoint deals with here.
Consuming the Bing News Search API
Like other Cognitive Services APIs we’ve introduced in the past, consuming the News Search API involves a typical workflow that consists of the following steps:
- Creating a Cognitive Services account in Azure
- Constructing a valid HTTP request
- Processing the APIs JSON response
To get started, you can create a free account here. After you have your key, you then have a few options available to you in terms of consuming the Bing News Search API.
Dedicated SDK: At the time of writing there is a dedicated SDK with support for .NET, Java, Python, Node.js, and Go. The SDK reduces the amount of code you need to write and effectively wraps the low-level REST requests and responses that you’ll typical deal with when consuming the Bing News Search API. The SDKs also ship with predefined models (or business objects) that help make the development experience smoother. You can find out more about the dedicated SDKs here.
REST API: You also have the option of consuming the Bing News REST API endpoints directly. You get more control over the data and by handcrafting your models/business objects manually, can add the exact properties you need.
Constructing Requests
So, we’ve introduced the Bing News Search API and the main features it ships with, we’ll look at how you can create and send requests to the API endpoints. We’ll use Postman for testing purposes then see how easy it is to consume the API using C#.
Invoking the Bing News Search API using Postman
We’ll create a request in Postman and send it to the Bing News Search APIs “General News” endpoint. We need a search query so will supply a news topic that’s popular with Brits – the weather! This gets assigned to the parameter q.
We’ll set the “mkt” parameter. This tells the API which region in the world to search for “General News” – in our example GB.
Finally, we set the count parameter to 3. This tells the API to only fetch 3 results.
In the screenshot below we’re creating the GET request with these parameters:
Next, we click Send. The following JSON is returned from Bing News Search API. The key nodes to look for are:
- Original query: the query we sent
- Name: the news headline
- URL: the url of the news article
- Description: a longer form description of the news article
- Provider: the publisher of the news article
You can copy and paste one of the URLs into your browser and confirm “weather” search result is valid:
Invoking the Bing News Search API using C#
Now that we have an idea of the parameters you need to send and the typical response you can expect to receive from the General News endpoint, let’s look at what’s involved in terms of building the same request in C#.
Like other APIs that belong to the Cognitive Services family, consuming the Bing News Search API followed this workflow:
General Workflow
- Getting the relevant Cognitive Services key from your Azure Portal (you can create an account for free).
- Constructing a valid request.
- Processing the JSON message by parsing out the data returned by the Bing News Search API.
For simplicity, we’ll use a basic console application that performs all these steps. The code doesn’t need too much explanation.
We have some constants to store our Azure access key, News Search API endpoint url, search term and number of results we want (count).
There is a private method BingNewsSearch that accepts our search parameter which is used to form the request that we send to the Bing Search API, we then execute this request, take the JSON response and display this in the console window.
When you run this application in Visual Studio and examine the JSON response, you’ll see you have a nice formatted structed that contains news stories that match our search query:
You can take one of these URLs and paste it into your browser to confirm the website content matches the initial search term (weather).
You can find the source code for this C# console application on my GitHub profile here.
News by Category
One nice feature the Bing News API ships with is being able return news stories that belong to a given category. For example, sport, politics or entertainment. You can do this by supplying the category parameter.
This functionality lives in another endpoint: https://api.cognitive.microsoft.com/bing/v7.0/news
In this example we’re creating another request and sending it to Postman, this time we include the category parameter and set it to Business. Note you don’t need the parameter q which is the search term:
When we send this request to Postman, you can see all news stories have a business-related theme:
You can find a full list of supported categories by market here.
We’ve just seen how you can construct and send requests to the Bing News Search API. We’ve also looked at some of the available endpoints and functionality they offer. Be sure to check out the Trending Topics endpoint which will let you fetch news articles that are currently trending on social!
Ideas for Use Cases
You probably have your own ideas about what’s possible with the Bing News Search API, here are a few I have which spring to mind:
Reputation Management
In the online world, bad news can spread fast and it pays to be proactive to any potential emerging threats. Use the Bing News Search API to form part of middleware services that run continuously in the cloud that check for breaking news stories related to your product, brand or service.
News Aggregator
Combine the Bing News Search API with other APIs such as Facebook, Twitter, Reddit and Instagram and build services that aggregate data from around the web and surface content that you’re interested in, whether it be crypto-currency, current affairs or anything in between.
Which leads onto…
Analytics
Use the Bing News Search API and blend it with the Azure Cognitive Service Text Analytics API to identify the most popular key phrases, sentiment or even the entities being discussed. Present this information in easy-to-use dashboards, thereby giving users easy to understand insights that are immediately actionable.
Summary
In this blog post we’ve introduced the Bing News Search API. We’ve seen how you can easily integrate Bing News Search in a few lines of code. We used Postman to quickly test that our requests were formatted correctly and ran through a simple C# example.
We looked at 2 of the 3 available endpoints and explored one of the features that let you filter news stories by category, simply by supplying an additional parameter to your request.
If you need to integrate Video Search into your existing application, the Bing Video Search API ships with endpoints and features that make this seamless.
Are you considering introducing Azure Cognitive Services to your existing software applications? The Grey Matter Services team are Azure experts and can help you with your use case. Contact them on: (01364) 654200.
Contact Grey Matter
If you have any questions or want some extra information, complete the form below and one of the team will be in touch ASAP. If you have a specific use case, please let us know and we'll help you find the right solution faster.
By submitting this form you are agreeing to our Privacy Policy and Website Terms of Use.
Jamie Maguire
http://www.jamiemaguire.netSoftware Architect, Consultant, Developer, and Microsoft AI MVP. 15+ years’ experience architecting and building solutions using the .NET stack. Into tech, web, code, AI, machine learning, business and start-ups.
Related News
Build Your Own Multi-Itinerary Optimisation Services
Learn how to build an itinerary optimisation service on Bing Maps and Azure Maps, using an open-source optimiser library and an array of distance matrix between a set of origins and destinations.
Grey Matter Christmas and New Year Opening Hours
We’re coming to the end of yet another year of enabling our customers to do what they do best through the range of software and services we have on offer! We hope you have a wonderful time celebrating with your...
Top Geospatial Tools to Leverage Location Intelligence in the New Year
The ultimate guide for the top geospatial tools and APIs from leading map providers like Azure Maps and HERE Technologies.
Sophos Firewall XG Series EOL and XGS Migration
The Sophos XG Series hardware appliances will reach their end of life (EOL) on 31 March 2025. Now is the time to upgrade to the XGS Series Sophos Firewall Sophos recommends that you migrate to the XGS series. Reasons to...