Tutorial on how to get started with the News API
World News API gives you access to thousands of news sources in 86+ languages from 210+ countries. The news accessed through the API is semantically tagged, allowing for semantic news search by location, person or organization. This tutorial shows how to get the World News API up and running with Python.
Create an account
To start, visit the World News API website and create an account. Once the account is created, the next step is to confirm your email address.
You will receive an email at the address you registered with. It will look something like this:
Simply click on confirm your account, and your account is verified.
Get your API key
Now that your email is verified, log in to the API console and get your API key. The API key is a unique identifier that allows you to access the API and request news articles.
After you log in, the console opens on the Dashboard, with your key at the top under "Your API key". It is hidden at first: click Show to see it, or Copy to copy it.
Keep the API key somewhere safe and don't share it with anyone. If it ever leaks, generate a new one under "Profile & API key".
Use the Python SDK
For this tutorial, we will be using the worldnewsapi Python SDK. See our SDK page if you want to try the API in another language.
Install the SDK
To install the SDK, run the following command:
pip install worldnewsapi pip downloads the SDK and its dependencies (pydantic, urllib3, python-dateutil and a few more). The output looks like this (shortened):
Collecting worldnewsapi
Downloading worldnewsapi-2.2.0-py3-none-any.whl.metadata (2.1 kB)
…
Installing collected packages: urllib3, tzdata, typing-extensions, annotated-types, typing-inspection, python-dateutil, pydantic-core, pydantic, worldnewsapi
Successfully installed annotated-types-0.8.0 pydantic-2.14.4 pydantic-core-2.50.5 python-dateutil-4.0.0 typing-extensions-4.16.0 typing-inspection-0.4.5 tzdata-2026.6 urllib3-2.0.7 worldnewsapi-2.2.0 We have installed the worldnewsapi SDK for Python and its dependencies!
Make your first request
Now that the SDK is installed, let's test it out to see if we can fetch some news.
Let's use the search_news function to search for news that contain the word "football" in the UK.
Step 1. Import the SDK
import worldnewsapi
from worldnewsapi.rest import ApiException The imports are as follows:
- worldnewsapi: the SDK itself
- worldnewsapi.rest.ApiException: the API exception object for exception handling
Step 2. Initialize the configuration
Initialize the API configuration and set the API key with:
newsapi_configuration = worldnewsapi.Configuration(api_key={'apiKey': '<YOUR_API_KEY>'}) Replace <YOUR_API_KEY> with the API key you got above.
Step 3. Create a NewsApi instance and start a search
try:
newsapi_instance = worldnewsapi.NewsApi(worldnewsapi.ApiClient(newsapi_configuration))
response = newsapi_instance.search_news(
text='football',
source_country='gb',
language='en',
earliest_publish_date='2024-07-01',
latest_publish_date='2024-07-17',
categories='sports',
sort="publish-time",
sort_direction="desc",
min_sentiment=-0.8,
max_sentiment=0.8,
offset=0,
number=10)
except ApiException as e:
print("Exception when calling NewsApi->search_news: %s\n" % e) Here we search for news that contain the word football in the UK (source_country='gb') in the English language. The news will have a sentiment between -0.8 and 0.8 and will be published between 2024-07-01 and 2024-07-17. A total of 10 such news articles will be fetched.
You can set text to an empty string to limit the filtering to the other parameters.
See the parameters of search_news for more details.
Step 4. Process the response
Now we can process the response and output the retrieved data:
for article in response.news:
print("\nTitle: " + str(article.title))
print("Authors: " + str(article.authors))
print("URL: " + str(article.url))
print("Sentiment: " + str(article.sentiment))
print("Text: " + str(article.text[:80]) + "...") # print first 80 characters of the text Step 5. Add paging (optional)
The API responds with a maximum of 100 articles. If you want to retrieve more, you'll need to add paging to your query.
Your code could look like this:
max_results = 250 # replace with your maximum
offset = 0
all_results = []
while len(all_results) < max_results:
request_count = min(100, max_results - len(all_results)) # request 100 or the remaining number of articles
response = newsapi_instance.search_news(
text='football',
source_country='gb',
language='en',
earliest_publish_date='2024-07-01',
latest_publish_date='2024-07-17',
categories='sports',
sort="publish-time",
sort_direction="desc",
min_sentiment=-0.8,
max_sentiment=0.8,
offset=offset,
number=request_count)
print("Retrieved " + str(len(response.news)) + " articles. Offset: " + str(offset) + "/" + str(max_results) +
". Total available: " + str(response.available) + ".")
if len(response.news) == 0:
break
all_results.extend(response.news)
offset += 100 Full code
The full code is as follows:
import worldnewsapi
# Initial SDK configuration
newsapi_configuration = worldnewsapi.Configuration(api_key={'apiKey': '<YOUR API KEY>'})
try:
newsapi_instance = worldnewsapi.NewsApi(worldnewsapi.ApiClient(newsapi_configuration))
max_results = 250 # replace with your maximum
offset = 0
all_results = []
while len(all_results) < max_results:
request_count = min(100, max_results - len(all_results)) # request 100 or the remaining number of articles
response = newsapi_instance.search_news(
text='football',
source_country='gb',
language='en',
earliest_publish_date='2024-07-01',
latest_publish_date='2024-07-17',
categories='sports',
sort="publish-time",
sort_direction="desc",
min_sentiment=-0.8,
max_sentiment=0.8,
offset=offset,
number=request_count)
print("Retrieved " + str(len(response.news)) + " articles. Offset: " + str(offset) + "/" + str(max_results) +
". Total available: " + str(response.available) + ".")
if len(response.news) == 0:
break
all_results.extend(response.news)
offset += 100
except worldnewsapi.ApiException as e:
print("Exception when calling NewsApi->search_news: %s\n" % e)
for article in all_results:
print("\nTitle: " + str(article.title))
print("Authors: " + str(article.authors))
print("URL: " + str(article.url))
print("Sentiment: " + str(article.sentiment))
print("Text: " + str(article.text[:80]) + "...") # print first 80 characters of the text Get the top news
To get the top news for a given day, country and language, you can run this script:
try:
response = newsapi_instance.top_news(
source_country='us',
language='en',
var_date='2024-06-01',
headlines_only=False)
except worldnewsapi.ApiException as e:
print("Exception when calling NewsApi->top_news: %s\n" % e)
for news in response.top_news:
print("\n\nNext Cluster ##########################################\n")
for article in news.news:
print("Title: " + str(article.title))
print("URL: " + str(article.url))
print("Text: " + str(article.text[:80]) + "...") # print first 80 characters of the text And that's it! You can explore more functions, such as extracting news links or extracting news from a page, in the Python SDK docs on GitHub.
Happy coding.
Frequently asked questions
How do I get started with World News API?
Sign up for a free API key at worldnewsapi.com/console, then make your first API call using the search-news endpoint. No credit card is required for the free tier.
What programming languages are supported?
World News API provides SDKs for 21 programming languages including Python, JavaScript, Java, C#, PHP, Ruby, Go, and more. You can also use any language that supports HTTP requests.
Do I need a credit card to start?
No. The free tier includes 50 daily API points with no credit card required. You can upgrade to a paid plan later if you need more capacity.
How do I make my first API call?
After getting your API key, make a GET request to https://api.worldnewsapi.com/search-news?api-key=YOUR-API-KEY&text=tesla. Replace YOUR-API-KEY with your actual key. The response will contain matching news articles in JSON format.