0

Update a user

by
Published Apr 8, 2025

Update a user's attributes.

Script clerk Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Clerk = {
3
  apiKey: string;
4
};
5
/**
6
 * Update a user
7
 * Update a user's attributes.
8
 */
9
export async function main(
10
  auth: Clerk,
11
  user_id: string,
12
  body: {
13
    external_id?: string;
14
    first_name?: string;
15
    last_name?: string;
16
    primary_email_address_id?: string;
17
    notify_primary_email_address_changed?: false | true;
18
    primary_phone_number_id?: string;
19
    primary_web3_wallet_id?: string;
20
    username?: string;
21
    profile_image_id?: string;
22
    password?: string;
23
    password_digest?: string;
24
    password_hasher?: string;
25
    skip_password_checks?: false | true;
26
    sign_out_of_other_sessions?: false | true;
27
    totp_secret?: string;
28
    backup_codes?: string[];
29
    public_metadata?: {};
30
    private_metadata?: {};
31
    unsafe_metadata?: {};
32
    delete_self_enabled?: false | true;
33
    create_organization_enabled?: false | true;
34
    legal_accepted_at?: string;
35
    skip_legal_checks?: false | true;
36
    create_organizations_limit?: number;
37
    created_at?: string;
38
  },
39
) {
40
  const url = new URL(`https://api.clerk.com/v1/users/${user_id}`);
41

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