Create connection
Connect an environment to a resource, and set what the connection gives the deployments of the environment. config has one member, named after the type of the resource. app and environment take an id or a slug.
An environment has one connection to each resource. If the connection exists, you get a 409. Change its config with connections.updateConnection.
Deployments that run do not change. Start a new deployment to give the environment the connection.
planetscale_database
The connection gives each deployment database URLs in environment variables. config.planetscaleDatabase.branch takes the branch id or its name, such as main. The connection stores the id, so a renamed branch keeps it. envVars names the variable of each URL:
urlis the read-write URL, and is required.unpooledUrlis the read-write URL that skips PgBouncer. Only Postgres databases have it.replicaUrlturns the read-only replica URL on.
Unkey sets names that start with UNKEY, and app connections set names that end with _HOST, so a variable cannot use either.
- The branch can still provision. Each deployment needs a ready branch to get its credentials, also a deployment that a Git push starts. A deployment that starts before
databases.listBranchesshows the branchreadycan fail, and it does not restart when the branch becomes ready. Redeploy once the branch isready. - A read-only replica URL needs a branch with replicas. Without them you get a
412. - A variable name that the environment already has gets a
400that names it.
The workspace must have the databases, connections and app_v2 flags. Without them you get a 403.
Required Permissions
Your root key must be able to read the resource. For a planetscale_database, that is one of these permissions:
unkey:v1:<workspace_id>:projects/*/databases/*#read(to read the databases of any project)unkey:v1:<workspace_id>:projects/<project_id>/databases/*#read(to read the databases of one project)unkey:v1:<workspace_id>:projects/<project_id>/databases/<database_id>#read(to read one database)
It must also be able to write the credentials of the database, because Unkey creates a credential for each deployment of the connected environment. The caller never gets the credential. That is one of these permissions:
unkey:v1:<workspace_id>:projects/*/databases/*/credentials/*#write(for the databases of any project)unkey:v1:<workspace_id>:projects/<project_id>/databases/*/credentials/*#write(for the databases of one project)unkey:v1:<workspace_id>:projects/<project_id>/databases/<database_id>/credentials/*#write(for one database)
And it must be able to write the connections of the environment, with one of these permissions:
unkey:v1:<workspace_id>:projects/*/apps/*/environments/*/connections/*#write(to connect any environment)unkey:v1:<workspace_id>:projects/<project_id>/apps/<app_id>/environments/<environment_id>/connections/*#write(to connect one environment)
Replace <workspace_id> with the authenticated workspace ID.
Authorizations
Unkey uses bearer tokens for authentication. Public integrations use root keys, while the dashboard proxy uses short-lived JWTs. To authenticate, include the token in the Authorization header of each request:
Root keys have specific permissions attached to them, controlling what operations they can perform. Resource permissions use Unkey Resource Names plus actions, like unkey:v1:ws_123:projects/*/keyspaces/*/keys/*#write.
Security best practices:
- Keep root keys secure and never expose them in client-side code
- Use different root keys for different environments
- Rotate keys periodically, especially after team member departures
- Create keys with minimal necessary permissions following least privilege principle
- Monitor key usage with audit logs.
Body
Response
The environment is connected with the config of the request.