0

Create a String List Item

by
Published Apr 8, 2025

Create a new item in 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
 * Create a String List Item
7
 * Create a new item in a >.
8
 */
9
export async function main(
10
  auth: Persona,
11
  body: { data?: { attributes?: { "list-id"?: string; value?: string } } },
12
  include?: string,
13
  fields?: string,
14
  Key_Inflection?: string,
15
  Idempotency_Key?: string,
16
  Persona_Version?: string,
17
) {
18
  const url = new URL(`https://api.withpersona.com/api/v1/list-item/strings`);
19
  for (const [k, v] of [
20
    ["include", include],
21
    ["fields", fields],
22
  ]) {
23
    if (v !== undefined && v !== "" && k !== undefined) {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const headers: Record<string, string> = {
28
    Authorization: `Bearer ${auth.apiKey}`,
29
    "Content-Type": "application/json",
30
  };
31
  if (Key_Inflection) {
32
    headers["Key-Inflection"] = Key_Inflection;
33
  }
34
  if (Idempotency_Key) {
35
    headers["Idempotency-Key"] = Idempotency_Key;
36
  }
37
  if (Persona_Version) {
38
    headers["Persona-Version"] = Persona_Version;
39
  }
40
  const response = await fetch(url, {
41
    method: "POST",
42
    headers,
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51