npm init -y
There is an npm module that will play a major role in the functionality of this application. This is the got module, which is an HTTP request library for NodeJS. The following line of code will install the latest version of the got library in your application files:
npm install got@latest
Now, you can go ahead and build your application.
// import the got library into your application import got from 'got'; // fetch data from an API and prints its body to the terminal (async () => { try { const response = await got(URL); const data = JSON.parse(response.body); console.log(data); } catch (error) { console.log(error.data); } })();
The above application will pull data from any REST API on the web. However, you first need to provide the URL to the relevant resource.
The Weatherbit.io API is one of the more popular weather APIs. Inserting the URL of this API into the simple JavaScript application above will get it working.
// import the got library into your application const got = require('got'); // fetch data from an API and prints its body to the terminal (async () => { try { const URL = 'https://api.weatherbit.io/v2.0/current?lat=40.7128&lon=-74.0060&key=API_KEY'; const response = await got(URL); const data = JSON.parse(response.body); console.log(data); } catch (error) { console.log(error.data); } })();
The URL for the Weatherbit.io API is now part of the app. However, there is one aspect of the URL that you need to adjust for the app to run. This is the section labeled 'API_KEY'. The API key is what you will receive from Weatherbit.io when you sign up for a free account.
You also have the option to adjust the query in the above code. The app currently queries the weather at latitude 40.7128 and longitude -74.0060, but you can insert new coordinates. Although the query above is the recommended approach, you can search for the weather in a location by city name.
After inserting the API key in the relevant section above, you can now execute your JavaScript file. The application will display something like the following output in your terminal:
Some of the more important aspects of response data include:
This section uses the Newsdata.io API. Like all REST APIs on the web, it provides several query options that you can use to retrieve breaking news from around the world. With the Newsdata.io API, you can get news from a specific country or in a specific language, category, etc.
Using JavaScript Application you can retrieve data from the news REST API. Just replace the URL in the above application with the following URL:
'https://newsdata.io/api/1/news?apikey=YOUR_API_KEY&country=us'
The next step is to replace the 'YOUR_API_KEY' part in the URL above with the API key that you will receive after registering with Newsdata.io. The URL above will return breaking news from the US. However, if you want news from Japan, you can simply replace the query 'country=us' with 'country=jp' .
The response body will be an array of objects containing news from the US. The following object is the first object in the array:
You can get data for your website or app in any programming language you're familiar with. So, if you don't want to use JavaScript, you can achieve the same result with a Python application. You can build a Python API using one of its frameworks or get data from an existing REST API using a simple script.
All you need to do is install the request python HTTP module using the pip environment. Then you can build your Python application with the following code:
# import the requests library import requests # grabbing data using the requests library URL = 'https://newsdata.io/api/1/sources?apikey=YOUR_API_KEY=us' res = requests.get(URL) json = res.json() for key in json: print(key, json[key])
Like the previous examples, you will need to insert your API key in the relevant section. You will then receive a response body similar to what the JavaScript application returns.