1 | type Convertkit = { |
2 | apiSecret: string |
3 | } |
4 |
|
5 | export async function main( |
6 | resource: Convertkit, |
7 | tagId: string, |
8 | subscriberData: { |
9 | email: string |
10 | firstName?: string |
11 | fields?: { [key: string]: string } |
12 | } |
13 | ) { |
14 | const endpoint = `https://api.convertkit.com/v3/tags/${tagId}/subscribe` |
15 |
|
16 | const body = { |
17 | api_secret: resource.apiSecret, |
18 | email: subscriberData.email, |
19 | ...(subscriberData.firstName && { first_name: subscriberData.firstName }), |
20 | ...(subscriberData.fields && { fields: subscriberData.fields }) |
21 | } |
22 |
|
23 | const response = await fetch(endpoint, { |
24 | method: 'POST', |
25 | headers: { |
26 | 'Content-Type': 'application/json; charset=utf-8' |
27 | }, |
28 | body: JSON.stringify(body) |
29 | }) |
30 |
|
31 | if (!response.ok) { |
32 | throw new Error(`HTTP error! status: ${response.status}`) |
33 | } |
34 |
|
35 | const data = await response.json() |
36 |
|
37 | return data.subscription |
38 | } |
39 |
|