/NewsAPI-Javascript-Client

A Javascript implementation package of the popular NewsAPI (that actually works)

Primary LanguageJavaScriptMIT LicenseMIT

NewsAPI-Javascript-Client

A Javascript implementation package of the popular NewsAPI (that actually works) It is recommended you view the NewsAPI documentation to understand what each parameter does and how the endpoint is structured before using this package.

How to use

Download the code and move the /Src folder into your own project. The only important file for this to work is the /Src/index.js script which contains the main code that queries the NewsAPI

1. Creating a NewsAPIClient object

var client = new NewsAPIClient(APIKey); //replace APIKey with your own API key as a string
var client = new NewsAPIClient("1234567890"); //like this

2. Creating a NewsAPIQuery object

Everything:

var query = new NewsAPIQuery()
    .setKeywords("bitcoin")
    .setFrom("2023-07-17")
    .setTo("2023-08-17")
    .setSortBy("popularity")
    .setDomains("techcrunch.com, thenextweb.com");

Top-Headlines:

var query = new NewsAPIQuery()
    .setKeywords("trump")
    .setCountry("us")
    .setCategory("business");

Sources:

var query = new NewsAPIQuery()
    .setLanguage("en");

3. Sending the Query

Everything:

var response = await (client.everything(query)); //where response is a NewsAPIResponse object
console.log(response.status);
console.log(response.body); //contains articles as an array

Top Headlines:

var response = await (client.topHeadlines(query)); //where response is a NewsAPIResponse object
console.log(response.status);
console.log(response.body); //contains articles as an array

Sources:

var response = await (client.sources(query)); //where response is a NewsAPIResponse object
console.log(response.status);
console.log(response.body); //contains articles as an array