17

I have a 10 digit string being passed to me, and I want to verify that it is a valid ASIN before doing more processing and/or redirection.

I know that a non ISBN ASIN will always be non-numeric and 10 characters in length

I just want to be able to tell if the item being passed is a valid ASIN or is it just a search string after I have already eliminated that it could be a ISBN.

For example "SOUNDBOARD" is a search term while "B000J5XS3C" is an ASIN and "1412775884" is an ISBN.

Is there a lightweight way to check ASIN?

JDB
  • 25,172
  • 5
  • 72
  • 123
RAD Moose
  • 177
  • 1
  • 1
  • 7
  • What are the conditions to be satisfied for a string to be a valid ASIN? 10 chars long, should contain digits (how many - min/max - at what positions), no spaces and...? – Amarghosh Jan 23 '10 at 12:35
  • ASIN numbers are 10 characters long. That is the only set requirement that Amazon has released. Typically they can also be 10 digit ISBN numbers (but that is easy as it is all numeric.) They USUALLY start with B but not always, and they have a mix of letters and numbers, no spaces, so there isn't a "ASIN RegEx" method that would distinguish between a possible search term and ASIN. – RAD Moose Jan 23 '10 at 13:08
  • One note, in all cases that I could find so far, the first character of a valid ASIN (that wasn't also an ISBN) was always a B. However, according to Amazon, that is not guaranteed. – RAD Moose Jan 24 '10 at 04:31
  • I went back to using the AWS Product Advertising API for now. – RAD Moose Jan 24 '10 at 04:37
  • 1
    I used information from an article titled "Amazon® AWS HMAC signed request using PHP" by Ulrich Mierendorff in conjunction with the Amazon AWS Product Advertising API. http://mierendo.com/software/aws_signed_query/ – RAD Moose Jan 24 '10 at 04:42

4 Answers4

37

Update, 2017

@Leonid commented that he’s found the ASIN BT00LLINKI.

Although ASIN’s don’t seem to be strictly incremental, the oldest non-ISBN ASINs do tend to have more zeros than newer ASINs. Perhaps it was inevitable that we’d start seeing ASINs with no zero padding (and then what, I wonder...). So we’re now looking for "B" followed by nine alphanumeric characters (or an ISBN) — unfortunately, the "loss" of that zero makes it a lot easier to get a false positive.

/^(B[\dA-Z]{9}|\d{9}(X|\d))$/

Original answer

In Javascript, I use the following regexp to determine whether a string is or includes what’s plausibly an ASIN:

/^\s*(B\d{2}[A-Z\d]{7}|\d{9}[X\d])\s*$/

or, without worrying about extra whitespace or capturing:

/^(B\d{2}[A-Z\d]{7}|\d{9}[X\d])$/

As others have mentioned, Amazon hasn't really revealed the spec. In practice I've only seen two possible formats for ASINs, though:

  1. 10-digit ISBNs, which are 9 digits + a final character which may be a digit or "X".
  2. The letter B followed by two digits followed by seven ASCII-range alphanumeric characters (with alpha chars being uppercase).

If anyone has encountered an ASIN that doesn't fit that pattern, chime in. It may actually be possible to get more restrictive than this, but I'm not certain. Non-ISBN ASINs might only use a subset of alphabetic characters, but even if so, they do use most of them. Some seem to appear more frequently than others, at least (K, Z, Q, W...)

Semicolon
  • 6,793
  • 2
  • 30
  • 38
  • Good thing to note it is indeed a "plausible" ASIN :-). – Styxxy Oct 10 '12 at 23:16
  • Indeed, @Styxxy -- I think that's really what the question meant. Of course, there's no way to know whether the possible ASIN is in use without asking Amazon, but you can at least know that it's worth checking in the first place. If the Product Advertising or MWS Product APIs are too heavy for one's needs, then Flurin's answer remains solid. DSA's concerns are fortunately something most people can ignore. Without going into tons of detail, you actually *can* assume that one ASIN never points to two completely different products. Exceptions exist, but are quite rare (and are errors). – Semicolon Oct 11 '12 at 01:39
  • (Though, to be clear, the existence of an ASIN on one marketplace says nothing of whether it exists on another.) – Semicolon Oct 11 '12 at 01:43
  • 2
    For anyone who has recently come across this answer, [B0000000BS](http://amzn.com/B0000000BS) and [B00000BIES](http://amzn.com/B00000BIES) are currently valid ASIN's that link to real products... These do not fit the above pattern. Seems like a lot has changed in two years! – iglvzx Oct 20 '14 at 23:11
  • 1
    Humor aside, both of those examples do fit the above pattern -- (/^B\d{2}\w{7}|\d{9}(X|\d)$/i).test('B00000BIES') // true – Semicolon Oct 20 '14 at 23:37
  • "If anyone has encountered an ASIN that doesn't fit that pattern, chime in" - here: BT00LLINKI is a valid ASIN. https://www.amazon.com/Electrical-Machines-Diagnosis-Jean-Claude-Trigeassou-ebook/dp/BT00LLINKI/ref=sr_1_1?ie=UTF8&qid=1485240984&sr=8-1&keywords=BT00LLINKI – Leonid Jan 24 '17 at 07:00
  • Whoa, interesting @Leonid. Updating answer. – Semicolon Jan 24 '17 at 19:04
  • @Semicolon I saw the update. Note that there are still two zeroes in BT00LLINKI My ASIN checking logic is still kind of strict, but it is a losing battle. – Leonid Jan 24 '17 at 20:40
  • 1
    I can get large amounts of random, comma delimited, ASINS. If anyone would would like a list, shoot me a msg. (I haven't ran a mass check yet, but I will soon) :) – Ben Slayton Mar 06 '17 at 23:46
  • Another counter-example: I encountered this ASINs, which look like ISBN-10, but are not. You can check by going to the Amazon site and finding the corresponding ISBN. `ASIN = 059035342X; ISBN-10 = 0439708184`. At this point my checker for asin is: 1. exactly 10 symbols long, 2. Is alphanumeric, 3. `asin == upper(asin)`. That is the best I can do at the moment. – Leonid Jun 27 '17 at 04:31
  • You’re right, there’s very little to go on these days, but I don’t think the example there is contrary to the usual pattern — 059035342X is indeed an ISBN for that book. The oddity appears to just be that the ISBN-ASIN and the actual ISBN property for that specific product listing don’t match each other. So I still have yet to see an ASIN which is not either an ISBN or begins with "B", though I am not exactly gonna stake my life on it remaining true forever :) – Semicolon Jun 28 '17 at 03:24
  • re: @Simon_Weaver edit "he got ahead of himself when he found out some ASINS could be B followed by two digits and deleted the case where it was three. eg. B003H3G470" — no, I don’t think that’s true. My wording ("two digits followed by seven alphanumeric characters") is _inclusive_ of "two or three digits followed by six or seven alphanumeric characters". The "or" doesn’t add any information, does it? (In any case, it’s no longer true.) – Semicolon Sep 22 '17 at 04:56
  • Here is a post describing how ASIN are generated and assigned : https://inventlikeanowner.com/blog/the-story-behind-asins-amazon-standard-identification-numbers/ – dkam Nov 04 '21 at 02:55
  • Thank you for updating the regex pattern! Even the guy on their github page doesn't know this – Gokigooooks Sep 25 '22 at 16:47
4

For PHP, there is a valid regular expression for ASINs here.

function isAsin($string){
    $ptn = "/B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(X|0-9])/";
    return preg_match($ptn, $string, $matches) === 1;
}
Sebastian Viereck
  • 5,455
  • 53
  • 53
0

maybe you could check on the amazon site whether the ASIN exists.

http://www.amazon.com/dp/YOUR10DIGITASIN

this URL return a http-statuscode=200 when the product exists and a 404 if that was not a valid ASIN.

  • 1
    Yeah, I was thinking about that but wasn't sure if Amazon would want me to hit the site that way and generate possibly thousands of 404 errors. Guess I should ask them =) – RAD Moose Jan 23 '10 at 12:59
  • This worked, except note that some returned a 301 response. However, this seems to be outside of the TOS for Amazon's site. =/ – RAD Moose Jan 24 '10 at 04:32
  • it also doesn't work if the ASIN is a seller specific ASIN - they call this an FNSKU and it's primarily used as a barcode for a seller owned item in Amazon inventory – Simon_Weaver May 09 '15 at 00:07
0

After trying couple of solutions (including the top voted answer) they did not work well in PHP. (ex. 8619203011 is shown as ASIN)

Here is the solution that works very well:

function isAsin($string){
    $ptn = "/^(?i)(B0|BT)[0-9A-Z]{8}$/";
    if (preg_match($ptn, $string, $matches)) {
        return true;
    }
}
$testAsins = array('k023l5bix8', 'bb03l5bix8', 'b143l5bix8', 'bt00plinki', '         ', '');
foreach ($testAsins as $testAsin) {
    if(isAsin($testAsin)){
        echo $testAsin." is ASIN"."<br>";
    } else {
        echo $testAsin." is NOT ASIN"."<br>";
    }
}

Explanation:

/^(?i)(B0|BT)[0-9A-Z]{8}$/

/^ = Beginning

(?i) = Case in-sensitive

(B0|BT)= Starting with B0 or BT

[0-9A-Z]= any numbers or letters

{8} = 8 numbers or letters allowed (on top of +2 from B0 or BT)

Community
  • 1
  • 1
Tarik
  • 4,270
  • 38
  • 35