READMEmain
We couldn't render this README. Read it on GitHub
NewsAPI Service
A RESTful API service that wraps the NewsAPI.org endpoints to provide news articles, top headlines, and news sources. Built with Node.js, TypeScript, and Docker.
Features
- Get news articles with search and filtering
- Get top headlines by category and country
- Get news sources with filtering
- Pagination support
- Docker containerization
- Health check endpoint
- TypeScript for type safety
Prerequisites
- Node.js 20.x or Docker
- NewsAPI.org API key (get it from https://newsapi.org/register)
Environment Variables
Create a .env file in the root directory:
NEWS_API_KEY=your_api_key_here
NODE_ENV=development # or production
PORT=3000 # optional, defaults to 3000
Installation
Local Development
- Clone the repository:
git clone <repository-url>
cd newsapi_news
- Install dependencies:
npm install
- Start the development server:
npm run dev
Docker Deployment
- Build and start the container:
docker-compose up --build
- Stop the container:
docker-compose down
API Endpoints
GET /api/news/everything
Get articles based on search criteria.
Parameters:
q(required): Keywords or phrases to search for in the article title and bodyqInTitle: Keywords or phrases to search for in the article title onlysources: Comma-separated string of news source IDs (e.g., 'bbc-news,techcrunch')domains: Comma-separated string of domains to restrict the search to (e.g., 'bbc.co.uk,techcrunch.com')excludeDomains: Comma-separated string of domains to exclude from the searchfrom: ISO 8601 formatted date (e.g., '2025-03-14' or '2025-03-14T09:15:00Z')to: ISO 8601 formatted datelanguage: 2-letter ISO-639-1 language code. Available options:arArabicdeGermanenEnglishesSpanishfrFrenchheHebrewitItaliannlDutchnoNorwegianptPortugueseruRussiansvSwedishzhChinese
sortBy: The order to sort articles in. Available options:relevancy: Articles more closely related to the search querypopularity: Articles from popular sources and publisherspublishedAt: Newest articles first
page: Page number for paginated results (default: 1)pageSize: Number of results per page (default: 20, max: 100)
Example:
curl "http://localhost:3000/api/news/everything?q=bitcoin&language=en&sortBy=publishedAt&page=1&pageSize=5&domains=techcrunch.com,theverge.com"
GET /api/news/top-headlines
Get top headlines by category and country.
Parameters:
category: The category to get headlines for. Available options:businessentertainmentgeneralhealthsciencesportstechnology
country: The 2-letter ISO 3166-1 country code. Available options:aeUAEarArgentinaatAustriaauAustraliabeBelgiumbgBulgariabrBrazilcaCanadachSwitzerlandcnChinacoColombiacuCubaczCzech RepublicdeGermanyegEgyptfrFrancegbUnited KingdomgrGreecehkHong KonghuHungaryidIndonesiaieIrelandilIsraelinIndiaitItalyjpJapankrSouth KorealtLithuanialvLatviamaMoroccomxMexicomyMalaysiangNigerianlNetherlandsnoNorwaynzNew ZealandphPhilippinesplPolandptPortugalroRomaniarsSerbiaruRussiasaSaudi ArabiaseSwedensgSingaporesiSloveniaskSlovakiathThailandtrTurkeytwTaiwanuaUkraineusUnited StatesveVenezuelazaSouth Africa
sources: Comma-separated string of news source IDs (e.g., 'bbc-news,techcrunch') Note: Cannot be mixed withcountryorcategoryparametersq: Keywords or phrases to search for in headlinespage: Page number for paginated results (default: 1)pageSize: Number of results per page (default: 20, max: 100)
Example:
curl "http://localhost:3000/api/news/top-headlines?category=technology&country=us&page=1&pageSize=5&q=AI"
GET /api/news/sources
Get news sources with filtering.
Parameters:
category: Filter sources by category. Available options:businessentertainmentgeneralhealthsciencesportstechnology
language: Filter sources by language. Uses 2-letter ISO-639-1 codes:arArabicdeGermanenEnglishesSpanishfrFrenchheHebrewitItaliannlDutchnoNorwegianptPortugueseruRussiansvSwedishzhChinese
country: Filter sources by country. Uses 2-letter ISO 3166-1 codes (same as top-headlines)
Example:
curl "http://localhost:3000/api/news/sources?category=business&language=en&country=us"
Response Format
Articles Response
{
"articles": [
{
"title": "Article Title",
"url": "https://article.url",
"description": "Article description",
"imageUrl": "https://image.url",
"publishedAt": "2025-03-14T10:00:00Z",
"category": "technology"
}
],
"timestamp": 1741944027694
}
Sources Response
{
"sources": [
{
"id": "source-id",
"name": "Source Name",
"description": "Source description",
"url": "https://source.url",
"category": "business",
"language": "en",
"country": "us"
}
],
"timestamp": 1741944027694
}
Error Handling
The API returns appropriate HTTP status codes:
- 200: Success
- 400: Bad Request (invalid parameters)
- 401: Unauthorized (invalid API key)
- 429: Too Many Requests
- 500: Internal Server Error
Development
# Run tests
npm test
# Build TypeScript
npm run build
# Start production server
npm start
License
MIT