Identifier for Anonymous Sessions
anonymousId that define to which Anonymous Session they belong. This identifier can be freely chosen, and you could re-use an identifier given to you by an analytics library or a Device ID on a mobile device. However, if you do not use the OAuth access tokens to manage the session for you, you need to make sure that no other session with that identifier already exists.Use Anonymous Sessions with special OAuth tokens
create_anonymous_token:{projectKey} scope. In the OAuth token request, set the scope parameter to the storefront scopes that the session needs. For example, use view_products:{projectKey}, manage_my_orders:{projectKey}, and manage_my_profile:{projectKey}.
Copy or download the client credentials, they cannot be revealed after creation for security reasons.
Create a token with a new Anonymous Session
Use the OAuth client to request an anonymous-session token. In the first example, the API generates the session identifier for you.
$curl https://auth.{region}.commercetools.com/oauth/{projectKey}/anonymous/token --basic --user "{client_id}:{client_secret}" -X POST -d "grant_type=client_credentials&scope=view_products:{projectKey} manage_my_orders:{projectKey} manage_my_profile:{projectKey}"
phone-123. If this identifier is already in use, the request will fail. $curl https://auth.{region}.commercetools.com/oauth/{projectKey}/anonymous/token --basic --user "{client_id}:{client_secret}" -X POST -d "grant_type=client_credentials&scope=view_products:{projectKey} manage_my_orders:{projectKey} manage_my_profile:{projectKey}&anonymous_id=phone-123"
access_token:{
"access_token": "vkFuQ6oTwj8_Ye4eiRSsqMeqLYNeQRJi",
"token_type": "Bearer",
"expires_in": 172800,
"refresh_token": "{projectKey}:OWStLG0eaeVs7Yx3-mHcn8iAZohBohCiJSDdK1UCJ9U",
"scope": "view_products:{projectKey} manage_my_orders:{projectKey} manage_my_profile:{projectKey}"
}
You can then use this token with the My endpoints and other allowed scopes, for example to query Product Projections:
$curl -sH "Authorization: Bearer vkFuQ6oTwj8_Ye4eiRSsqMeqLYNeQRJi" https://api.{region}.commercetools.com/{projectKey}/product-projections?staged=true&limit=10
Best practice: Create Anonymous Sessions only once necessary
Use the token to create a cart and order
POST /{projectKey}/me/carts with:{
"currency": "USD",
"country": "US"
}
anonymousId automatically set, for example to phone-123.POST /{projectKey}/me/carts/<cart-id> with:{
"version": <cart-version>,
"actions": [{
"action": "addLineItem",
"productId": "<product-id>",
"variantId": 1,
"quantity": 1
}]
}
anonymousId will also be phone-123.Keep the refresh token
With the refresh token, you can get new access tokens for the Anonymous Session. It can live for a long time, and contain as many carts and orders as you need. However, if the refresh token is lost, there's no other way to claim this Anonymous Session.
Depending on your platform, you can save the refresh token for later usage. For example, a browser app may save it in local storage, or an iOS app in the keychain.
End an Anonymous Session by assigning it to a Customer
anonymousId is still set. After the session ends, the access token and refresh token are immediately invalid.POST /{projectKey}/me/signup with:{
"firstName": "Alice",
"lastName": "Doe",
"email": "alice@example.com",
"password": "secret"
}
Now we need to get new access and refresh tokens with the password flow:
$curl https://auth.{region}.commercetools.com/oauth/{projectKey}/customers/token --basic --user "{client_id}:{client_secret}" -X POST -d "grant_type=password&username=alice@example.com&password=secret&scope=view_products:{projectKey} manage_my_orders:{projectKey} manage_my_profile:{projectKey}"
GET /{projectKey}/me/carts with the new access token returns the same Carts, but we'll notice that the customerId is the one of the newly created account. The anonymousId is still set to phone-123. If we create another Cart, it has the customerId set, but not the anonymousId.Use Anonymous Sessions with regular OAuth tokens
anonymousId yourself. You can assign it to a Cart during creation:POST /{projectKey}/carts with:{
"currency": "USD",
"country": "US",
"anonymousId": "foobar"
}
anonymousId will also be set on the new order.anonymousId = "foobar":GET /{projectKey}/carts?where=anonymousId%20%3D%20%22foobar%22anonymousId in the customer flow you implement. This lets the platform associate the guest resources correctly. For the exact behavior by sign-in and sign-up method, see Cart merge during sign-in and sign-up.anonymousId in the request body:POST /{projectKey}/customers with:{
"firstName": "Alice",
"lastName": "Doe",
"email": "alice@example.com",
"password": "secret",
"anonymousId": "foobar"
}
This lets the platform associate the guest Cart and other guest resources with the new Customer account.
Conclusion
anonymousId lifecycle yourself.