How to use Amazon Product Advertising API in Hugo?

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

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 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!

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:

… 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.

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

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.

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.

1 Like

Yes, that sounds about right.

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?

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.

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, not yourapi.com”).

Hopefully this can help someone

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.

1 Like