Branching
Branching is currently in Private Alpha:
- The feature is under active development and available to invited users only. Access must be requested.
- APIs, parameters and response fields may change in incompatible ways before general availability.
- No SLA is provided. Do not use it in production workloads.
To request access or send feedback, contact your account manager or submit a ticket.
Branching lets you manage your backend the way you manage code: create a branch from an environment, and the branch comes with the full database schema, RLS policies and data as a fully isolated environment. You can change tables, write data and deploy functions on the branch, or let an AI agent do it, without affecting the main environment.
A single CreateBranch call gives you a working branch environment, plus an ephemeral key that can only access that branch.
Use with an AI agent
We recommend having your agent (Claude Code, Cursor, CodeBuddy, etc.) call the branching APIs through CloudBase MCP or CloudBase CLI, logged in with an environment API key. An environment API key can only operate on the environment it belongs to.
- Create an environment API key as described in Prerequisites and configure it in the agent's runtime.
- Give the agent an instruction such as:
Read https://docs.cloudbase.net/en/database/postgresql/branching,
use CloudBase MCP to create a branch named feat-login from main,
then insert a few test rows into the notes table on the branch and query them.
You can also click "Copy page" or "View Markdown" on the right side of this page and give the content to the agent.
Use cases
- A safe workspace for AI agents: the agent creates tables, changes RLS and writes test data on the branch. The branch's ephemeral key cannot access main, so the agent never touches production data.
- Validate on real data before release: the branch carries main's full data, so new features, new queries and performance issues surface on near-production data.
- Rehearse destructive migrations: before dropping columns, changing types or rebuilding indexes, run them on a branch first to check duration and results.
- One environment per task: when developing several features in parallel, each feature gets its own branch without interfering with the others.
How it works
A branch is an independent CloudBase environment with its own environment ID (BranchId), database, functions and endpoints. It also records its parent branch, forming a branch tree. The root environment itself is main (Depth 0).
When a branch is created, the platform copies the parent branch's content into the new environment:
| Resource | State in the branch |
|---|---|
| PostgreSQL | Full copy: schema, indexes, RLS policies, functions and data. The branch database is separate; reads and writes do not affect each other |
| Authentication | Authentication config is inherited along with the database |
| Cloud functions | Copied |
| API keys | Not copied. An ephemeral key for the branch is returned on creation; see Branch ephemeral key |
A branch has the same specification as its parent. Branches can have child branches, up to 5 levels deep.
Prerequisites
- Access: a Tencent Cloud account with Private Alpha access, and a CloudBase environment under a platform edition that uses PostgreSQL (shared instance).
- Create an environment API key: in the environment settings of the CloudBase console, create a server-side API key for the environment you want to branch from.
- Configure it as environment variables in the runtime of your agent or scripts:
export CLOUDBASE_API_KEY=<environment API key>
export CLOUDBASE_ENV_ID=<your environment ID>
⚠️ Note: keep the API key in environment variables only and do not paste it into an agent conversation.
- Set up a tool (either one):
- CloudBase MCP: follow Connect CloudBase MCP. On startup MCP reads the two variables above and logs in with the environment API key.
- CloudBase CLI (3.9.0 or later):
npm install -g @cloudbase/cli
You can also call the branching APIs with a CAM SecretId / SecretKey. A CAM key has account-level scope, broader than an environment API key, so it is not recommended for now.
Quick start
The steps below use CloudBase CLI and show the equivalent CloudBase MCP call.
Step 1: Log in
tcb login --cloudbase-api-key "$CLOUDBASE_API_KEY" -e "$CLOUDBASE_ENV_ID"
With CloudBase MCP this step is not needed; MCP logs in from the environment variables.
Step 2: Create a branch
tcb api tcb CreateBranch -e "$CLOUDBASE_ENV_ID" --json \
--body '{"EnvId":"<environment ID>","BranchSetting":{"ParentBranchId":"<environment ID>","BranchName":"feat-a"}}'
In CloudBase MCP:
callCloudApi(service="tcb", action="CreateBranch", region="ap-shanghai",
params={"EnvId": "<environment ID>", "BranchSetting": {"ParentBranchId": "<environment ID>", "BranchName": "feat-a"}})
CreateBranch returns the BranchId immediately and the branch enters the creating state (Status = 1). The ApiKey in the response is the branch ephemeral key; see Branch ephemeral key.
Step 3: List branches
tcb api tcb DescribeBranchList -e "$CLOUDBASE_ENV_ID" --json --body '{"EnvId":"<environment ID>"}'
In CloudBase MCP: callCloudApi(service="tcb", action="DescribeBranchList", region="ap-shanghai", params={"EnvId": "<environment ID>"}).
The branch is ready when Status becomes 0. If creation fails, FailReason gives the reason. Example response (excerpt):
{
"Total": 2,
"BranchList": [
{ "BranchId": "my-app-xxxx", "BranchName": "main", "Depth": 0, "Status": 0 },
{ "BranchId": "feat-a-xxxx", "BranchName": "feat-a", "Depth": 1, "ParentBranchName": "main", "Status": 0 }
]
}
Step 4: Work on the branch
The main environment's API key can only access main, not the branch. To work on the branch, use the branch ephemeral key returned by CreateBranch:
# Read: returns the data in the branch (copied from main)
curl -s "https://<BranchId>.api.tcloudbasegateway.com/v1/rdb/rest/notes?select=id,body" \
-H "Authorization: Bearer $BRANCH_KEY"
# Write: goes to the branch only; main is not affected
curl -s -X POST "https://<BranchId>.api.tcloudbasegateway.com/v1/rdb/rest/notes" \
-H "Authorization: Bearer $BRANCH_KEY" -H "Content-Type: application/json" \
-d '{"body": "written on branch"}'
Using the same key against main returns 401 INVALID_CREDENTIALS.
Step 5: Delete the branch
tcb api tcb DeleteBranch -e "$CLOUDBASE_ENV_ID" --json --body '{"EnvId":"<environment ID>","BranchId":"<BranchId>"}'
After deletion, the branch is removed from the list immediately and its environment resources are reclaimed automatically. A branch with child branches cannot be deleted until its children are deleted.
Branch ephemeral key
The CreateBranch response includes a server-side key dedicated to the branch (KeyType is ephemeral_secret, prefixed with cb_secret_):
- Valid for 1 hour and expires automatically.
- Only works for this branch; using it on main or another branch returns
401. - Acts as the branch's administrator and bypasses RLS. Use it only on servers or in the agent's runtime, never in frontend code.
API reference
All three APIs belong to the CloudBase tcb service, version 2018-06-08, endpoint tcb.tencentcloudapi.com, and can be called with tcb api in the CLI or callCloudApi in MCP.
CreateBranch
Creates a new branch from the specified parent branch.
Request parameters
| Name | Type | Required | Description |
|---|---|---|---|
| EnvId | String | Yes | Root environment ID |
| BranchSetting.ParentBranchId | String | Yes | Parent branch ID. Use the root environment ID to branch from main |
| BranchSetting.BranchName | String | Yes | Branch name, used as the prefix of the BranchId |
| ReqKey | String | No | Request identifier |
Response parameters
| Name | Type | Description |
|---|---|---|
| BranchId | String | New branch ID, which is also the branch's environment ID |
| BranchInfo | Object | Branch information; same fields as DescribeBranchList |
| ApiKey | Object | Branch ephemeral key: ApiKey (key value), KeyType (ephemeral_secret), expiry time |
DescribeBranchList
Lists the branches of an environment.
Request parameters
| Name | Type | Required | Description |
|---|---|---|---|
| EnvId | String | Yes | Root environment ID |
| BranchId | String | No | Query only the specified branch |
| Depth | Integer | No | Filter by level, 0–5; 0 is main |
| Offset | Integer | No | Pagination offset |
| Limit | Integer | No | Page size |
Response parameters
| Name | Type | Description |
|---|---|---|
| Total | Integer | Total number of branches (including main) |
| BranchList[].BranchId | String | Branch ID |
| BranchList[].BranchName | String | Branch name; main for the root |
| BranchList[].Depth | Integer | Level; 0 for main |
| BranchList[].ParentBranchId / ParentBranchName | String | Parent branch |
| BranchList[].Status | Integer | 1 creating, 0 ready |
| BranchList[].FailReason | String | Reason for creation failure |
| BranchList[].CreateTime | String | Creation time |
DeleteBranch
Deletes the specified branch. Returns an error if the branch has child branches; delete the children first.
Request parameters
| Name | Type | Required | Description |
|---|---|---|---|
| EnvId | String | Yes | Root environment ID |
| BranchId | String | Yes | ID of the branch to delete |
Limits
- Only environments under a platform edition with a PostgreSQL shared instance are supported. Calls on regular plan environments (Personal, Standard, etc.) return
UnsupportedOperation.BranchOperation. - A branch has the same specification as its parent; each branch counts toward the environment quota.
- After a branch is created, later changes on main are not synced to the branch.
- This version does not provide branch diff, merging back to main, or resetting from the parent branch.
- Creation time depends on the amount of data and the number of functions.
- Logging in to CloudBase CLI / MCP with an API key is not yet supported on branch environments. The branch ephemeral key can access the branch's data API.