0

Create a Government ID Document

by
Published Apr 8, 2025

Creates a new government ID document

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 Government ID Document
7
 * Creates a new government ID document
8
 */
9
export async function main(
10
  auth: Persona,
11
  body: {
12
    data: {
13
      attributes?: {
14
        "back-photo"?: { data?: { data: string; filename: string }[] };
15
        "country-code": string;
16
        "front-photo"?: { data?: { data: string; filename: string }[] };
17
        "inquiry-id": string;
18
        "account-id"?: string;
19
        "selected-country-code"?: string;
20
        "selected-id-class"?: string | 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/document/government-ids`,
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 headers: Record<string, string> = {
42
    Authorization: `Bearer ${auth.apiKey}`,
43
    "Content-Type": "application/json",
44
  };
45
  if (Key_Inflection) {
46
    headers["Key-Inflection"] = Key_Inflection;
47
  }
48
  if (Idempotency_Key) {
49
    headers["Idempotency-Key"] = Idempotency_Key;
50
  }
51
  if (Persona_Version) {
52
    headers["Persona-Version"] = Persona_Version;
53
  }
54
  const response = await fetch(url, {
55
    method: "POST",
56
    headers,
57
    body: JSON.stringify(body),
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65