0

Add tag to Transaction

by
Published Apr 8, 2025

Adds a tag to a Transaction. Create a new tag if the tag does not already exist. No effect if the transaction already has the tag.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Add tag to Transaction
7
 * Adds a tag to a Transaction. Create a new tag if the tag does not already exist. No effect if the transaction already has the tag.
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}/add-tag`,
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