Amazon’s Product Advertising API (also known as PA API) allows you to access a lot of data available on Amazon.com as well as their marketplaces in other countries.

The data you can access include products for sale, customer and seller reviews. The API can also be used to find products and offers.

But to be able access this data, you need to send requests that the API can understand. Amazon have published a detailed documentation for it. You can  check it here .

I’ve recently open sourced a client library in Go which can be used to easily access the API.

The client library is available on github at  https://github.com/utekaravinash/gopaapi5

This blog post will help you understand how it can be used to quickly access the API without much efforts.

Installing Client Library (gopaapi5)

Installing packages in Go is straight-forward.

You just have run this command to get the package on your local machine

go get github.com/utekaravinash/gopaapi5

You can now use (import) the gopaapi5 package in your code.

Using gopaapi5 Client Library

Amazon’s Product Adverting API 5.0 lets you access their service using four operations. These operations are as follows:

  1. GetBrowseNodes : This operation can be used to lookup information about browse nodes.
  2. GetItems : This operation can be used to lookup information about products.
  3. GetVariations : This operation can be used to lookup information about a product’s variations.
  4. SearchItems : This operation can be used to search for products on Amazon.

Our client library also exposes the methods for the above operations with same names.

Let’s see how we can use gopaapi5 client library to access these operations.

First of all, we’ll have to initiate our client instance.

But to initiate client instance we need four things in advance. They are:

  1. Access Key
  2. Secret Key
  3. Associate Tag
  4. Locale

You can get the above information once you are registered to  Product Advertising API .

Before that you need to have an access to Amazon Associate account. Here’re the sites to Amazon Associate programs in different countries:

  • Australia : affiliate-program.amazon.com.au
  • Brazil : associados.amazon.com.br
  • Canada : associates.amazon.ca
  • France : partenaires.amazon.fr
  • Germany : partnernet.amazon.de
  • India : affiliate-program.amazon.in
  • Italy : programma-affiliazione.amazon.it
  • Japan : affiliate.amazon.co.jp
  • Mexico : afiliados.amazon.com.mx
  • Singapore : affiliate-program.amazon.sg/
  • Spain : afiliados.amazon.es
  • Turkey : gelirortakligi.amazon.com.tr
  • United Arab Emirates : affiliate-program.amazon.ae
  • United Kingdom : affiliate-program.amazon.co.uk
  • United States : affiliate-program.amazon.com

Create an instance of Client

This is how you should initiate the gopaapi5 client instance once you have the required keys, tag and locale.

client, _ := gopaapi5.NewClient(accessKey, secretKey, associateTag, api.UnitedStates)

In the above code, you are passing access key, secret key, associate tag and a locale for United States to gopaapi5’s NewClient function to get an instance of Client struct.

Client struct exposes four methods viz. GetBrowseNodes, GetItems, GetVariations, and SearchItems. These methods can be used to access respective operations allowed by the API.

In our example, we will try to do a  GetBrowseNodes  operation against the API and print its output on terminal.

So, we will use  client.GetBrowseNodes  method for this operation.

This method accepts  GetBrowseNodesParams  object which holds all information required for this operation to complete successfully.

Construct request parameters

GetBrowseNodesParams  exposes  BrowseNodeIds, Resources  and  LanguagesOfPreference  fields. These fields lets API know what browse node ids, resources and preferred language we expect in the output.

This is how you should construct the  GetBrowseNodesParams  object.

params := api.GetBrowseNodesParams{
    BrowseNodeIds: []string{
        "6960520011",
        "281407",
    },
    Resources: []api.Resource{
        api.BrowseNodesAncestor,
        api.BrowseNodesChildren,
    },
    LanguagesOfPreference: []api.Language{api.EnglishUnitedStates},
}

The  param  variable in the above code holds the parameters we want to pass to the  GetBrowseNodes  operation.

So, we need two browse node ids viz. 6960520011 and 281407. Also, we are want only  BrowseNodesAncestor  and  BrowseNodesChildren  resources from the response with  en_US  as preferred language.

Send Request to PA API

We will now pass a pointer to  param  variable to GetBrowseNodes method exposed by the client as below.

response, err := client.GetBrowseNodes(context.Background(), &params)

We would either get a response or an error from the PA API. Handle the error if any or process the information in the response as per your needs.

You can use  client.SetHttpClient()  method to force gopaapi5 client use your custom HTTP client. Here’s how you can do that:

client.SetHttpClient(&http.Client{Timeout: time.Second * 10})

This code would set a custom HTTP client that would timeout all API requests exceeding 10 seconds.

Conclusion

As you can see, making HTTP request to Amazon Product Advertising API using  gopaapi5  API is extremely easy and can be done in a few minutes.

Let me know if you come across any issue with the library in the comments below or on the github page of the library.

I’ll try my best to fix the issues or your suggestions as early as possible.

Complete example can be found here:  https://github.com/utekaravinash/gopaapi5/blob/master/_examples/get_browse_nodes/main.go