Querying NewsData.io in JavaScript with Pagination
Languages: JavaScript, curl · Estimated time: ~7 minutes · NewsData.io live status ↗
NewsData.io's free tier gives 200 calls/day. This tutorial covers fetching top news, filtering by country and category, handling the nextPage token for pagination, and staying within the daily cap.
NewsData.io aggregates 50,000+ sources across 150+ countries. The free tier is 200 calls/day with a required API key (email signup, no credit card). At 10 articles per call and 200 calls/day, you can access up to 2,000 articles per day on the free plan.
Live status at /newsdata.
Your First Request
# Replace YOUR_KEY with your key from the NewsData dashboard
curl "https://newsdata.io/api/1/news?apikey=YOUR_KEY&language=en&category=technology"
# Response structure:
# {
# "status": "success",
# "totalResults": 1240,
# "results": [ { "title": "...", "link": "...", "pubDate": "...", ... }, ... ],
# "nextPage": "16816293875" <-- token for the next page, null if last page
# } JavaScript: News Fetcher with Pagination
const NEWSDATA_BASE = "https://newsdata.io/api/1/news";
const API_KEY = process.env.NEWSDATA_KEY; // store in environment, never hardcode
async function fetchNews({ country, category, query, language = "en", nextPage = null } = {}) {
const params = new URLSearchParams({ apikey: API_KEY, language });
if (country) params.set("country", country);
if (category) params.set("category", category);
if (query) params.set("q", query);
if (nextPage) params.set("nextPage", nextPage); // pagination token
const res = await fetch(`${NEWSDATA_BASE}?${params}`);
if (!res.ok) {
const err = await res.json();
throw new Error(`NewsData error: ${err.message ?? res.status}`);
}
return res.json();
// Returns: { status, totalResults, results: [...], nextPage: string|null }
}
// Fetch page 1
const page1 = await fetchNews({ country: "us", category: "technology" });
console.log(`Total results: ${page1.totalResults}`);
page1.results.forEach(a => console.log(`- ${a.title} (${a.pubDate})`));
// Fetch page 2 using the nextPage token (costs another API call)
if (page1.nextPage) {
const page2 = await fetchNews({
country: "us",
category: "technology",
nextPage: page1.nextPage
});
page2.results.forEach(a => console.log(`- ${a.title}`));
} Managing the 200 Calls/Day Limit
200 calls/day = roughly one call every 7.2 minutes if polling continuously. In practice, most applications don't need continuous polling — a 15-minute cache interval is more appropriate for news.
// Simple in-memory cache for news results
const cache = new Map();
const CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
async function getCachedNews(params) {
const key = JSON.stringify(params);
const cached = cache.get(key);
if (cached && Date.now() - cached.fetchedAt < CACHE_TTL_MS) {
console.log("Serving from cache");
return cached.data;
}
const data = await fetchNews(params);
cache.set(key, { data, fetchedAt: Date.now() });
return data;
}
// Usage — will only call NewsData if cache is stale
const techNews = await getCachedNews({ country: "us", category: "technology" }); Filtering Tips
- Multiple countries: country=us,gb,au (comma-separated, up to 5 on free tier)
- Multiple categories: category=technology,science (comma-separated)
- Full-text search: q=artificial+intelligence returns articles where the text contains the phrase
- Exclude domains: excludedomain=example.com to filter out low-quality sources
- Date range: from_date=2026-05-01&to_date=2026-05-07 (format: YYYY-MM-DD)
- The nextPage token encodes the pagination state — you must pass all other filters again alongside it
When to Upgrade
At 200 calls/day you'll hit the limit if you have multiple users polling or if you're paginating through many results. The paid plans start at around $15/month for more daily calls and higher results-per-page.
Alternatives to consider: The Guardian API (500 calls/day, no credit card) if you can work with Guardian content; GNews (100 calls/day, key) for broader coverage with a smaller limit. See /category/news for the live-ranked full list.