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:
import { NewsClient, EverythingQuery, Phrase, SourceQuery, Category, Country, Language, TopHeadlineQuery } from 'newsapi-node'
const client = new NewsClient('paste your api key here');
Sources endpoint:
const sourceQuery = new SourceQuery(Category.BUSINESS, Country.US, Language.EN);
client.searchForSources(sourceQuery)
.then(sources => console.log(sources))
.catch(error => console.log(error));
Everything endpoint:
const phrase = new Phrase(['user data'], ['Apple'], ['iPhone']);
const everythingQuery = new EverythingQuery(phrase);
client.searchForEverything(everythingQuery)
.then(articles => {
console.log(articles);
})
.catch(error => {
console.log(error);
});
Top headlines endpoint:
const topHeadlineQuery = new TopHeadlineQuery(['Samsung'], undefined, 5);
client.searchForTopHeadlines(topHeadlineQuery)
.then(articles => console.log(articles))
.catch(error => console.log(error));
Get the number of the found articles:
client.getArticlesCount(everythingQuery)
.then(count => console.log(`Number of found articles: ${count}.`))
.catch(error => console.log(error));
client.getArticlesCount(topHeadlineQuery)
.then(count => console.log(`Number of found headlines: ${count}.`))
.catch(error => console.log(error));
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/
License
MIT