Migrating an existing Python app off the NewsAPI.org client
(newsapi-python) and onto newsdata.io? This repo gives
you a drop-in compatibility layer plus a step-by-step migration guide so you can
switch with minimal code changes.
The newsdata_compat.NewsDataClient class exposes the same method names and
return shape as newsapi.NewsApiClient — get_top_headlines(), get_everything()
and get_sources() — but talks to the newsdata.io REST API (https://newsdata.io/api/1)
under the hood and translates the responses back into the NewsAPI.org format your
code already expects.
Everything here is built and tested against the newsdata.io FREE tier.
| NewsAPI.org | newsdata.io | |
|---|---|---|
| Auth | X-Api-Key header / apiKey param |
apikey query param |
| Latest news | /v2/top-headlines |
/api/1/news |
| Search | /v2/everything |
/api/1/news?q= |
| Sources | /v2/sources |
/api/1/sources |
| Pagination | numeric page |
opaque nextPage token |
| Free tier | limited, dev only | generous, production-friendly |
git clone https://github.com/your-account/python-newsapi-to-newsdata-migration.git
cd python-newsapi-to-newsdata-migration
pip install -r requirements.txt
export NEWSDATA_API_KEY="your_free_api_key_here" # Windows: set NEWSDATA_API_KEY=...
python -m examples.demoGet a free key at https://newsdata.io/register.
Before (NewsAPI.org):
from newsapi import NewsApiClient
client = NewsApiClient(api_key="YOUR_NEWSAPI_KEY")
resp = client.get_top_headlines(q="bitcoin", language="en", country="us")
for article in resp["articles"]:
print(article["title"], "-", article["source"]["name"])After (newsdata.io — change two lines):
from newsdata_compat import NewsDataClient
client = NewsDataClient() # reads NEWSDATA_API_KEY from the environment
resp = client.get_top_headlines(q="bitcoin", language="en", country="us")
for article in resp["articles"]:
print(article["title"], "-", article["source"]["name"])The loop body is unchanged: the compat layer returns the same
{"status", "totalResults", "articles": [...]} structure, where each article has
title, description, url, urlToImage, publishedAt, content, author and
a nested source dict.
If api_key is omitted it is read from NEWSDATA_API_KEY. Raises ValueError
if neither is provided.
Maps to GET /news. Returns NewsAPI-shaped dict.
Maps to GET /news (newsdata.io uses one news endpoint for both). domains
maps to newsdata.io's domain filter.
Maps to GET /sources.
NewsAPI.org uses an incrementing integer page. newsdata.io uses an opaque
nextPage token returned in each response. The compat layer surfaces this token
in two places so you can choose your migration style:
- The returned dict includes a
nextPagekey (the raw newsdata.io token). - You may pass that token straight back as the
page=argument to fetch the next page.
resp = client.get_everything(q="climate")
while resp.get("nextPage"):
resp = client.get_everything(q="climate", page=resp["nextPage"])All API errors raise newsdata_compat.NewsDataException (aliased as
NewsAPIException for familiarity). The client handles, with clear messages:
- 401 — invalid/missing API key
- 429 — rate limit reached (back off and retry)
- 403 / 422 — a requested feature requires a paid plan (see below)
- empty
results— returns an emptyarticleslist instead of crashing
The following are not available on the free tier. This kit never relies on them in the core flow; they are exposed only as opt-in flags that degrade gracefully (the client detects the 403/422 "upgrade your plan" response, logs a warning, and returns results without the paid feature):
sentiment/ sentiment analysis- AI fields:
ai_tag,ai_region,ai_org,ai_summary - the historical
/archiveendpoint and long date ranges - advanced full-text query operators
| Env var | Required | Description |
|---|---|---|
NEWSDATA_API_KEY |
yes | Your newsdata.io API key (free tier works). |
newsdata_compat/
__init__.py # public exports
client.py # NewsDataClient drop-in + response translation
errors.py # NewsDataException / NewsAPIException alias
examples/
demo.py # runnable before/after demo
MIT — see headers. Not affiliated with NewsAPI.org. Built to showcase newsdata.io.