1 | |
2 |
|
3 | |
4 | * Create Contact |
5 | * Create a contact from the common fields. Use Create CRM Object for any other property, or Create or Update Contact to upsert by email. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Hubspot, |
9 | company: string | undefined, |
10 | email: string | undefined, |
11 | firstname: string | undefined, |
12 | lastname: string | undefined, |
13 | phone: string | undefined, |
14 | website: string | undefined |
15 | ) { |
16 | const properties: { [key: string]: string } = {} |
17 | const fields = { company, email, firstname, lastname, phone, website } |
18 | for (const [key, value] of Object.entries(fields)) { |
19 | if (value != null && value !== "") properties[key] = value |
20 | } |
21 |
|
22 | const response = await fetch( |
23 | "https://api.hubapi.com/crm/v3/objects/contacts", |
24 | { |
25 | method: "POST", |
26 | headers: { |
27 | Authorization: `Bearer ${auth.token}`, |
28 | "Content-Type": "application/json", |
29 | Accept: "application/json", |
30 | }, |
31 | body: JSON.stringify({ properties }), |
32 | } |
33 | ) |
34 |
|
35 | if (!response.ok) { |
36 | throw new Error(`${response.status} ${await response.text()}`) |
37 | } |
38 |
|
39 | return await response.json() |
40 | } |
41 |
|