1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create an Inquiry |
7 | * Creates a new inquiry with optional pre-filled attributes. |
8 |
|
9 | See [Sessions](https://docs.withpersona.com/docs/inquiry-sessions) for how to continue the inquiry in [Embedded Flow](https://docs.withpersona.com/docs/embedded-flow) or [Hosted Flow](https://docs.withpersona.com/docs/hosted-flow). |
10 | */ |
11 | export async function main( |
12 | auth: Persona, |
13 | body: { |
14 | data: { |
15 | attributes: { |
16 | "template-id"?: string; |
17 | "inquiry-template-id"?: string; |
18 | "inquiry-template-version-id"?: string; |
19 | "reference-id"?: string; |
20 | "account-id"?: string; |
21 | "creator-email-address"?: string; |
22 | "theme-id"?: string; |
23 | "theme-set-id"?: string; |
24 | "redirect-uri"?: string; |
25 | note?: string; |
26 | fields?: |
27 | | {} |
28 | | ({ |
29 | birthdate?: string; |
30 | "name-first"?: string; |
31 | "name-middle"?: string; |
32 | "name-last"?: string; |
33 | "phone-number"?: string; |
34 | "email-address"?: string; |
35 | } & { |
36 | "address-street-1"?: string; |
37 | "address-street-2"?: string; |
38 | "address-city"?: string; |
39 | "address-subdivision"?: string; |
40 | "address-postal-code"?: string; |
41 | } & { "address-country-code"?: string }); |
42 | tags?: string[]; |
43 | "initial-step-name"?: string; |
44 | }; |
45 | }; |
46 | meta?: { |
47 | "auto-create-account"?: false | true; |
48 | "auto-create-account-type-id"?: string; |
49 | "auto-create-account-reference-id"?: string; |
50 | "expiration-after-create-interval-seconds"?: number; |
51 | "expiration-after-start-interval-seconds"?: number; |
52 | "expiration-after-resume-interval-seconds"?: number; |
53 | "one-time-link-expiration-seconds"?: number; |
54 | }; |
55 | }, |
56 | include?: string, |
57 | fields?: string, |
58 | Key_Inflection?: string, |
59 | Idempotency_Key?: string, |
60 | Persona_Version?: string, |
61 | ) { |
62 | const url = new URL(`https://api.withpersona.com/api/v1/inquiries`); |
63 | for (const [k, v] of [ |
64 | ["include", include], |
65 | ["fields", fields], |
66 | ]) { |
67 | if (v !== undefined && v !== "" && k !== undefined) { |
68 | url.searchParams.append(k, v); |
69 | } |
70 | } |
71 | const headers: Record<string, string> = { |
72 | Authorization: `Bearer ${auth.apiKey}`, |
73 | "Content-Type": "application/json", |
74 | }; |
75 | if (Key_Inflection) { |
76 | headers["Key-Inflection"] = Key_Inflection; |
77 | } |
78 | if (Idempotency_Key) { |
79 | headers["Idempotency-Key"] = Idempotency_Key; |
80 | } |
81 | if (Persona_Version) { |
82 | headers["Persona-Version"] = Persona_Version; |
83 | } |
84 | const response = await fetch(url, { |
85 | method: "POST", |
86 | headers, |
87 | body: JSON.stringify(body), |
88 | }); |
89 | if (!response.ok) { |
90 | const text = await response.text(); |
91 | throw new Error(`${response.status} ${text}`); |
92 | } |
93 | return await response.json(); |
94 | } |
95 |
|