Documentation
Feedback
Guides

Headless authentication

Building a headless ecommerce means setting up communication between your frontend and VTEX's REST APIs. Each HTTP request sent from your frontend to VTEX must be authenticated with a user token. The implementation shown below describes how to obtain this token in your application.

This feature is currently being tested by a limited number of VTEX accounts. Please do not share this documentation with people outside of your operation.

Implementation

To ensure a secure shopping experience, shopper authentication for headless VTEX stores must follow a specific flow, as presented in the following:

{"base64":"  ","img":{"width":1530,"height":779,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":154180,"url":"https://cdn.jsdelivr.net/gh/vtexdocs/dev-portal-content@main/docs/guides/Integration-Guides/headless-commerce/headless-authentication_1.png"}}

  1. The shopper authenticates themselves in your frontend via an OAuth integration.
  2. Your frontend exchanges the OAuth identity provider access token for a VTEX user token.
  3. Now, with the user token previously obtained, your frontend application can make API requests to VTEX and access all the necessary information to perform actions on behalf of the shopper.

Below you can learn more details on each of these steps.

When building a headless store, you are opting to use a frontend application different from VTEX's native frontend. Hence, you must adhere to the guidelines provided below and be aware of your application's responsibilities in order to ensure a smooth and secure shopping experience for your customers.

OAuth log in

Shopper authentication in your headless store must happen through an external identity provider. To do this, you must set up a custom OAuth login integration.

Headless authentication scenarios require using a custom OAuth login integration. The native social authentication settings described in the Configuring login with Facebook and Google guide don't apply to headless authentication.

After you have configured your OAuth connection in the VTEX Admin panel, you can implement the OAuth login on your frontend, according to your store's needs and responsibilities.

Make sure that the OAuth access token has scope email (or another value associated with scope email).

Exchanging OAuth identity provider access token for VTEX user token

If you have set up your OAuth integration and implemented login on your frontend, users may be able to authenticate themselves. However, this alone is not sufficient for your frontend to communicate with VTEX REST APIs.

To enable this communication, your frontend application must exchange the access token from the OAuth login for a VTEX user token. This can be achieved through the POST Exchange OAuth access token for VTEX credential endpoint.

Check out request and response body examples below and refer to the POST Exchange OAuth access token for VTEX credential API reference for more details on each field.

Request body example


_10
{
_10
"providerId": "custom-oauth-provider",
_10
"accessToken": "dsfDShdgfhDFI1NiIsIrtyZCI6IjFBRjI5MUUwRDY0MERENTlEQTkzRTg0REMxNjQyNjA3ODZEQjY3ODAiLCJ0eXAiOiJqd3QifQ.eyJzdWIiOiJ2dGV4YXBwa2V5LXZ0ZXhoZWxwLVdWQ0FCVCIsImFjY291bdg465DATU4GVscCIsImF1MBllbmNlIjoiYWRtaW4iLCJleHAiOjE2Njk3NzA3MzcsInVzZXJJZCI6IjM5MjNhMmUy5khmMTctNGNiYy04YzU3LWQ3OGFkNmUxYTU2NiIsImlhdCI6MTY2OTc0OTEzNywiaXNzIjoidG9rZW4tZW1HgoRlciIsImp0aSI6IjNiNjAxODA2LTExMzEtNDcwYS05MWJjLTVhM2JhOThiYWQyNiJ9.Q7N8MFa1FMJsQUpxBY29oije4aa-654fgjLFLl6LUY3Wei3MRUVUMRQWkey6Kug8iFPonZ1L-PaFmwfzSz3TCQ",
_10
"duration": 90
_10
}

Response body example


_10
{
_10
"authToken": "eyJhbGciOiJFUzI1NiIsIrtyZCI6IjFBRjI5MUUwRDY0MERENTlEQTkzRTg0REMxNjQyNjA3ODZEQjY3ODAiLCJ0eXAiOiJqd3QifQ.eyJzdWIiOiJ2dGV4YXBwa2V5LXZ0ZXhoZWxwLVdWQ0FCVCIsImFjY291bnQiOiJwerV4aGVscCIsImF1MBllbmNlIjoiYWRtaW4iLCJleHAiOjE2Njk3NzA3MzcsInVzZXJJZCI6IjM5MjNhMmUy5khmMTctNGNiYy04YzU3LWQ3OGFkNmUxYTU2NiIsImlhdCI6MTY2OTc0OTEzNywiaXNzIjoidG9rZW4tZW1HgoRlciIsImp0aSI6IjNiNjAxODA2LTExMzEtNDcwYS05MWJjLTVhM2JhOThiYWQyNiJ9.Q7N8MFa1FMJsQUpxBY29oije4aa-Jf463lwgLFLl6LUY3Wei3MRUVUMRQWkey6Kug8iFPonZ1L-PaFmwfzSz3TCQ"
_10
}

Making requests to VTEX APIs

Once with a valid VTEX user token, your frontend application can leverage VTEX APIs to manage various commerce capabilities of your store, including product information, shopping cart management, and checkout.

Learn more about machine authentication in VTEX and how to use user tokens to authenticate your requests.

Authentication responsibilities of headless applications

Since your store is not using VTEX's native frontend to authenticate shoppers, there are some capabilities that your application must be prepared to handle:

  • Managing login flow with the external identity provider.
  • Making sure that the OAuth access token has scope email (or another value associated with scope email).
  • Keeping the OAuth access token in the contexts where it is relevant.
  • Refreshing tokens and other information when necessary.
  • Keeping track of the expiration time set for the VTEX user token.
  • Requiring new login if it was somehow revoked.
  • Repeating the token exchange in case the shopper had to log in again.

Learn more

See these other guides to learn more about building a headless shopping experience using VTEX:

Contributors
2
Photo of the contributor
Photo of the contributor
Was this helpful?
Yes
No
Suggest Edits (GitHub)
Contributors
2
Photo of the contributor
Photo of the contributor
On this page