Overview
Creating an OAuth application within PlanetScale allows your application to access your users’ PlanetScale accounts. With PlanetScale OAuth applications, you can choose what access your application needs, and a user will allow (or deny) your application those accesses on their PlanetScale account. The organization that you create the OAuth application in is the “owner” of the application.Getting started
1. Creating an OAuth application in PlanetScale
- To create a new OAuth application, log into your organization and click Settings > OAuth applications.
- Create a new OAuth application by clicking Create new application.
- You will need to fill out the following fields:
- Name: A user-friendly name for your OAuth application.
- Domain: The full URL to your application’s domain.
- Redirect URI: The full URL PlanetScale should redirect users on completion of the authorization flow, also known as the callback URL. It must have the same domain as the domain above.
- Avatar: An image that represents your OAuth application. (Optional but recommended.)
2. Credentials to copy to your application code
Once you have created your OAuth application in PlanetScale, you will need the following credentials to use the OAuth authorization flow:- ID: Your OAuth application’s ID.
- Client ID: Your OAuth application’s client ID.
- Redirect URL: The full URL PlanetScale should redirect users on completion of the authorization flow, also known as the callback URL.
- Client secret: Your OAuth application’s client secret, used to exchange authorization codes for access tokens. (This will only be shown once, make sure to save it!)
3. OAuth application access scopes
Every OAuth application in PlanetScale will request from its users a specific set of permissions in the users’ databases. We call these permissions “access scopes.” They are broken into:- User access
- Organization access
- Database access
- Branch access
write_branches on an organization allows you to write branches across all databases in organizations the user gives permission to, while write_branches on a database enables you to only write branches in databases the user gives permission to.
The API reference for each endpoint will say what scope is needed.
In this step, select the access scopes you think your application will need on a user’s account and click the Save access scopes button.
OAuth Authorization Flow
PlanetScale’s OAuth implementation supports the Authorization Code grant type. The following diagram walks through the flow.
0. Prerequisites
You must have created an OAuth application in PlanetScale (see step 1 above) and saved your:- Client ID: Your OAuth application’s client ID
- Client Secret: Your OAuth application’s client secret
- Redirect URI: The callback URL for your application
1. User authorizes your OAuth application on their account
Your application should direct your users to the PlanetScale authorization page (see URL below) so that they can grant your application access to their PlanetScale account:Query parameters:
Your OAuth application’s client id
The full URL PlanetScale should redirect users on completion of the authorization flow, also known as the callback URL
You may also optionally pass a state parameter, which exists to prevent third-party attacks. Pass a random string, and PlanetScale will return it in step 2. Compare to ensure the request came from your application.
2. The authorization code returns to your application
Upon authorization, PlanetScale will redirect the user to yourredirect_uri with an authorization code in the query parameters. The authorization code is only good for one use. It will look like the following URL:
Query parameters:
An authorization code to be exchanged for an access token
Compare with the original
state parameter to ensure they match. Abort the process if they do not because the request may have come from a third party. Only present if you provided one in step 1.3a. Exchange authorization code for an OAuth token
Your application can now exchange the authorization code for an access token. POSTPOST request will need the following:
Query parameters:
Your OAuth application’s client ID
Your OAuth application’s client secret
The code located in the query parameters of the previous step
Set to
authorization_codeThe full URL PlanetScale should redirect users on completion of the authorization flow, also known as the callback URL
Your OAuth access token. Use this in the
Authorization: Bearer <access_token> header to make API calls on behalf of the user.Will always be “Bearer”
The number of seconds until the access token expires
A refresh token that can refresh your access token when it expires. See step 3b for more info.
Space-separated list of scopes granted to this token
access_token to make API calls on behalf of the user using Bearer authentication.
3b. Refreshing an OAuth token
When an OAuth token expires, you can refresh it: POSTPOST request will need the following:
Query parameters:
Your OAuth application’s client ID
Your OAuth application’s client secret
The refresh token sent in the initial token response in step 3a
Set to
refresh_token4. Using the OAuth token
Now your application can make requests to the PlanetScale API on behalf of the user. To make requests to the API, add theaccess_token in the Authorization header as a Bearer token in your HTTP API request using the following format:


