Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Int too big to fit in my API

Int too big to fit in my API

jacobo
Visitor
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.

Replies 4 (4)

Sufiane
Visitor
2 0 1

I face the same issue....

How can we handle that ?

mikedasilva
Shopify Staff (Retired)
61 7 13

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

To learn more visit the Shopify Help Center or the Community Blog.

Sufiane
Visitor
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

jacomogrelli
Visitor
1 0 0

Hello! I have the same problem on Fastify. Does someone solved this without custom body parsers? Using json-bigint affect all other routes.