0

Create an Account

by
Published Apr 8, 2025

Creates a new Account for your organization.

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 an Account
7
 * Creates a new Account for your organization.
8
 */
9
export async function main(
10
  auth: Persona,
11
  body: {
12
    data?: {
13
      attributes?: {
14
        "account-status"?: string;
15
        "reference-id"?: string;
16
        "selfie-photo"?: { data?: { data?: string; filename?: string } };
17
        tags?: string[];
18
        "country-code"?: string;
19
        "social-security-number"?: string;
20
        fields?: {};
21
        birthdate?: string;
22
        "name-first"?: string;
23
        "name-middle"?: string;
24
        "name-last"?: string;
25
        "phone-number"?: string;
26
        "email-address"?: string;
27
        "address-street-1"?: string;
28
        "address-street-2"?: string;
29
        "address-city"?: string;
30
        "address-subdivision"?: string;
31
        "address-postal-code"?: string;
32
      };
33
    };
34
    meta?: {};
35
  },
36
  include?: string,
37
  fields?: string,
38
  Key_Inflection?: string,
39
  Idempotency_Key?: string,
40
  Persona_Version?: string,
41
) {
42
  const url = new URL(`https://api.withpersona.com/api/v1/accounts`);
43
  for (const [k, v] of [
44
    ["include", include],
45
    ["fields", fields],
46
  ]) {
47
    if (v !== undefined && v !== "" && k !== undefined) {
48
      url.searchParams.append(k, v);
49
    }
50
  }
51
  const headers: Record<string, string> = {
52
    Authorization: `Bearer ${auth.apiKey}`,
53
    "Content-Type": "application/json",
54
  };
55
  if (Key_Inflection) {
56
    headers["Key-Inflection"] = Key_Inflection;
57
  }
58
  if (Idempotency_Key) {
59
    headers["Idempotency-Key"] = Idempotency_Key;
60
  }
61
  if (Persona_Version) {
62
    headers["Persona-Version"] = Persona_Version;
63
  }
64
  const response = await fetch(url, {
65
    method: "POST",
66
    headers,
67
    body: JSON.stringify(body),
68
  });
69
  if (!response.ok) {
70
    const text = await response.text();
71
    throw new Error(`${response.status} ${text}`);
72
  }
73
  return await response.json();
74
}
75