Cart Token behavior in AJAX API in new browser session

I encountered an interesting behavior in the Cart AJAX API. When i am in a fresh browser session, every GET request on cart.js returns a different cart token in the response. After adding an item to the cart, the token is “stable” and every GET returns the same cart token.

Also, the format of the cart token is different. At first its some random alphanumeric string (e.g. “65bbe6ae705b0f50ee04f5456d03265a”) and after an item was added to the cart, it always starts with “Z2N…” with some key at the end (like the example in the documentation: Cart API reference)

I would like to know whether this is intended behavior or not? I’m using the cart token to associate some data we upload in our App before an item is added to the cart but ran into the issue that the cart token on a fresh session was different to the one I got from the orders create webhook.

I believe it is.

I think the cart token might be some sort of hash that changes based on the contents of the cart and/or other attributes about the session.

Can you tell us more about your need to tie a cart action to an order? Maybe you can leave a cart attribute UUID before checkout so you can associate the action with the order?

Hi Dylan,

thanks for the suggestion, it fits my use case really well. I now generate UUID on the client side and in the put it in the cart attributes (if this attribute isn’t in the cart yet). In the order I read it from note_attributes. Works fine!

1 Like

Awesome! Glad to hear that works for you :slight_smile:

I too hope there’s some UUID managed by Shopify in the future. It would help us developers have a standard to build against, as well as keep order / cart metadata a bit less cluttered from UUIDs from other apps all for the same purpose of tracking a pre-order action to an order.