What is Xata?
OAuth 2
alpha
alpha feature

Experimental feature subject to potentially major changes, this is not recommended for use in production and access must be requested. Request access.

OAuth 2

Edit on GitHub

Xata offers OAuth 2.0 authentication for external applications. This allows you to use Xata as an identity provider for your application. This is useful if you want to build an application that uses Xata and you want to allow users to sign in with their Xata account.

#

Authorization Code Flow

Xata supports the Authorization Code Flow of OAuth2. This flow is designed for web applications that run on a server. It is not designed for mobile or desktop applications.

The Authorization Code Flow works as follows:

  1. The application redirects the user to Xata to sign in.
  2. Xata redirects the user back to the application with an authorization code.
  3. The application exchanges the authorization code for an access token.
  4. The application uses the access token to make requests to the Xata API.

To redirect the user to Xata, the application must redirect the user to the following URL:

https://app.xata.io/integrations/oauth/authorize

The application must include the following query parameters:

ParameterDescription
client_idThe client ID of the application.
redirect_uriThe URL that Xata should redirect the user back to after the user signs in. This URL must be registered with the application.
response_typeMust be set to code.
scopeScopes that the application is requesting. Scopes define what actions or data the application can access or perform for the user. This can be repeated to request multiple scopes. Currently, the only supported scope is admin:all and it can be ignored as it is the default.
stateA string that the application can use to maintain state between the request and the callback. Also used to prevent CSRF attacks.

For example:

https://app.xata.io/integrations/oauth/authorize?client_id=123&redirect_uri=https://example.com/callback&response_type=code&scope=admin:all&state=abc123

After the user signs in, Xata will redirect the user back to the application. Xata will include the following query parameters:

ParameterDescription
codeThe authorization code.
stateThe state that was provided in the original request.
color-modeThe color mode of the user's Xata account. Can be light or dark.

For example:

https://example.com/callback?code=abc123&state=abc123&color-mode=light

To exchange the authorization code for an access token, the application must make a POST request to the following URL:

https://app.xata.io/api/integrations/oauth/token

The application must include the following parameters in the request body:

ParameterDescription
client_idThe client ID of the application.
client_secretThe client secret of the application.
grant_typeMust be set to authorization_code.
codeThe authorization code.
redirect_uriThe URL used in the original request.

For example:

{
  "client_id": "123",
  "client_secret": "abc",
  "grant_type": "authorization_code",
  "code": "abc123",
  "redirect_uri": "https://example.com/callback"
}

The response will be a JSON object with the following properties:

PropertyDescription
access_tokenThe access token.
refresh_tokenThe refresh token.
expires_inThe date and time when the access token will expire.
token_typeThe type of token. Will always be Bearer.

For example:

{
  "access_token": "abc123",
  "refresh_token": "def456",
  "expires_in": "2023-10-19T03:04:28Z",
  "token_type": "Bearer"
}

The application can use the access token to make requests to the Xata API. The access token must be included in the Authorization header of the request.

Access tokens are set to automatically expire. When an access token expires, the application must use the refresh token to get a new access token.

To refresh the access token, the application must make a POST request to the following URL:

https://app.xata.io/api/integrations/oauth/token

The application must include the following parameters in the request body:

ParameterDescription
client_idThe client ID of the application.
client_secretThe client secret of the application.
grant_typeMust be set to refresh_token.
refresh_tokenThe refresh token.

For example:

{
  "client_id": "123",
  "client_secret": "abc",
  "grant_type": "refresh_token",
  "refresh_token": "def456"
}

The response will be a JSON object with the following properties:

PropertyDescription
access_tokenThe access token.
refresh_tokenThe refresh token.
expires_inThe number of seconds until the access token expires.
token_typeThe type of token. Will always be Bearer.

At the moment, Xata only supports the scope admin:all. This scope allows the application to make requests to the Xata API on behalf of the user. The scope has the same permissions as an API key created by the user.

  • Authorization codes expire after 10 minutes.
  • Access tokens expire after 1 month.
  • A user can only have 5 pending authorization codes at a time per application.
  • A user can only have 5 valid access tokens at a time per application.

On this page

Authorization Code FlowRedirecting the user to XataRedirecting the user back to the applicationExchanging the authorization code for an access tokenUsing the access tokenRefreshing the access tokenScopesLimits