Tutorial on how to get started with the News API

World News API is an API that gives you access to thousands of news sources in over 50 languages from over 150 countries. The News accessed through the API is semantically tagged, allowing for semantic news search, where features like location extraction, organization extraction, person extraction, news author extraction, and semantic search is also available. This article will look at how you can get the World News API up and running.

Create an Account

To start, visit the World News API website and create an account. Once an account is created, confirming the email address is the next step. This is an important step, as you won't be able to access the API until you've confirmed your email. Simply click on the confirmation link to confirm that your email.

Getting the API Key

Now that your email is verified. Log in to your World News API account and obtain your API key. API key is a unique identifier that allows you to access the API and request news articles.

After you have successfully logged in, perform the following steps to obtain your API.



From your dashboard, click on "Profile". In the "Profile" section, click on "Show/Hide API Key" to show your API key.

Copy the API Key and save it somewhere safer for easier access. Don’t share it with anyone else!

Installing the SDK

You can simply run "pip install worldnewsapi" to get the Python package from PyPi.

Alternatively, after getting the API, the next step can be downloading and installing the SDK.

Download the SDK from the World News API website by navigating to the SDKs page. For this tutorial, we will be using Python.

Extract the downloaded SDK file to a folder on your computer to a suitable location.

Open Command Prompt in administrator mode by right-clicking on the Command Prompt icon and selecting "Run as administrator".

Navigate to the SDK directory using the "cd" command in Command Prompt (cd path/to/sdk_folder).

Execute the following command to install the openapi_client.

python setup.py install


After the installation process is complete, you are all good to go!

Making Your first Request

Now that the "openapi_client" and "worldnewsapi" has been successfully installed, let’s test it out to see if we can successfully fetch some news.

Let's go through the code step by step to see what is really happening.

import openapi_client
from pprint import pprint
from com.worldnewsapi import news_api

In the snippet above, we are importing the necessary libraries.

  1. openapi_client - Base library for communication with servers.
  2. pprint - For pretting printing.
  3. com.worldnewsapi - Our API for fetching news.
configuration = openapi_client.Configuration(
    host = "https://api.worldnewsapi.com"
)

In the snippet above, we are setting the host for the openapi client to communicate with. In case the host is not provided, it will default to "https://api.worldnewsapi.com".

configuration.api_key['apiKey'] = "your_api_key"

To ensure that we can properly get authenticated with the server and communicate, we need to set an API key. As explained at the start of this article, you can get an API key after logging into your account.

with openapi_client.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = news_api.NewsApi(api_client)
url = "https://www.bbc.com/news/world-us-canada-59340789" # str | The url of the news.
analyze = True # bool | Whether to analyze the news (extract entities etc.) (default to False)

try:
    # Extract News
    api_response = api_instance.extract_news(url, analyze)
    pprint(api_response)
except openapi_client.ApiException as e:
    print("Exception when calling NewsApi->extract_news: %s\n" % e)

The above snippet is where most of the heavy lifting is done, the following operations are performed here:

  1. A context with an instance of API client is created.
  2. Instance of API class is created.
  3. Url of a news article is provided.
  4. To extract entities and perform additional analysis, "analyze" is set to true.
  5. Finally, the news is extracted using the "extract_news" method.

Calling "extract_news" consumes 1 point, if "analyze" is set to be true, 2 additional points are consumed. In the demonstrated example, a total of 3 points (1 for extract news, 2 for setting analyze to true) will be consumed. More on points later on.

Once executed, you will get output as follows:



Since the reply is in JSON format, it is easy to filter data, useful especially when skimming through the data.

api_response = api_instance.extract_news(url, analyze)
pprint("Title: " + api_response['title'])
pprint("Author: " + api_response['author'])
pprint("Url: " + api_response['url'])
pprint("Text: " + api_response['text'])

Which would give the output as:


Full Code

Putting it all together, the final code is as follows:

import openapi_client
from pprint import pprint
from com.worldnewsapi import news_api


# Defining the host is optional and defaults to https://api.worldnewsapi.com
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
    host = "https://api.worldnewsapi.com"
)

# The client must configure the authentication and authorization parameters
# in accordance with the API server security policy.
# Examples for each auth method are provided below, use the example that
# satisfies your auth use case.

# Configure API key authorization: apiKey
configuration.api_key['apiKey'] = 'your_api_key'

# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['apiKey'] = 'Bearer'

# Enter a context with an instance of the API client
with openapi_client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = news_api.NewsApi(api_client)
    url = "https://www.bbc.com/news/world-us-canada-59340789" # str | The url of the news.
    analyze = True # bool | Whether to analyze the news (extract entities etc.) (default to False)

    try:
        # Extract News
        api_response = api_instance.extract_news(url, analyze)
        pprint(api_response)
    except openapi_client.ApiException as e:
        print("Exception when calling NewsApi->extract_news: %s\n" % e)

World News API provides a very accessible and convenient way to news from all over the world. It provides you with news from more than 50 languages from 150+ countries. One of the best things about the API is that it is semantically tagged - meaning you can filter news based on sentiment and find the exact set of news you are looking for. Easily, Efficiently and Effortlessly.

Happy coding.