Failing to get product information from Amazon with Perl
Tags: perl, amazon, api, cpan
Published: 2021-10-01 14:22:13

---
title: Failing to get product information from Amazon with Perl
published: true
description: Amazon have switched off the API behind Net::Amazon and XML::Amazon, so it's time to build an alternative
tags: perl, amazon, api, cpan
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wi034r6n6ub612fr4g2c.png
---

I got a bit distracted this week. I was supposed to be writing a book, but as I was feeling a little unwell, I decided to do a little light recreational programming instead.

I'm an Amazon associate, which means if I share the right links I can drive people to the Amazon web site and get a cut of the money when they buy something. I started to muse about the idea of automatically generating pages that contain lots of the right links (books I recommend, films I enjoy, things like that). Ideally, I'd like to start with a list of ASINs (Amazon's unique product identifiers) and a TT template and end with an HTML page. I've done something like this before on the [Line of Succession shop page](https://lineofsuccession.co.uk/shop).

But when I looked at the code that generates that page I realised that all the information about the products was hard-coded in a data file. I was copying the information from Amazon myself. I can't remember why I didn't grab the information from Amazon automatically.

Luckily, I've done this before. Ten years ago, I wrote [a series of articles](https://perlhacks.com/articles/modern-perl-programming/) for *Linux Format* about Modern Perl. And the application I wrote over those articles was a reading tracker. And you added books to the system by giving it an ASIN which the program then used to get the title, author and cover image from Amazon. It did that using [Net::Amazon](https://metacpan.org/pod/Net::Amazon). So I checked out the sample code, fired up the application and tried it out.

Only to find that it didn't work. The Amazon code was broken. Further investigation revealed that the API behind Net::Amazon had been turned off earlier this years and all requests to it returned the HTTP status "410 Gone". I found [a conversation about this](https://github.com/boumenot/p5-Net-Amazon/issues/9) on the GitHub repo for the module, in which it's obvious that the author has little interest in updating the module to support the replacement API - which is unsurprising as it would mean a complete rewrite of the code.

I went off looking for alternatives. And found [XML::Amazon](https://metacpan.org/pod/XML::Amazon). And, even better, there was a release of this module that happened after the API was turned off. But my happiness was short-lived when I discovered that it had exactly the same problem. I wondered how it had a new release if (presumably) all of the tests failed. It turns out that there's an environment variable that needs to be set in order for the live API tests to run. None of the CPAN testers will set that and I guess the author forgot to do that when releasing the new version.

(There's a potential blog post here about how the shrinking Perl community will see an increase of this kind of bit-rot on CPAN in the coming years - but this isn't that post.)

The replacement API for the one these older modules used is called the [Product Advertising API](https://webservices.amazon.com/paapi5/documentation/) and it's completely different to the older version. I guess Amazon have learned a lot about API design in the last few years - given that they seem to release dozens of new ones each month :-)

But I was enthused enough to start work on a replacement. It's called Amazon::API::ProductAdvertising. It's not yet ready for CPAN, but there's a [GitHub repo](https://github.com/davorg/amazon-api-productadvertising). I was slightly scared by the [signing mechanism](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) that Amazon uses on API requests, but it turns out that [Net::Amazon::Signature::V4](https://metacpan.org/pod/Net::Amazon::Signature::V4) exists and makes that really easy.

So it basically works. I just need to work a bit on the parameter passing (there are a **lot** of possible parameters), add the documentation and package it up for CPAN. I'm hoping to get that done over the weekend. This blog post is mostly here to encourage me to do that!