API Tutorial: How to use BING News API

Yasu
6 min readJul 5, 2019

--

Bing News is a news aggregator powered by artificial intelligence and is part of Microsoft’s Bing search engine which processes billions of global searches, providing you access to news articles from all around the world. Using their API, you can enrich your app or website with news search offering world news grouped and filtered by topic, local news, and searchable metadata.

The Bing News API is available to developers on Rakuten RapidAPI and in this article, we will cover how to find the latest and greatest news using javascript and node.js.

Setup

To get started you’ll need

  1. Javascript editor and npm unirest package that will speed up code writing
  2. Rakuten RapidAPI account and the API key that comes with it

Getting the Javascript Code

Rakuten RapidAPI has you covered by packaging all the javascript code you need to work with the API in a single unirest SDK library which simplifies HTTP REST requests to save you time writing code.

Get started by installing the unirest package with the following command:

npm install unirest –save

It’s safe to ignore the npm warn messages at this point if you see any. As soon as you’ve got the package ready, you can start exploring the API.

Start with Signing Up to Rakuten RapidAPI

Before writing any code, you navigate to the API directly in your browser and open the Bing News Search API page.

To make requests you’ll need to log into your Rakuten RapidAPI account (or sign-up for one completely free). After signing in you’ll receive the API keys which verify that this request is coming from you. You’ll also get production ready code snippets that can be directly embedded into your app.

As soon as you sign-in with your account your API keys will appear in the input fields (X-Mashape-Key and X-Mashape-Host) in the Request Headers section. A ready to copy snippet of code will be appear on the right side:

Choosing a Plan

Bing News Search is priced using a freemium model which includes 100 daily requests for free. A 500 error code from the API may indicate that you have not chosen a plan yet. Proceed to the pricing page and choose the subscription plan. You’ll need to inout your credit card details but won’t charged unless you exceed the free quota.

Bing News Search API Overview

Searching the news functionality is broken down into three separate endpoints that you can see on the left side of the screen:

News in Category — 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. Only available for the US.

What’s new — Getting Trending Topics

Let’s get started with discovering what’s hot on people’s’ minds nowadays with trending topics:

  1. Click on the Trending Topics function on the left menu
  2. Press the big TEST Function button

The results will be always somewhat unpredictable, but it’s structure is always the same. Let’s look inside the response.

This response inside the body.value field clearly indicates that the German top model Heidi Klum made it to the top of the trending news on Halloween.

With each trending topic you’ll find also

  • An image URL and the organization or author of that image
  • A query with text that you can use in the Search function to find more articles on the topic
  • News Search Url you can redirect the user to read more about the topic
  • Web Search Url you can redirect the user to make a global internet search on the subject

Now that you’ve got your user intrigued with some trending topic, let’s dig deeper and find everything on the subject:

Bing News Search

Staying on the subject of costumes:

  1. Click on the Search function page on the left menu
  2. Enter “Halloween” in the “q” query string input field
  3. Press the TEST Function button

This time we’ve got a bit more information to go through:

_typeType hint. Always NewsreadLinkReadLinks provide the URL the bing news search api is able to return this result through. However, it’s not complete. At this point, we can ignore all readlinksqueryContextDescribes the request you’ve madesortDescribes how the results are ordered. Usually first by relevance and then by newest firstvalueThe field that contains your actual found results. Let’s look into it

The response value field

nameTitle a news outlet used for the articleurlLink to the article on the news websiteimageAn image with url and author informationdescriptionA short summary of the news articleaboutCollection of topics, categories, and subjects to which the article belongsproviderInformation about the news outlet that published this articledateThe date of the articlecategoryThe category to which the news article belongs

Making Search Requests

The code for making search requests is available at the right side of the screen. Feel free to copy it and use it directly in your app.

Also, you may choose to implement pagination — by using the count and offset query parameters.

For example, if your user interface displays 20 articles per page, set count to 20 and offset to 0 to get the first page of results. For each subsequent page, increment offset by 20 (for example, 0, 20, 40) while keeping the same count value. It is possible for multiple pages to include some overlap in results if new news appeared between the page requests.

To be more relevant to your user and make the search specific to some geographic region, set mkt parameter to one of the available markets, listed on the screen underneath the input field.

To be even more relevant, you could offer news that matches your site best, news of a specific category. Let’s see how to do just that.

News of Specific Category

The News in Category function gets you news filtered to a specific category.

Try it:

  1. Click on the News in Category function on the left menu
  2. Expand the Optional Parameters section
  3. In the Category input enter the word World_Europe
  4. Press the TEST Function button

The result is waiting for you under the value key inside body. It’s exactly the same as the result for the news search query. Take a look at the response fields table above if you need help understanding what is what.

However, the list of possible categories is limited. Using non-listed values will result in an error. Here is the list you can use:

Allowed Categories

  • Business
  • Entertainment
  • Entertainment_MovieAndTV
  • Entertainment_Music
  • Health
  • Politics
  • Products
  • Sports
  • Sports_Golf
  • Sports_MLB
  • Sports_NBA
  • Sports_NFL
  • Sports_NHL
  • Sports_Soccer
  • Sports_Tennis
  • Sports_CFB
  • Sports_CBB
  • World
  • World_Africa
  • World_Americas
  • World_Asia
  • World_Europe
  • World_MiddleEast
  • ScienceAndTechnology
  • Technology
  • Science
  • US
  • US_Northeast
  • US_South
  • US_Midwest
  • US_West

That’s It

You are ready to roll your application and display the latest news, trending topics and articles from various categories using JavaScript and Bing News Search on your website or app. Feel free to use the code in your productions applications and check out the other APIs from Rakuten RapidAPIs to enhance your application even further.

About Rakuten RapidAPI

Rakuten RapidAPI is the world’s largest API marketplace with 8,000+ third-party APIs and used by over 500,000 active developers. We enable developers to build transformative apps through the power of APIs. Find, test and connect to all the APIs you need in one place!

Check out some of the world’s best APIs including Microsoft, Sendgrid, Crunchbase, and Skyscanner.

Facebook | LinkedIn | Twitter

--

--