newsapi-n
Get breaking news headlines, and search for articles from over 30,000 news sources - you need an API key from https://newsapi.org/ to get started.
Usage examples
Creating a client:
// Creating a client - you have to provide a valid API key.const client = 'paste your api key here';
Sources endpoint:
// Creating a query for searching news sources - serving business news from the US.const sourceQuery = CategoryBUSINESS CountryUS LanguageEN;// Searching for news sources.client;
Everything endpoint:
// Constructing a phrase (can be used when searching for articles/top headlines).// Must contain the syntagm "user data", the word "Apple". Must not contain the word "iPhone".const phrase = 'user data' 'Apple' 'iPhone';// Creating a query can be sent to the /everything endpoint.const everythingQuery = phrase;// Searching for articles using the query above.client;
Top headlines endpoint:
// Creating a query can be sent to the /top-headlines endpoint. Containing the word "Samsung", displaying 5 articles per page.const topHeadlineQuery = 'Samsung' undefined 5;// Searching for top headlines using the query above.client;
Get the number of the found articles:
// Everyhting endpointclient;// Top headlines endpointclient;
Response entities
- Article: an array of articles is returned by the searchForTopHeadlines and the searchForEverything methods.
- Source: an array of sources is returned by the searchForSources method.
Enums
- Country: Restriction on the country you want to get headlines for. Can be used when searching for sources and top headlines.
- Category: Restriction on the category you want to get headlines for. Can be used when searching for sources and top headlines.
- Language: Restriction on the language of the sources or the articles. Can be used on all endpoints.
- SortBy: Restriction on the order to sort the articles in. Can be used when searching for articles (Everything endpoint).
Links
News API documentation: https://newsapi.org/docs/