1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Import Accounts |
7 | * Bulk import accounts by uploading a CSV file. |
8 |
|
9 | Each row should be the details for a new account. The columns we allow are: |
10 | - reference_id |
11 | - name_first |
12 | - name_middle |
13 | - name_last |
14 | - birthdate |
15 | - social_security_number |
16 | - tags |
17 | */ |
18 | export async function main( |
19 | auth: Persona, |
20 | body: { |
21 | data: { attributes: { file: { data?: string; filename?: string } } }; |
22 | }, |
23 | include?: string, |
24 | fields?: string, |
25 | Key_Inflection?: string, |
26 | Idempotency_Key?: string, |
27 | Persona_Version?: string, |
28 | ) { |
29 | const url = new URL(`https://api.withpersona.com/api/v1/importer/accounts`); |
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 formData = new FormData(); |
39 | for (const [k, v] of Object.entries(body)) { |
40 | if (v !== undefined) { |
41 | formData.append(k, String(v)); |
42 | } |
43 | } |
44 | const headers: Record<string, string> = { |
45 | Authorization: `Bearer ${auth.apiKey}`, |
46 | "Content-Type": "application/json", |
47 | }; |
48 | if (Key_Inflection) { |
49 | headers["Key-Inflection"] = Key_Inflection; |
50 | } |
51 | if (Idempotency_Key) { |
52 | headers["Idempotency-Key"] = Idempotency_Key; |
53 | } |
54 | if (Persona_Version) { |
55 | headers["Persona-Version"] = Persona_Version; |
56 | } |
57 | const response = await fetch(url, { |
58 | method: "POST", |
59 | headers, |
60 | body: JSON.stringify(body), |
61 | }); |
62 | if (!response.ok) { |
63 | const text = await response.text(); |
64 | throw new Error(`${response.status} ${text}`); |
65 | } |
66 | return await response.json(); |
67 | } |
68 |
|