What is a Cart Key?

A cart key is what identifies the cart stored in session along with it’s cart data and expiration. The cart key by default, will be a random set of letters and numbers or if the Cart API request is authenticated, it will relate to the user ID of the authenticator instead.
If you try to identify a registered user via the cart key then you will be asked to authenticate as that user instead.

Finding the Cart Key

You can find the cart key returned in the cart response called cart_key and via the returned headers called CoCart-API-Cart-Key once the first API request has been made. Once you have the cart key, you can then use it to set the cart_key as a global parameter with any of the Cart API routes to load that cart. Without the cart key set for other Cart API routes, any requests made will not be applied to the appropriate cart session. This is partially important when adding, updating or removing items to and from the cart as such. The same for coupons, fees and other cart related requests made.
The cart_key parameter is a global parameter so it must be queried rather than being added as part of the body data you send with the API request you make. Otherwise it will not update the appropriate cart. Should you fail to do so correctly will lead to adding items stored in their own individual cart session which you will not be able to recover from previous requests.

Creating a Cart Key Manually

You can create your own cart key but, it cannot be longer than 42 characters as that is the limit for storing the key in the database.

HTTP request

/wp-json/cocart/v2/cart?cart_key=mycartkey123
If you do create your own cart key, it is best that you do so when adding the first item to the cart in order to update the correct cart from the start.