Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erencanpelin/newsapi-javascript-client
A Javascript implementation package of the popular NewsAPI (that actually works)
https://github.com/erencanpelin/newsapi-javascript-client
Last synced: 3 days ago
JSON representation
A Javascript implementation package of the popular NewsAPI (that actually works)
- Host: GitHub
- URL: https://github.com/erencanpelin/newsapi-javascript-client
- Owner: ErencanPelin
- License: mit
- Created: 2023-07-18T01:30:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-19T02:04:09.000Z (over 1 year ago)
- Last Synced: 2024-10-11T10:31:25.253Z (26 days ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NewsAPI-Javascript-Client
A Javascript implementation package of the popular [NewsAPI](https://newsapi.org/) (that actually works)
It is recommended you view the [NewsAPI documentation](https://newsapi.org/docs) 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
```js
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:
```js
var query = new NewsAPIQuery()
.setKeywords("bitcoin")
.setFrom("2023-07-17")
.setTo("2023-08-17")
.setSortBy("popularity")
.setDomains("techcrunch.com, thenextweb.com");
```
Top-Headlines:
```js
var query = new NewsAPIQuery()
.setKeywords("trump")
.setCountry("us")
.setCategory("business");
```
Sources:
```js
var query = new NewsAPIQuery()
.setLanguage("en");
```## 3. Sending the Query
Everything:
```js
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:
```js
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:
```js
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
```