> ## Documentation Index
> Fetch the complete documentation index at: https://www.unkey.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Unkey is two separate products. Compute builds, deploys, and runs apps behind a gateway. API Management issues API keys, enforces rate limits, manages identities and permissions, and reports usage. Say which product a page belongs to; a reader can use either without the other.
> Every Unkey API endpoint is an HTTP POST to https://api.unkey.com/v2/{service}.{procedure} with a root key in the Authorization: Bearer header. Root keys are workspace scoped.
> Error codes have the form err:{system}:{category}:{specific} and each has a page at /errors/{system}/{category}/{specific}.
> The word environment means production or preview in Compute. Rate limiting has four meanings on this site; the glossary lists them.

# Connect and disconnect

> Connect your PlanetScale account and database branches to your app, and disconnect or remove them later.

## Connect your PlanetScale account

Connect your account in the workspace settings or from the project's databases page. Unkey sends you to PlanetScale to approve access, then back to the dashboard. Only workspace admins can connect or disconnect an account.

* A workspace connects one PlanetScale account. If you connect it again, Unkey replaces its tokens.
* Disconnecting the account also removes every database that uses it from its project. Unkey then asks PlanetScale to revoke the token. If that fails, Unkey still deletes its copy, and you can revoke the token in your PlanetScale settings.

If PlanetScale later refuses the account's token, see [Account needs a reconnect](https://www.unkey.com/docs/compute/integrations/planetscale/troubleshooting.md#account-needs-a-reconnect).

## Connect a database to an app

Any workspace member can add a database to a project and connect or disconnect environments. Creating a new database needs a workspace admin.

First add the database to your project. On the project's databases page, click **Create database** to create a new PlanetScale database, or **Connect database** to add one that already exists. Both open a side panel. After a new database is ready, the panel continues straight into connecting an app, which you can skip.

You can connect a database to an app from either side:

* **From the database**: open the database's page and click **Connect app** in its **Connections** section. Pick the app in the **Select an app** step. Each connected app shows as a row, and clicking it opens **Manage connection**.
* **From the app**: open your app's **Connections** page, click **Add connection**, and pick the database. If the page also offers app connections, first choose **Database** in the **Choose a connection** step. Connected databases share one table with your app connections. Each database row shows the environments it's connected to and their branches. Click a row to open **Manage connection**. Its menu has **Go to database**, **Manage connection**, and **Disconnect**.

Both paths use the same steps. Each environment of the app talks to one branch of the database, and you choose which one in the **Choose branches** step. The step has a field for production and one for preview. An environment that's already connected to the database shows **Connected to** and its branch instead of a picker. Nothing is saved until you click **Connect and redeploy**, or **Connect** when connecting doesn't redeploy production.

### Suggested branches

Unkey fills in a branch for each environment, and you can change any of them. Say your project has two apps, `api` and `worker`, and the `orders` database has a `main` branch (the default) and a `preview` branch.

* **You connect `api` first.** Nothing else uses `orders` yet, so Unkey suggests `main` for production, because it's the default branch, and `preview` for preview, because it matches the environment's name.
* **Later you connect `worker`.** `api` already uses `orders`, so Unkey suggests the same branches.

If you keep the suggestions, `api` and `worker` read and write the same data in each environment. Unkey leaves a branch empty when it has nothing to suggest: when no branch matches the environment's name, or when two apps already use different branches for the same environment.

To use another branch later, [disconnect the environment](#disconnect-and-remove), then connect it again with the new branch. If another environment of the app is still connected to this database, **Manage connection** stays open and the environment's tab shows **Not connected**. Select that tab and click **Connect** with the environment's name, for example **Connect Preview**, to pick the new branch. If it was the app's last environment connected to this database, the connection row goes away and **Manage connection** closes. Connect again with **Add connection** on the app's **Connections** page, or with **Connect app** on the database's page. See [Disconnect and remove](#disconnect-and-remove) for what happens to running deployments, and [When a connection takes effect](#when-a-connection-takes-effect) for when the new branch reaches your deployments.

> [!WARNING]
>
> Connecting preview to the production branch means preview deployments read and write your production data, and the **Choose branches** step warns you. **Connect an app** doesn't create branches. To give preview its own data, create a `preview` branch first: open the database's page, click **Create preview branch** in its **Database branches** section, and pick a size. The button shows only while the database has one branch, and only admins can use it. PlanetScale bills the branch separately.

### When a connection takes effect

Credentials are created only when a deployment starts, so connecting doesn't change running deployments. After you connect, the dashboard redeploys live production, and production gets the database once that new deployment is live. If production is rolled back or has no deployment yet, the next production deployment gets it instead. If the redeploy fails, the dashboard shows an error and a redeploy banner.

A preview environment has no single live deployment, so its next deployment gets the database. This also applies to any stopped deployment that wakes.

## Disconnect and remove

> [!WARNING]
>
> Disconnecting an environment deletes its running deployments' credentials right away. On Vitess and Neki, open connections close within seconds. On Postgres, they stay open until you stop or redeploy, but no new ones can be made. Before disconnecting, deploy a version of your app that doesn't need the database. Older deployments you might roll back to lose their credentials too.

* **Disconnect an environment**: open **Manage connection**, select the environment's tab under **Credentials**, click the **⋯** button beside the tabs (named **Actions for \[environment]**, for example **Actions for Production**), and choose **Disconnect** with the environment's name. Unkey tries to delete the database's credentials for every deployment of the environment. If any delete fails, the environment stays connected, and credentials already deleted stay deleted. If your PlanetScale account caused the failure, the dashboard asks you to reconnect it. Once it's reconnected, you can try again.
* **Disconnect an app**: choose **Disconnect** in the connection's menu, or in **Manage connection** click the **⋯** button beside the tabs (named **Actions for \[environment]**) and choose **Disconnect** with the app's name. Unkey disconnects every environment of the app from the database, as with disconnecting an environment.
* **Disconnect a database from a project**: open the database's page, open the **More actions** menu, and choose **Disconnect from project**. Unkey first disconnects every environment that uses the database, as with disconnecting an environment, so those apps lose database access. If any credential can't be deleted, the database stays connected, and you can try again. The database and its data stay in PlanetScale.
* **Disconnect your PlanetScale account**: Unkey disconnects every environment that uses the account's databases, removes those databases from their projects, then revokes the account's access. Those deployments lose their credentials right away, as with disconnecting an environment. If a credential can't be deleted, the account stays connected, and you can try again. Your data in PlanetScale stays.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.