0

Sets tags on a Transaction

by
Published Apr 8, 2025

Set the list of tags on a transaction. Remove all tags on the transaction that don't appear on the list. Add all tags on the transaction from the list.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Sets tags on a Transaction
7
 * Set the list of tags on a transaction. Remove all tags on the transaction that don't appear on the list. Add all tags on the transaction from the list.
8
 */
9
export async function main(
10
  auth: Persona,
11
  transaction_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/transactions/${transaction_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