0

Import IP Address Lists

by
Published Apr 8, 2025

Bulk import IP address List Items by uploading a CSV file. Each row should be the details for a new List Item. The columns we allow are: - value Both IPv4 and IPv6 are supported.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Import IP Address Lists
7
 * Bulk import IP address List Items by uploading a CSV file.
8

9
Each row should be the details for a new List Item. The columns we allow are:
10
  - value
11

12
Both IPv4 and IPv6 are supported.
13
 */
14
export async function main(
15
  auth: Persona,
16
  body: {
17
    data: {
18
      attributes: {
19
        file: { data?: string; filename?: string };
20
        "list-id": string;
21
      };
22
    };
23
  },
24
  include?: string,
25
  fields?: string,
26
  Key_Inflection?: string,
27
  Idempotency_Key?: string,
28
  Persona_Version?: string,
29
) {
30
  const url = new URL(
31
    `https://api.withpersona.com/api/v1/importer/list-item/ip-addresses`,
32
  );
33
  for (const [k, v] of [
34
    ["include", include],
35
    ["fields", fields],
36
  ]) {
37
    if (v !== undefined && v !== "" && k !== undefined) {
38
      url.searchParams.append(k, v);
39
    }
40
  }
41
  const formData = new FormData();
42
  for (const [k, v] of Object.entries(body)) {
43
    if (v !== undefined) {
44
      formData.append(k, String(v));
45
    }
46
  }
47
  const headers: Record<string, string> = {
48
    Authorization: `Bearer ${auth.apiKey}`,
49
    "Content-Type": "application/json",
50
  };
51
  if (Key_Inflection) {
52
    headers["Key-Inflection"] = Key_Inflection;
53
  }
54
  if (Idempotency_Key) {
55
    headers["Idempotency-Key"] = Idempotency_Key;
56
  }
57
  if (Persona_Version) {
58
    headers["Persona-Version"] = Persona_Version;
59
  }
60
  const response = await fetch(url, {
61
    method: "POST",
62
    headers,
63
    body: JSON.stringify(body),
64
  });
65
  if (!response.ok) {
66
    const text = await response.text();
67
    throw new Error(`${response.status} ${text}`);
68
  }
69
  return await response.json();
70
}
71