0

Create a Case

by
Published Apr 8, 2025

Creates a new case in your organization.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Create a Case
7
 * Creates a new case in your organization.
8
 */
9
export async function main(
10
  auth: Persona,
11
  body: {
12
    data: {
13
      attributes?: {
14
        "case-template-id": string;
15
        "creator-email-address"?: string;
16
        "case-queue-id"?: string;
17
      };
18
    };
19
    meta?: { "object-ids"?: string[]; "sla-expires-in-seconds"?: number };
20
  },
21
  include?: string,
22
  fields?: string,
23
  Key_Inflection?: string,
24
  Idempotency_Key?: string,
25
  Persona_Version?: string,
26
) {
27
  const url = new URL(`https://api.withpersona.com/api/v1/cases`);
28
  for (const [k, v] of [
29
    ["include", include],
30
    ["fields", fields],
31
  ]) {
32
    if (v !== undefined && v !== "" && k !== undefined) {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const headers: Record<string, string> = {
37
    Authorization: `Bearer ${auth.apiKey}`,
38
    "Content-Type": "application/json",
39
  };
40
  if (Key_Inflection) {
41
    headers["Key-Inflection"] = Key_Inflection;
42
  }
43
  if (Idempotency_Key) {
44
    headers["Idempotency-Key"] = Idempotency_Key;
45
  }
46
  if (Persona_Version) {
47
    headers["Persona-Version"] = Persona_Version;
48
  }
49
  const response = await fetch(url, {
50
    method: "POST",
51
    headers,
52
    body: JSON.stringify(body),
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60