How to use Amazon Product Advertising API V5.0 in Nodejs.

By signing up to become a Product Advertising API developer, you join the tens of thousands of developers who are already realizing financial gains by creating Product Advertising API-driven applications and web stores

Ariful Haque
4 min readOct 27, 2019

Amazon recently published its Amazon Product Advertising API v5.0 for Amazon Affiliate Program. From 31st October 2019, previous version 4.0 of API will be taken down and only v5.0 will be supported to access Amazon products programmatically.

For this Amazon PA-API 5.0, Amazon has released an SDK for PHP, NodeJs, Python and, Java.

Amazon PA-API 5.0 SDK for NodeJS is not available in NPM and requires a lot of configuration. So, I build an NPM package amazon-pa-api50 to simplify the use of this SDK.

In this article, I’ll show how to use amazon-pa-api50 npm package for Nodejs or Javascript-based applications.

Prerequisite

To use Amazon Product Advertising API 5.0,

You must have an Amazon Associate Account that has been reviewed and received final acceptance of the Amazon Associate Program.

If you are an Amazon Associate Account holder but did not receive your API Access Key and Secret Key, look at this guideline from Amazon.

Without these Access Key and Secret Key From the Amazon associate program, you can not access the Amazon PA API 5.0

Installation

Github

git clone git@github.com:arifulhb/amazon-pa-api50.git

NPM

npm install amazon-pa-api50 — save

Connect with Amazon Product Affiliate API.

Import the Config module and API client.

const Api = require(‘amazon-pa-api50’)const Config = require(‘amazon-pa-api50/lib/config’)

Optional loading of modules

All of these are optional. It depends on what you want to search for or fetch.

// Optional for different resources
const resources = require(‘amazon-pa-api50/lib/options’).Resources
// for product condition
const condition = require(‘amazon-pa-api50/lib/options’).Condition
// Optional for different country
const country = require(‘amazon-pa-api50/lib/options’).Country
// for Search Index
const searchIndex = require(‘amazon-pa api50/lib/options’)
.SearchIndex

Create Config instance

// By Default all resources and will connect to United States
let myConfig = new Config();
// if you want other country then united states.
let myConfig = new Config(undefined, country.UnitedKingdom);
// if you want custom resources. this custom `resources.Offers` will be set default to all of your search. You can customize the resources in your search specific function also.
let myConfig = new Config(resources.Offers);
// if you want custom resources with different country
let myConfig = new Config(resources.ItemInfo, country.Canada);

Configure the Config class

After adding the credentials in `myConfig`, Create `Api` instance

myConfig.accessKey = '<ACCESS_KEY>'
myConfig.secretKey = '<SECRET_KEY>'
myConfig.partnerTag = '<PARTNER_TAG>'

const api = new Api(myConfig)

Sample code to get a Product Item info from Amazon

If all of your credentials are valid, you must be able to search in Amazon now.

let resourceList = resources.getItemInfo
resourceList = resourceList
.concat(resources.getImagesPrimary)

api.getItemById(['B079JD7F7G'], {
parameters: resourceList,
condition: condition.Any
}).then((response) => {
console.log(' ===== find by Item ids =====')
console.log('data', response.data)
}).catch((error) => {
console.log('Error: ', error)
})

Usage

api have four main functions to fetch data from amazon. Each function has 2 parameters and return `promise`.

  • api.getItemById([‘asin1’, ‘asin2’], {})
  • api.search(‘keyword’, {})
  • api.getVariations(‘asin1’, {})
  • api.getBrowseNodes([‘nodeid1’, ‘nodeid2’], {})

Inside then, block of the promise, response object contains response.data and response.response as full response from Amazon.

Sample source code for how to use all the functions are given in this Demo file of Github Repo amazon-pa-api50.

What's coming

The readme.md file covers all the options, library and how to use. The Documentation is still under development and hopes to publish by mid-October.

I’ll add more example documents to how to search different types of categories like Books, Movies, or Digital Products, etc.

Conclusion

As an Open source contributor, I’d like my code to be reviewed. Star the repo, fork the repo, If you find any issue, create a new Issue and make your PR with features or values you want to add.

--

--

Ariful Haque

Passionate Software Engineer | Founder of LaptopList.com | Enthusiast Photographer