0

Create a password

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** `connect_production_branch`, `connect_branch` **OAuth Scopes** | Resource | Scopes | | :------- | :---------- | | Organization | `manage_passwords`, `manage_production_branch_passwords` | | Database | `manage_passwords`, `manage_production_branch_passwords` | | Branch | `manage_passwords` |

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 password
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
 `connect_production_branch`, `connect_branch`
14

15
**OAuth Scopes**
16

17
 | Resource | Scopes |
18
| :------- | :---------- |
19
| Organization | `manage_passwords`, `manage_production_branch_passwords` |
20
| Database | `manage_passwords`, `manage_production_branch_passwords` |
21
| Branch | `manage_passwords` |
22
 */
23
export async function main(
24
  auth: Planetscale,
25
  organization: string,
26
  database: string,
27
  branch: string,
28
  body: {
29
    name?: string;
30
    role?: "reader" | "writer" | "admin" | "readwriter";
31
    replica?: false | true;
32
    ttl?: number;
33
    cidrs?: string[];
34
  },
35
) {
36
  const url = new URL(
37
    `https://api.planetscale.com/v1/organizations/${organization}/databases/${database}/branches/${branch}/passwords`,
38
  );
39

40
  const response = await fetch(url, {
41
    method: "POST",
42
    headers: {
43
      "Content-Type": "application/json",
44
      Authorization: `${auth.serviceTokenId}:${auth.serviceToken}`,
45
    },
46
    body: JSON.stringify(body),
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54