0

Set tags on an Account

by
Published Apr 8, 2025

Sets all tags on an Account. Any tags that are not provided in the request will be removed.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Set tags on an Account
7
 * Sets all tags on an Account. Any tags that are not provided in the request will be removed.
8
 */
9
export async function main(
10
  auth: Persona,
11
  account_id: string,
12
  body: { meta?: { "tag-name"?: string[]; "tag-id"?: string[] } },
13
  include?: string,
14
  fields?: string,
15
  Key_Inflection?: string,
16
  Idempotency_Key?: string,
17
  Persona_Version?: string,
18
) {
19
  const url = new URL(
20
    `https://api.withpersona.com/api/v1/accounts/${account_id}/set-tags`,
21
  );
22
  for (const [k, v] of [
23
    ["include", include],
24
    ["fields", fields],
25
  ]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const headers: Record<string, string> = {
31
    Authorization: `Bearer ${auth.apiKey}`,
32
    "Content-Type": "application/json",
33
  };
34
  if (Key_Inflection) {
35
    headers["Key-Inflection"] = Key_Inflection;
36
  }
37
  if (Idempotency_Key) {
38
    headers["Idempotency-Key"] = Idempotency_Key;
39
  }
40
  if (Persona_Version) {
41
    headers["Persona-Version"] = Persona_Version;
42
  }
43
  const response = await fetch(url, {
44
    method: "POST",
45
    headers,
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