0

Create a Geolocation 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 Geolocation 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
        latitude?: number;
15
        "list-id"?: string;
16
        longitude?: number;
17
        "radius-meters"?: number;
18
      };
19
    };
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(
28
    `https://api.withpersona.com/api/v1/list-item/geolocations`,
29
  );
30
  for (const [k, v] of [
31
    ["include", include],
32
    ["fields", fields],
33
  ]) {
34
    if (v !== undefined && v !== "" && k !== undefined) {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const headers: Record<string, string> = {
39
    Authorization: `Bearer ${auth.apiKey}`,
40
    "Content-Type": "application/json",
41
  };
42
  if (Key_Inflection) {
43
    headers["Key-Inflection"] = Key_Inflection;
44
  }
45
  if (Idempotency_Key) {
46
    headers["Idempotency-Key"] = Idempotency_Key;
47
  }
48
  if (Persona_Version) {
49
    headers["Persona-Version"] = Persona_Version;
50
  }
51
  const response = await fetch(url, {
52
    method: "POST",
53
    headers,
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62