0

Add Persona objects to a Case

by
Published Apr 8, 2025

Adds Persona objects (such as inquiries, accounts, or reports) to a >

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 Persona objects to a Case
7
 * Adds Persona objects (such as inquiries, accounts, or reports) to a >
8
 */
9
export async function main(
10
  auth: Persona,
11
  case_id: string,
12
  body: { meta?: { "object-ids"?: 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/cases/${case_id}/add-objects`,
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