Menu
Guides

VTEX OAuth provider

Learn how to use the VTEX OAuth provider so AI agents, MCP clients, and partner apps can act on behalf of a VTEX user with the standard OAuth authorization code flow.

8 min read

The VTEX OAuth provider allows external clients, such as AI agents, MCP clients, and partner apps, to request a VTEX user's permission and act on their behalf. It follows the OAuth 2.1 authorization code flow with Proof Key for Code Exchange (PKCE), so you can use the OAuth libraries you already know instead of building a custom integration.

OAuth is an open standard that allows an app to access a user's data on another service without receiving the user's password. In this guide, the client is the app that wants to access a VTEX user's data, and the VTEX OAuth provider verifies the user, asks for their permission, and issues the tokens.

This guide is for apps that access VTEX on behalf of a user. If you want to allow store customers to log in with an external identity provider, see Webstore login with an external OAuth 2.0 provider.

With the VTEX OAuth provider, clients can:

  • Register themselves, with no manual setup per client.
  • Request access from a VTEX user and keep that access without asking the user to sign in again, using refresh tokens.
  • Discover how the provider works through standard discovery documents and verify tokens on their own.

All endpoints belong to the VTEX ID API, under the OAuth provider tag.

Before you begin

The VTEX OAuth provider issues VTEX ID access tokens on behalf of a user. It doesn't replace application keys for server-to-server integrations. To choose the credential that fits your use case, see the Authentication guide.

Discovery

Clients can find the provider endpoints automatically instead of hardcoding them:

DocumentEndpointSpecification
Authorization server metadataGET /.well-known/oauth-authorization-server/{tenant}RFC 8414
OpenID Connect discoveryGET /.well-known/openid-configurationOpenID Connect Discovery

In the {tenant} path parameter, use the account name.

Authorization flow

The following diagram shows the authorization code flow, from client registration to refreshing the access token. Redirects (302) are shown as separate steps, because the user's browser carries the flow between your client and VTEX:

If the user denies access, VTEX redirects to the redirect_uri with the access_denied error.

What the user sees

During the flow, the user leaves your client, interacts with VTEX pages, and returns to your client:

  1. Login: If the user doesn't have an active VTEX session, they'll see the VTEX Admin login screen, where they enter their email and select Continue, or sign in with Google. The user signs in on VTEX and never enters their password in your client.

  2. Consent: After the user logs in, VTEX shows a consent screen titled Authorize Access, which informs the user that an application is requesting access and lists the necessary actions:

    • Identify the user by reading their profile and email.
    • Determine which resources the user can access by reading their roles and assigned resources.
    • Act on the user's behalf using those resources.

    The user then selects Allow or Cancel. If the user has already granted your client permission, they don't need to consent again.

    The screen doesn't show your client's name.

    {"base64":"  ","img":{"width":464,"height":590,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":30235,"url":"https://cdn.jsdelivr.net/gh/vtexdocs/dev-portal-content@main/images/vtex-oauth-provider-consent.png"}}

    For users with the Super Admin role, the screen also warns that the application will act across all resources and includes a checkbox that the user must select to activate the Allow button.

    {"base64":"  ","img":{"width":464,"height":729,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":39631,"url":"https://cdn.jsdelivr.net/gh/vtexdocs/dev-portal-content@main/images/vtex-oauth-provider-consent-super-admin.png"}}

    These screens are provided by VTEX and are the same for every client, so you can't customize them.

  3. Return to your client: VTEX redirects the user to your redirect_uri. If the user allowed access, the URL includes the code and the state. If the user denied access, it includes error=access_denied and the state.

Your client must handle both outcomes. For example, when the user denies access, show a message explaining that the client can't continue without permission, and offer to try again.

The following steps describe the flow from the client's perspective:

  1. Register the client.
  2. Send the user to the authorization endpoint.
  3. The user signs in, if needed, and grants or denies access to the client.
  4. Exchange the authorization code for an access token.
  5. Use the access token and, when it expires, refresh it.

Step 1 - Register the client

Each client registers once, using the Register client endpoint, which implements dynamic client registration (RFC 7591).


_10
curl --request POST \
_10
--url 'https://{accountName}.myvtex.com/api/vtexid/v1/oauth/register' \
_10
--header 'Content-Type: application/json' \
_10
--data '{
_10
"client_name": "My App",
_10
"redirect_uris": ["https://app.example.com/callback"],
_10
"token_endpoint_auth_method": "client_secret_basic",
_10
"grant_types": ["authorization_code", "refresh_token"],
_10
"response_types": ["code"]
_10
}'

FieldDescription
redirect_urisRequired. Absolute URIs where VTEX redirects the user after the authorization.
client_nameHuman-readable name of the client.
token_endpoint_auth_methodHow the client authenticates at the token endpoint: client_secret_basic (default), client_secret_post, or none for public clients.
grant_typesMust include authorization_code. Add refresh_token to allow the client to refresh access tokens.
response_typesMust include code.

The response contains the client_id and, for confidential clients, the client_secret. Store the secret securely: never expose it in frontend code or public repositories.

Step 2 - Request authorization

Generate a PKCE code_verifier and derive the code_challenge from it with the S256 method. Then redirect the user's browser to the Request authorization code endpoint:


_10
https://{accountName}.myvtex.com/api/vtexid/v1/oauth/authorize
_10
?response_type=code
_10
&client_id={client_id}
_10
&redirect_uri=https://app.example.com/callback
_10
&scope=openid
_10
&state={state}
_10
&code_challenge={code_challenge}
_10
&code_challenge_method=S256

If the user isn't signed in, VTEX asks them to log in. Then VTEX shows a consent page where the user grants or denies access to your client. Your client doesn't interact with the consent page; VTEX handles the user's decision and redirects the user back to your client.

After the user grants access, VTEX redirects the user agent to your redirect_uri with the code and the state you sent. Check that the state matches the one you generated. If the authorization fails, VTEX redirects with the error and state in the URL fragment.

Step 3 - Exchange the code for an access token

Call the Get access token endpoint with the code and the code_verifier you generated in the previous step. Confidential clients authenticate with their client_id and client_secret, either in the body or through HTTP Basic authentication.


_10
curl --request POST \
_10
--url 'https://{accountName}.myvtex.com/api/vtexid/v1/oauth/token' \
_10
--header 'Content-Type: application/x-www-form-urlencoded' \
_10
--data-urlencode 'grant_type=authorization_code' \
_10
--data-urlencode 'code={code}' \
_10
--data-urlencode 'redirect_uri=https://app.example.com/callback' \
_10
--data-urlencode 'code_verifier={code_verifier}' \
_10
--data-urlencode 'client_id={client_id}' \
_10
--data-urlencode 'client_secret={client_secret}'

The response contains the access token:


_10
{
_10
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
_10
"token_type": "Bearer",
_10
"expires_in": 86400,
_10
"refresh_token": "opaque-refresh-token-handle"
_10
}

The refresh_token is only returned if the client registered for the refresh_token grant.

Step 4 - Use and validate the access token

The access token is a VTEX ID token. Resource servers and clients can verify it with the Introspect access token endpoint, which returns {"active": true} along with the token claims, or {"active": false} if the token isn't valid. Public clients (token_endpoint_auth_method set to none) can't use this endpoint.

Refreshing the access token

To keep access without asking the user to sign in again, call the Get access token endpoint with grant_type=refresh_token:


_10
curl --request POST \
_10
--url 'https://{accountName}.myvtex.com/api/vtexid/v1/oauth/token' \
_10
--header 'Content-Type: application/x-www-form-urlencoded' \
_10
--data-urlencode 'grant_type=refresh_token' \
_10
--data-urlencode 'refresh_token={refresh_token}' \
_10
--data-urlencode 'client_id={client_id}' \
_10
--data-urlencode 'client_secret={client_secret}'

You can use each refresh token only once. Every successful refresh returns a new refresh_token, and the one you just used stops working. Presenting a refresh token a second time results in an invalid_grant error, so always store the latest one.

The refresh token expires when your client's session ends, and refreshing doesn't extend it. The session duration comes from the account session settings, and the default is 7 days. When the session ends, the token endpoint returns invalid_grant and your client must restart the authorization flow.

The OAuth refresh_token isn't the same as the vid_rt refresh token used in headless storefronts. When you request an access token, VTEX creates a session for your client that is independent of the user's browser session. VTEX keeps the underlying VTEX ID refresh token on its servers and gives your client only an opaque refresh_token to use at the token endpoint. Because the sessions are independent, the user signing out of their browser doesn't end your client's session. To end it, revoke the token.

Revoking tokens

To end a session, call the Revoke token endpoint with either the access token or the refresh token. Revoking either one ends the whole client session, so all its refresh tokens stop working. Public clients can revoke their own tokens without a secret.

Common errors

Token, introspection, revocation, and registration endpoints return errors in the format defined by RFC 6749:


_10
{
_10
"error": "invalid_grant",
_10
"error_description": "The provided authorization grant is invalid, expired, or revoked."
_10
}

ErrorMeaning
invalid_requestA required parameter is missing or the request is malformed.
invalid_clientThe client doesn't exist, isn't active, or failed authentication.
invalid_grantThe authorization code or refresh token is unknown, expired, already used, or was issued to a different client.
unauthorized_clientThe client isn't registered for the requested grant, for example, using refresh_token without registering for it.
unsupported_grant_typeThe grant_type isn't supported.
invalid_redirect_uriThe redirect_uri isn't a valid absolute URI or isn't registered for the client.
invalid_client_metadataThe registration metadata is invalid.