Skip to main content

Overview

The News API retrieves relevant weather and climate news articles using the GNews API. It provides localized news content based on the city’s weather context.

Client Function

getNews

Client-side function to fetch news articles for a specific city.
export const getNews = async (city: string)

Parameters

city
string
required
City name to fetch weather-related news for

Returns

Returns a promise that resolves to an array of news article objects, or an empty array if the request fails.

Example

import { getNews } from '@/lib/api/news';

const articles = await getNews('Buenos Aires');

articles.forEach(article => {
  console.log(article.title);
  console.log(article.url);
});

Error Handling

The function catches all errors and returns an empty array instead of throwing. Errors are logged to the console with the prefix “News Fetch Error:”.

API Endpoint

GET /api/news

Server-side endpoint that fetches news from GNews API.

Query Parameters

city
string
City name for news search context (optional)

Response

Returns an array of article objects or an error object.
articles
array
Array of news article objects (max 6 articles)

Example

const response = await fetch('/api/news?city=Madrid');
const articles = await response.json();

console.log(articles);

Search Query Logic

The endpoint uses intelligent query construction:
const query = city ? `${city} clima` : "medio ambiente";
  • With city: Searches for "{city} clima" (e.g., “Madrid clima”)
  • Without city: Defaults to "medio ambiente" (environment news)
This ensures relevant results even when city parameter is missing.

Request Configuration

Base URL: https://gnews.io/api/v4/search Parameters:
  • q: Search query (city + “clima” or “medio ambiente”)
  • lang: Language code (“es” for Spanish)
  • max: Maximum articles (6)
  • apikey: GNews API key from environment variable

Status Codes

CodeMeaning
200Success - Articles returned
500Server Error - Failed to fetch from GNews

Environment Variables

Required environment variable:
NEWS_API_KEY=your_gnews_api_key

Getting an API Key

  1. Visit gnews.io
  2. Sign up for a free account
  3. Copy your API key from the dashboard
  4. Add it to your .env.local file

Rate Limits

GNews free tier limits:
  • 100 requests/day
  • 10 requests/minute
  • Max 10 articles per request (we use 6)

Integration Guide

Basic Implementation

import { getNews } from '@/lib/api/news';

// Fetch news for current city
const articles = await getNews(currentCity);

if (articles.length > 0) {
  console.log(`Found ${articles.length} articles`);
} else {
  console.log('No news available');
}

With Loading States

const [news, setNews] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
  const fetchNews = async () => {
    try {
      setLoading(true);
      const articles = await getNews(city);
      setNews(articles);
    } catch (err) {
      setError('Failed to load news');
    } finally {
      setLoading(false);
    }
  };
  
  fetchNews();
}, [city]);

Caching Strategy

To respect API rate limits, consider implementing caching:
const CACHE_DURATION = 30 * 60 * 1000; // 30 minutes
const newsCache = new Map();

export const getNewsWithCache = async (city: string) => {
  const cacheKey = city.toLowerCase();
  const cached = newsCache.get(cacheKey);
  
  if (cached && Date.now() - cached.timestamp < CACHE_DURATION) {
    return cached.data;
  }
  
  const articles = await getNews(city);
  newsCache.set(cacheKey, {
    data: articles,
    timestamp: Date.now()
  });
  
  return articles;
};

Debugging

The API endpoint logs responses for debugging:
console.log("GNews Response:", data);
Check your development console or server logs to inspect:
  • Full API responses
  • Error messages
  • Article count and quality

Build docs developers (and LLMs) love