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.Parameters
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
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 name for news search context (optional)
Response
Returns an array of article objects or an error object.Array of news article objects (max 6 articles)
Example
Search Query Logic
The endpoint uses intelligent query construction:- With city: Searches for
"{city} clima"(e.g., “Madrid clima”) - Without city: Defaults to
"medio ambiente"(environment news)
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
| Code | Meaning |
|---|---|
| 200 | Success - Articles returned |
| 500 | Server Error - Failed to fetch from GNews |
Environment Variables
Required environment variable:Getting an API Key
- Visit gnews.io
- Sign up for a free account
- Copy your API key from the dashboard
- Add it to your
.env.localfile
Rate Limits
GNews free tier limits:- 100 requests/day
- 10 requests/minute
- Max 10 articles per request (we use 6)
Integration Guide
Basic Implementation
With Loading States
Caching Strategy
To respect API rate limits, consider implementing caching:Debugging
The API endpoint logs responses for debugging:- Full API responses
- Error messages
- Article count and quality
