0

Update a user

by
Published Oct 17, 2025
Script gorgias Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gorgias = {
3
  username: string;
4
  apiKey: string;
5
  domain: string;
6
};
7
/**
8
 * Update a user
9
 *
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  id: string,
14
  body: {
15
    bio?: string;
16
    email?: string;
17
    external_id?: string;
18
    meta?: {};
19
    name?: string;
20
    new_password?: string;
21
    old_password?: string;
22
    roles?: {
23
      name?:
24
        | "admin"
25
        | "agent"
26
        | "basic-agent"
27
        | "lite-agent"
28
        | "observer-agent"
29
        | "user";
30
    }[];
31
  },
32
) {
33
  const url = new URL(`https://${auth.domain}.gorgias.com/api/users/${id}/`);
34

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