A quick start guide on how to make requests to APIs on RapidAPI using Node.js. We step through the process of getting the necessary authenication tokens on RapidAPI and then demonstrate with simple examples how to use these authentication tokens to make requests with https, Axios, Got and SuperAgent.
The purpose of this guide is to quickly help readers get up and running consuming APIs available on RapidAPI using Node.js. All our examples show how to properly pass RapidAPI authentication tokens into the header of the request using the example libraries. We will be using the Parazun Amazon Data API to demonstrate making requests and we will need to get an RapidAPI account ready by subscribing to the free basic plan and getting the API key so that we can make our requests.
Parazun Amazon Data API is a real-time web scraping api which allows any user to get up-to-date data from all of Amazon's worldwide marketplace websites easily without getting into the nitty gritty of web scraping such as handling captchas and proxies.
To follow this article you will need to:
npm
and use of the console by calling node
. All examples here are run on Node.js v16.If you haven't already, register for an account at RapidAPI at https://rapidapi.com/auth/sign-up
Visit https://rapidapi.com/bernardko/api/parazun-amazon-data/ and click on the "Subscribe to Test" button.
You will be brought to the Pricing page, click on the "Subscribe" button under the Basic Plan.
Next you will be taken to the subscribe page. RapidAPI requires a credit card to subscribe to APIs. Make sure you see on the right side of the page that the Total Due Today is "$0.00". Enter your credit card details and click "Pay Now".
Now we need to gather the information we need to make the API request.
Next, we will collect the details we will need to make the request. We will using the product endpoint which will grab all the data on Amazon's product page and return the data in clean and ready to use JSON format.
Go back to https://rapidapi.com/bernardko/api/parazun-amazon-data/ and on the left side of the page, click on "Product Detail".
On the right side of the same page, click on the drop down menu and select "Shell" and then select "cURL". A script will show up below which will make it easy for you to copy and paste the required header information and URL. Copy the information in the red box below.
What you should have now:
https://parazun-amazon-data.p.rapidapi.com/product/
x-rapidapi-host : parazun-amazon-data.p.rapidapi.com
x-rapidapi-key : <Your RapidAPI Key>
The "x-rapidapi-key" which is blurred in the screenshot is a secret key and you should keep it in a safe place.
Next we will need a Amazon product ASIN code. ASIN codes are Amazon's code for uniquely identifying products in their catalog. In this demonstration we will use B00FLYWNYQ
which is the ASIN code for an instant pot pressure cooker.
Also, we will need the region code of the Amazon marketplace that these products are listed on, in this case for the US Amazon marketplace, it is US
. All Amazon Marketplaces is supported. For a list of other region codes please refer to https://rapidapi.com/bernardko/api/parazun-amazon-data/ documentation.
Lets combine the endpoint URL, product ASIN and region to create the full request URL
https://parazun-amazon-data.p.rapidapi.com/product/?asin=B00FLYWNYQ®ion=US
Notice that the parameter name for the ASIN code is asin
and the parameter name for the region code is region
.
We now have all the information we need to make the request.
Here we will be using the built-in request modules called http and https. This module can be imported without any additional package installation and is straightforward to use. If the URL you are calling is http, then use the http module. Otherwise use the https module which we will be using below.
An explanation the following code is as follows:
https.request
function and pass options as the first parameter and the event callback function as the second parameter.req.end
// Step 1
var https = require('https');
// Step 2
var options = {
host: "parazun-amazon-data.p.rapidapi.com",
port: 443,
path: "/product/?asin=B00FLYWNYQ®ion=US",
method: "GET",
headers: {
"x-rapidapi-host":"parazun-amazon-data.p.rapidapi.com",
"x-rapidapi-key":"<Your RapidAPI Key>"
}
};
// Step 3
var req = https.request(options, function(res) {
var data = [];
res.on('data', function(chunk) {
data.push(chunk);
});
res.on('end', function() {
var product = JSON.parse(Buffer.concat(data).toString());
console.log(product);
});
});
req.on('error', function(error) {
console.error(error);
})
req.end();
Axios is an http client module that works both in Node.js and in the browser. It is a popular promise-based library which would allow you to convert the following code snippet for an async/await format.
First we have to install the library on command line using npm
:
npm install --save axios
Similarly to the last example, this is how this snippet works:
axios.request
and pass in the options hash table to create the request and set a callback function to handle the response data.// Step 1
var axios = require("axios").default;
// Step 2
var options = {
method: 'GET',
url: 'https://parazun-amazon-data.p.rapidapi.com/product/',
params: {
asin: 'B00FLYWNYQ',
region: 'US'
},
headers: {
'x-rapidapi-host': 'parazun-amazon-data.p.rapidapi.com',
'x-rapidapi-key': '<Your RapidAPI Key>'
}
};
// Step 3
axios.request(options).then(function (response) {
console.log(response.data); }
).catch(function (error) {
console.error(error);
});
Got is another popular and lightweight http client library similar to Axios. It is promises-based with HTTP/2 support and a pagination API.
Install it from command line using npm
:
npm install --save got
Using Got is similar to Axios with slight syntax differences:
// Step 1
var got = require('got');
// Step 2
var headers = {
'x-rapidapi-host': 'parazun-amazon-data.p.rapidapi.com',
'x-rapidapi-key': '<Your RapidAPI Key>'
}
// Step 3
got(
'https://parazun-amazon-data.p.rapidapi.com/product/?asin=B00FLYWNYQ®ion=US',
{
headers:headers,
responseType: 'json'
}
).then(response => {
console.log(response.body);
}).catch(error => {
console.log('Error occurred');
});
SuperAgent is a request package that has been around since 2011. It features both a callback and promised-based API with support for plugins.
Install SuperAgent using npm
:
npm install --save superagent
The following code snippet shows how to use SuperAgent to make a request:
get
function and setting the URL. set
, set query parameters by calling query
and add the callback function to handle the results by calling end
.// Step 1
const superagent = require('superagent');
// Step 2
req = superagent.get('https://parazun-amazon-data.p.rapidapi.com/product/')
// Step 3
req.set('x-rapidapi-host','parazun-amazon-data.p.rapidapi.com')
req.set('x-rapidapi-key','<Your RapidAPI Key>' )
req.query({ asin: 'B00FLYWNYQ', region: 'US' })
req.end((err, res) => {
if (err) {
console.log(err);
}
console.log(res.body);
});
As you can see from the above, we show the basic syntax for using some of the most popular http clients available for Node.js. Hopefully this has been helpful for you to quickly get start making requests to RapidAPI. Be sure to try them all out in the node console.