0

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