Int too big to fit in my API

jacobo
New Member
1 0 1

Int too big to fit in my API

Hello, I am having an issue upon receiving webhooks in my NodeJs API (v12.14 NestJS - Express (*)). IDs in the Integer format are incorrectly parsed. Indeed, since shopify IDs are 64-bit and NodeJS only handles up to 53 bit ints, we cannot properly read them.

Indeed, I am hooked to product creation and when I check `req.body.id`, I get, for instance, the following id 788032119674292900 instead of 788032119674292922.

This poses several issues. I could not properly validate the HMAC by some mysterious reason, and could not reuse the ID when doing calls to Shopify's REST API (ID would not be found). After a lot of debugging, I found that the real ID was, in this example, "788032119674292922". (I stringified the raw buffer of the request body in order to get it).

This can easily get worked around for the HMAC validation (by directly using the raw buffer), but still poses a huge issue when the body is normally parsed and one wants to store the product ID. Indeed, the number is too large for being stored in a 32-bit int / 52-bit number (being normal representation for ints in JS) and precision is then dropped, thus leading to incorrect data received with no error triggered.

I would like to be able to use the body as in a normal API, with no buffer stringifying hacks or number to String casting. Would you have a solution for this ? 

 

----
(*) : This is almost surely related to : https://github.com/expressjs/body-parser/issues/278 , but still there is no real solution for now.

Sufiane
New Member
2 0 1

I face the same issue....

How can we handle that ?

mikedasilva
Shopify Staff
Shopify Staff
25 2 2

Hi @jacobo 

Would it be possible to use a BigInt? 

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

 

Cheers

Mike

mikedasilva | Developer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
Sufiane
New Member
2 0 1

Hello @mikedasilva ,

thanks for your answer, unfortunately we cannot use this with the shopify response.

As you can see in this issue, bigInt are not yet supported in JSON.parse. This prevent us from having precise enough parsing of the shopify response.

Indeed this code:

{ "value" : 9223372036854775807}

 would then be parsed as :

{ value: 9223372036854776000 }

 

There is some libs than can help us avoid this issue like, https://github.com/sidorares/json-bigint, but as you can see there is still some limitations (no round trips in this case)

Yes JS has a new primitive BigInt but its not quite well supported.

Is there another way you'll recommend us to use to face this problem ?

Thanks again,

Sufiane

0 Likes