aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docs/lib/ConvertKitApi.ts
blob: c2915a1307608374fdb7331693c9e93f539b05a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import axios from "axios";

const API_KEY = process.env.CONVERTKIT_API_KEY;
const API_SECRET = process.env.CONVERTKIT_API_SECRET;

const Http = axios.create({
  baseURL: "https://api.convertkit.com/v3",
  headers: {
    "Content-Type": "application/json; charset=utf-8",
  },
});

export function subscribeToForm({
  formId,
  email,
  firstName,
  fields,
}: {
  formId: string;
  email: string;
  firstName: string;
  fields?: Record<string, any>;
}): Promise<Subscriber> {
  return Http(`/forms/${formId}/subscribe`, {
    method: "POST",
    data: { api_key: API_KEY, email, first_name: firstName, fields },
  }).then((res) => res.data.subscription?.subscriber);
}

export function updateSubscriber(
  id: string,
  update: Subscriber
): Promise<unknown> {
  return Http(`/subscribers/${id}`, {
    method: "PUT",
    data: {
      api_secret: API_SECRET,
      ...update,
    },
  }).then((res) => res.data);
}

export interface Subscriber {
  id: number;
  first_name: string;
  email_address: string;
  state: string; // maybe 'active' | 'inactive'
  created_at: string;
  fields: Record<string, any>;
}

export function getSubscriber(id: string): Promise<Subscriber> {
  return Http(`/subscribers/${id}`, {
    method: "GET",
    data: {
      api_secret: API_SECRET,
    },
  }).then((res) => res.data.subscriber);
}