0

Create a branch

by
Published Oct 17, 2025

### Authorization A service token or OAuth token must have at least one of the following access or scopes in order to use this API endpoint: **Service Token Accesses** `create_branch`, `restore_production_branch_backup`, `restore_backup` **OAuth Scopes** | Resource | Scopes | | :------- | :---------- | | Organization | `write_branches`, `restore_production_branch_backups`, `restore_backups` | | Database | `write_branches`, `restore_production_branch_backups`, `restore_backups` | | Branch | `restore_backups` |.

Script planetscale Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Planetscale = {
3
  serviceTokenId: string;
4
  serviceToken: string;
5
};
6
/**
7
 * Create a branch
8
 * 
9
### Authorization
10
A service token or OAuth token must have at least one of the following access or scopes in order to use this API endpoint:
11

12
**Service Token Accesses**
13
 `create_branch`, `restore_production_branch_backup`, `restore_backup`
14

15
**OAuth Scopes**
16

17
 | Resource | Scopes |
18
| :------- | :---------- |
19
| Organization | `write_branches`, `restore_production_branch_backups`, `restore_backups` |
20
| Database | `write_branches`, `restore_production_branch_backups`, `restore_backups` |
21
| Branch | `restore_backups` |.
22
 */
23
export async function main(
24
  auth: Planetscale,
25
  organization: string,
26
  database: string,
27
  body: { name: string; parent_branch: string; backup_id?: string },
28
) {
29
  const url = new URL(
30
    `https://api.planetscale.com/v1/organizations/${organization}/databases/${database}/branches`,
31
  );
32

33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "Content-Type": "application/json",
37
      Authorization: `${auth.serviceTokenId}:${auth.serviceToken}`,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47