# How to use Amazon Product Advertising API in Hugo?

**URL:** <https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240>\
**Category:** support\
**Created:** [June 2, 2018, 11:55pm UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240 "2018-06-02T23:55:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [June 2, 2018, 11:55pm UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/1 "2018-06-02T23:55:28Z")

</div>

I’m trying to think of how to employ the Amazon Product Advertising API in Hugo. The sign documentation for that API is here: [https://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP\_GettingStarted.html](https://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_GettingStarted.html)

It of course requires an API key and credentials to sign the request. I’m still new to web dev stuff, and mainly I’ve been interested in just using Hugo, but it seems to me that to make the API call work, a different workflow would be needed? Like, when Hugo is building the site, it doesn’t pull data from the internet, so how can the API work (without publishing the API key and credentials in the html source) with Hugo?

Does it require other processing before and after the Hugo build process?

Jekyll has a plugin: [https://github.com/scottcwilson/ort](https://github.com/scottcwilson/ort) that seems to do this but I know nothing about Jekyll’s processing, whether it allows the api calls to happen during the build or there are other pre-processors in the mix.

Does anyone know how to go about this? Thank you!

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 3, 2018, 12:26am UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/2 "2018-06-03T00:26:38Z")

</div>

I know nothing about that API so take this as general feedback.

Looks like that plugin goes off and gets each ASIN you reference in markdown files, during the build. I don’t know of a way you can do that kind of on-the-fly triggering with hugo. Looks like he chose jekyll because the code he needed to pull the data was already in a ruby gem, so he could easily integrate it with the rest of the jekyll build system.

With hugo you _can_ call external json, like:

> **[Data Templates](https://gohugo.io/templates/data-templates/#call-the-functions-with-a-url)**
>
> In addition to Hugo's built-in variables, you can specify your own custom data in templates or shortcodes that pull from both local and dynamic sources.

… but that assumes the json is just sitting there, available for use (locally or at an URL). Often, json available from a URL would be behind some kind of authentication so, what I am usually doing is pulling whatever json I need in advance, in a script, before doing the hugo build.

For instance, say there was a list of books with details that you could pull via whatever language, and the API reference shows me some examples. I find out I can pull with a simple curl command, so I write a little shell script to go grab the json I’m looking for using `curl` with an auth key, and put the resulting json in my `/data` folder. Then I can refer to that json when I build hugo (either with `hugo` or `hugo server`), in my go templates.

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 3, 2018, 12:30am UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/3 "2018-06-03T00:30:06Z")

</div>

Oh, ok. It returns in XML (should have known, it’s amazon). You can pull it with curl.

> **[Using curl with the Amazon Product Request API](https://frdmtoplay.com/using-curl-with-the-amazon-product-request-api/)**
>
> How to use curl to create and sign an Amazon Product Advertising API Request from a bash prompt.

Then you’d need to convert the xml to json using whatever utility. If it were me, I’d go for something easily portable like a golang app.

> **[basgys/goxml2json](https://github.com/basgys/goxml2json)**
>
> XML to JSON converter written in Go (no schema, no structs) - basgys/goxml2json

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [June 3, 2018, 1:49am UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/5 "2018-06-03T01:49:54Z")

</div>

Hey, thanks so much for looking into this!

I thought that might be the solution. I was hoping that getJSON was something magical in Hugo so you could use it with an API key and whatever other credentials. That would basically solve the problem… and a bunch of other problems too i bet.

But if i understand, i’ll have to have a pre-process pull the ASINs i want from somewhere, go get the xml from the amazon API, convert it to json and save that as a data file in the /data/ folder. then have a template with a loop to build the html with the various data files when the hugo command runs.

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 3, 2018, 2:07am UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/6 "2018-06-03T02:07:09Z")

</div>

> [@gaetawoo](#):
>
> But if i understand, i’ll have to have a pre-process pull the ASINs i want from somewhere, go get the xml from the amazon API, convert it to json and save that as a data file in the /data/ folder. then have a template with a loop to build the html with the various data files when the hugo command runs.

Yes, that sounds about right.

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [June 3, 2018, 2:45am UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/7 "2018-06-03T02:45:10Z")

</div>

IFF the amazon API would return JSON, (theoretically) couldn’t I use the `getJSON` command along with the `getENV` command to get the API key and public and private credentials and use that to get all the item data? Like in a shortcode? Of course, if it returns xml then it’ll crash and would therefore require what you explained in the previous comment ya?

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 3, 2018, 3:22am UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/8 "2018-06-03T03:22:20Z")

</div>

If the API was such that, you could retrieve everything you need in one http GET, by including an access token, then yes, it would work. And yes, maybe you could hide the sensitive bits by setting them into environment vars and calling them with getENV.

---

<div class="post-metadata">

**Author:** ![robertmain53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/robertmain53/32/6974_2.png) [@robertmain53](https://discourse.gohugo.io/u/robertmain53)\
**Post date:** [September 24, 2018, 2:34pm UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/9 "2018-09-24T14:34:19Z")

</div>

Hi,

Did you come up with a solution?

I’m looking for the same thing. I’m not a developer, but i found a php script that returns the APi data in Json:

https ://tmsanrinsha.net/amp/post/2017/05/hugo-shortcode-amazon/  
https ://github.com/tmsanrinsha/amazon-json-api/blob/master/README.md

The code is able to generate json and it’s supposed to integrate with Hugo, but i still cannot manage to get the integration to work.

When trying to run hugo or hugo server it returns a error processing shortcode “shortcodes/amazon.html” for page “/index.md”: template: shortcodes/amazon.html:3:12: executing “shortcodes/amazon.html” at \<getJSON “https ://you…\>: error calling getJSON: Get https ://yourapi.com/?itemid=B07FTFL1NR: x509: certificate is valid for [cc.sedoparking.com](http://cc.sedoparking.com), not [yourapi.com](http://yourapi.com)”).

Hopefully this can help someone

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [September 24, 2018, 7:32pm UTC](https://discourse.gohugo.io/t/how-to-use-amazon-product-advertising-api-in-hugo/12240/10 "2018-09-24T19:32:50Z")

</div>

I dont’ have a solution but I also don’t yet have access to the amazon API to try it out. I just know that for Hugo, at this point, you need to have the json files already existing for hugo to build from them. Hugo basically doesn’t touch the Amazon api at all, so you have to populate those json files through some external means, however it is.
