Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 3 min read

Building a Real-Time News Aggregator with NewsData.io News API.

Building a Real-Time News Aggregator with NewsData.io News API A real-time news aggregator is a simple web page that fetches live headlines from an API and displays them automatically. In this guide, we have covered ho

Building a Real-Time News Aggregator with NewsData.io News API.

Building a Real-Time News Aggregator with NewsData.io News API

A real-time news aggregator is a simple web page that fetches live headlines from an API and displays them automatically. In this guide, we have covered how you can build a news aggregator using the NewsData.io News API using minimal coding and without the need for a backend server or database.

If you are new to working with APIs, this can be a great project to start with.
What you’ll need

  • A free NewsData.io account and API key
  • A text editor
  • A web browser

That’s it. No frameworks. No install.

Step 1: Get Your API Key

Sign up at newsdata.io/register and copy your API key from the dashboard. Every request to the API needs this key attached, kind of like showing an ID card so the service knows who's asking.

Step 2: Understand the API Request

NewsData.io’s News API works through a single URL that you customize with a few parameters. Here’s the basic shape of a request that fetches the latest news:

https://newsdata.io/api/1/latest?
apikey=YOUR_API_KEY&q=technology&language=en

Breaking that down:

  • apikey - your personal key.
  • q - the keyword you're searching for (here, "technology").
  • language - restricts results to English articles.

Paste that URL (with your real API key) into a browser tab, and you'll see a block of JSON, structured text containing article titles, links, images, and descriptions. That JSON is the raw material our aggregator will display nicely.

Step 3: Build the Page

First, create a new folder anywhere on your computer (for example, a folder called news-aggregator on your Desktop). Inside that folder, create a new file and name it exactly index.html.

You can do this in a couple of ways:

  • Using a code editor (recommended): Open a text editor like VS Code (free), open your new folder in it, then create a new file and name it index.html.
  • Using File Explorer or Finder: Create a new text file, then rename it from .txt to .html - just make sure your file explorer isn't hiding file extensions, or you'll end up with index.html.txt by mistake.

Once the file exists, open it in your editor and paste in the following code:

Here’s what’s happening, in plain terms:

  1. fetch(url) requests NewsData.io to retrieve data.
  2. .then(response => response.json()) converts the reply into something JavaScript can read.
  3. data.results.forEach(...) loops through each article and builds a small card with its title, description, and a link.
  4. If anything goes wrong (bad key, no internet), the catch block shows a friendly fallback message instead of a broken page.

Save the file, then double-click index.html in your folder (or drag it into a browser window). It will open directly in your browser, and you should see live tech headlines appear.

Step 4: Make it β€œReal-Time”

Right now, the page only fetches news once, when it loads. To make it refresh automatically, add one line before the closing tag:

setInterval(() => location.reload(), 300000); // refreshes every 5 minutes

This tells the browser to reload the page every 300,000 milliseconds (5 minutes), pulling fresh headlines each time.

Step 5: Make it Yours

From here, small tweaks go a long way:

  • Change q=technology to any topic, q=climate, q=gaming, q=your-city-name
  • Add a country=us parameter to focus on one region.
  • Style the cards with CSS to match your own design.

Wrapping Up

You now have a working, live-updating news aggregator built from a single HTML file. It’s a small project, but it covers real skills: making an API request, handling JSON, and rendering data dynamically, the same foundation used in much larger applications.

FAQs

Do I need a backend server for this?

No. Since NewsData.io allows requests directly from the browser, a single HTML file is enough for a basic version.

Is this safe to publish publicly with my API key visible?

Not recommended; for a live/public site, move the fetch call to a small backend so your key isn't exposed. Fine for local practice, though.

Can I filter news by country or category instead of keyword?

Yes, NewsData.io supports parameters like country and category alongside q, so that you can combine or swap them freely.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.