-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
193 lines (144 loc) · 4.47 KB
/
index.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
export type InstantlyLead = {
email: string;
first_name?: string;
last_name?: string;
company_name?: string;
personalization?: string;
phone?: string;
website?: string;
custom_variables?: Record<string, string>;
};
const getApiKey = () => {
const apiKey = process.env.INSTANTLY_API_KEY;
if(!apiKey) {
throw new Error('INSTANTLY_API_KEY environment variable is undefined');
}
return apiKey;
};
const ping = async () => {
// fetch
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const response = await fetch(`https://api.instantly.ai/api/v1/authenticate?api_key=${API_KEY}`, requestOptions)
.then(response => response.json())
.catch(error => console.log('Instantly error:', error));
return response;
};
const listCampaigns = async () => {
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const res = await fetch(`https://api.instantly.ai/api/v1/campaign/list?api_key=${API_KEY}&skip=0&limit=0`, requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res;
};
const getCampaignName = async (campaignId: string) => {
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const res = await fetch(`https://api.instantly.ai/api/v1/campaign/get/name?api_key=${API_KEY}&campaign_id=${campaignId}`, requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res;
};
const getCampaignStatus = async (campaignId: string) => {
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const res = await fetch(`https://api.instantly.ai/api/v1/campaign/get/status?api_key=${API_KEY}&campaign_id=${campaignId}`, requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res;
};
const getCampaignAccounts = async (campaignId: string) => {
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const res = await fetch(`https://api.instantly.ai/api/v1/campaign/get/accounts?api_key=${API_KEY}&campaign_id=${campaignId}`, requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res;
};
const getCampaignSummary = async (campaignId: string) => {
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const res = await fetch(`https://api.instantly.ai/api/v1/analytics/campaign/summary?api_key=${API_KEY}&campaign_id=${campaignId}`, requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res;
};
const addLeadsToCampaign = async (campaignId: string, leads: InstantlyLead[], options: {
skipIfInWorkspace?: boolean;
}) => {
if(!campaignId) {
throw new Error('campaignId is undefined');
}
if(!Array.isArray(leads)) {
throw new Error('leads is not an array');
}
if(leads.length === 0) {
throw new Error('leads is empty');
}
const API_KEY = getApiKey();
const raw = JSON.stringify({
api_key: API_KEY,
campaign_id: campaignId,
skip_if_in_workspace: options.skipIfInWorkspace ?? false,
leads
});
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: raw,
redirect: 'follow'
} as RequestInit;
const res = await fetch('https://api.instantly.ai/api/v1/lead/add', requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res;
};
const listAccounts = async () => {
const requestOptions = {
method: 'GET',
redirect: 'follow'
} as RequestInit;
const API_KEY = getApiKey();
const res = await fetch(`https://api.instantly.ai/api/v1/account/list?api_key=${API_KEY}`, requestOptions)
.then(response => response.json())
.catch(error => console.log('error', error));
return res?.accounts;
};
const instantly = {
ping,
campaigns: {
list: listCampaigns,
getName: getCampaignName,
getStatus: getCampaignStatus,
getAccounts: getCampaignAccounts,
getSummary: getCampaignSummary
},
leads: {
addToCampaign: addLeadsToCampaign
},
accounts: {
list: listAccounts
}
};
export default instantly;