Authentication

How to get your access token, based on OAuth 2.0 grants

All API requests must be authenticated. To get authorized, you must include a valid access token in the Authorization header:

Authorization: Bearer your-access-token

To get a valid access token you need to send a POST request to the following endpoint:

https://auth.commercelayer.io/oauth/token

The payload to be sent with the request differs based on the kind of API credentials you want to use to authenticate and will be detailed case by case in the following sections.

Please note that the authentication endpoint is subject to a rate limit of max 30 reqs / 1 min both in live and test mode.

Authorization grant flows

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 grant type as described in the table below:

Grant type
Sales channel
Integration
Webapp

Client credentials

Password

Authorization code

Refresh token

JWT bearer

Access token expiry

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:

API client
Default lifetime

2 hours

Refresh tokens have a fixed default lifetime of 2 weeks that cannot be modified.

Authorization scopes

For each of the above authorization flows you can restrict the scope to a specific market, store, and/or stock location.

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).

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 environments where corresponding resources would have different IDs.

Putting a market in scope

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.

{
  "grant_type": "{{authorization_grant}}",
  "client_id": "{{your_client_id}}",
  ...,
  "scope": "market:id:xYZkjABcde"
}

When fetching a collection of SKUs 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.

Putting a store in scope

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.

{
  "grant_type": "{{authorization_grant}}",
  "client_id": "{{your_client_id}}",
  ...,
  "scope": "store:id:bGvCXzYgNB"
}

Putting a stock location in scope

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.

{
  "grant_type": "{{authorization_grant}}",
  "client_id": "{{your_client_id}}",
  ...,
  "scope": "market:id:xYZkjABcde stock_location:id:WLgbSXqyoZ"
}

Last updated