Socket
Socket
Sign inDemoInstall

newsapi-node

Package Overview
Dependencies
8
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    newsapi-node

News API Client - https://newsapi.org/


Version published
Weekly downloads
2
Maintainers
1
Install size
617 kB
Created
Weekly downloads
 

Readme

Source

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'

// Creating a client - you have to provide a valid API key.
const client = new NewsClient('paste your api key here');

Sources endpoint:

// Creating a query for searching news sources - serving business news from the US.
const sourceQuery = new SourceQuery(Category.BUSINESS, Country.US, Language.EN);
// Searching for news sources.
client.searchForSources(sourceQuery)
.then(sources => console.log(sources))
.catch(error => console.log(error));

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 = new Phrase(['user data'], ['Apple'], ['iPhone']);
// Creating a query can be sent to the /everything endpoint.
const everythingQuery = new EverythingQuery(phrase);
// Searching for articles using the query above.
client.searchForEverything(everythingQuery)
.then(articles => {
    console.log(articles);
})
.catch(error => {
    console.log(error);
});

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 = new TopHeadlineQuery(['Samsung'], undefined, 5);
// Searching for top headlines using the query above.
client.searchForTopHeadlines(topHeadlineQuery)
.then(articles => console.log(articles))
.catch(error => console.log(error));

Get the number of the found articles:

// Everyhting endpoint
client.getArticlesCount(everythingQuery)
.then(count => console.log(`Number of found articles: ${count}.`))
.catch(error => console.log(error));
// Top headlines endpoint
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).

News API documentation: https://newsapi.org/docs/

License

MIT

Keywords

FAQs

Last updated on 19 Jan 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc