Authentication
How to get your access token, based on OAuth 2.0 grants
Last updated
How to get your access token, based on OAuth 2.0 grants
Last updated
All API requests must be authenticated. To get authorized, you must include a valid access token in the Authorization header:
To get a valid access token you need to send a POST
request to the following endpoint:
The payload to be sent with the request differs based on the kind of you want to use to authenticate and will be detailed case by case in the following sections.
Check our , a TypeScript library wrapper that helps you use Commerce Layer API for authentication, providing also some useful helper methods (e.g. and ).
To get an access token, you need to execute an authorization flow by using valid API credentials for the client.
The authorization flow depends on the as described in the table below:
Client credentials
Password
Authorization code
Refresh token
JWT bearer
As a general rule, non-confidential (public) API credentials (e.g. sales channels) can authenticate (requesting and revoking access tokens, using refresh tokens) using just a client ID, without providing a client secret and can be safely used client-side. Confidential (private) API credentials (e.g. integrations, webapps) need a client secret to authenticate.
For security reasons, access tokens expire after a default period of time. Your access token lifetime differs based on the kind of API credentials you're using:
4 hours
2 hours
2 hours
To avoid security issues, be careful not to set too long expiration dates for your access tokens.
The access token scope is a string that can be composed in two ways:
by ID — {{resource_name}}:id:{{resource_id}}
Where {{resource_name}}
can be one of market
, store
, or stock_location
, and {{resource_id}}
is the id
of the resource (e.g. market:id:xYZkjABcde
, store:id:bGvCXzYgNB
, stock_location:id:WLgbSXqyoZ
).\
by code — {{resource_name}}:code:{{resource_code}}
Where {{resource_name}}
can be one of market
, store
, or stock_location
, and {{resource_code}}
is the code
of the resource (e.g. market:code:europe
, store:code:outlet_ny
, stock_location:code:eu_warehouse
).
By including a market scope in the access token request — market:id:xYZkjABcde
— all the resources (e.g. SKUs, prices, stock items) you fetch are automatically filtered.
By including a store scope in the access token request — store:id:bGvCXzYgNB
— all the resources (e.g. SKUs, prices) you fetch are automatically filtered by the market the store is associated with, orders are associated with the store in scope, and the returned available payment methods are the store's ones (if any, otherwise the associated market ones). When calculating the stock availability, the associated market stock locations hierarchy is extended by adding the store's stock location (if any) as the one with the highest priority and increasing by 1 the inventory model's stock locations cutoff value.
When putting a store in scope, there's no need to specify the associated market, which is inherited from the store itself. Only one store in scope is allowed, if you put multiple store in scope the API responds with a 400 Bad Request
error code.
By including a stock location scope in the access token request — stock_location:id:WLgbSXqyoZ
— the stock is restricted to the SKUs available in that specific stock location.
When putting a stock location in scope, adding the associated market in the access token request is mandatory. If the market scope is missing or the stock location doesn't belong to the market in scope the API responds with a 400 Bad Request
error code.
You can specify a custom lifetime for the token at the API credentials level on the admin Dashboard when you create/update each of them or set it via our . The token lifetime value must be expressed in seconds and fall within a min of 2 hours (7200 secs) and a max of 15 days (1296000 secs).
A new token is issued 15 mins (900 secs) before the previous token's expiration date. During that overlap time window, both the new and the old token are valid, thus helping you and avoid asking for a new token every time you make a call.
have a fixed default lifetime of 2 weeks that cannot be modified.
For each of the above authorization flows you can restrict the scope to a specific , , and/or .
Defining a market, store, or stock location code and using it for your scope(s) can come in handy to replicate your setup when switching from Test to Live mode which are two different, separate where corresponding resources would have different IDs.
The market in scope must be active (i.e. enabled). are excluded from any authorization scope recalculation.
When with a market in scope only the SKUs that are sellable in that market are returned. To be sellable in a market an SKU must have a price in the market's price list and at least one stock item in one of the market's stock locations, regardless of its quantity.
Sales channels require a market in scope when requesting their access tokens to perform the . If the market in scope is associated with a customer group, it becomes private and can be accessed only by the customers belonging to the group — in that case, to get your token you must use the .