0

Create draft for customer

by
Published Oct 17, 2025

Creates an outbound draft to a customer through a specific channel (email, SMS, Facebook DM, etc).

Script kustomer Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Kustomer = {
3
  apiKey: string;
4
};
5
/**
6
 * Create draft for customer
7
 * Creates an outbound draft to a customer through a specific channel (email, SMS, Facebook DM, etc).
8
 */
9
export async function main(
10
  auth: Kustomer,
11
  id: string,
12
  body:
13
    | {
14
        channel: "email";
15
        conversation?: string;
16
        app?: "gmail" | "postmark";
17
        customer?: string;
18
        auto?: false | true;
19
        sendAt?: string;
20
        scheduled?: false | true;
21
        source?: "bulk" | "satisfaction";
22
        lang?: string;
23
        shortcuts?: string[];
24
        kbArticles?: string[];
25
        to?:
26
          | { email: string; name?: string }[]
27
          | { email: string; name?: string };
28
        from?: { email: string; name?: string };
29
        body?: string;
30
        cc?: { email: string; name?: string }[];
31
        bcc?: { email: string; name?: string }[];
32
        subject?: string;
33
        replyTo?: string;
34
        headers?: { name: string; value?: string }[];
35
        template?: string;
36
        payload?: {};
37
      }
38
    | {
39
        channel: "sms";
40
        app?: "twilio" | "zipwhip";
41
        conversation?: string;
42
        customer?: string;
43
        auto?: false | true;
44
        sendAt?: string;
45
        scheduled?: false | true;
46
        source?: "bulk" | "satisfaction";
47
        lang?: string;
48
        shortcuts?: string[];
49
        kbArticles?: string[];
50
        to?: string;
51
        from?: string;
52
        body?: string;
53
        payload?: {};
54
      }
55
    | ({
56
        channel: "whatsapp";
57
        conversation?: string;
58
        customer?: string;
59
        auto?: false | true;
60
        sendAt?: string;
61
        source?: "bulk" | "satisfaction" | "biz-rules";
62
        lang?: string;
63
        shortcuts?: string[];
64
        kbArticles?: string[];
65
        to?: string;
66
        from?: string;
67
        payload?: {};
68
      } & { app: "whatsapp" | "twilio_whatsapp" | "messagebird"; body: string })
69
    | ({
70
        channel: "whatsapp";
71
        conversation?: string;
72
        customer?: string;
73
        auto?: false | true;
74
        sendAt?: string;
75
        source?: "bulk" | "satisfaction" | "biz-rules";
76
        lang?: string;
77
        shortcuts?: string[];
78
        kbArticles?: string[];
79
        to?: string;
80
        from?: string;
81
        payload?: {};
82
      } & {
83
        app: "whatsapp";
84
        meta: {
85
          interactive:
86
            | {
87
                type: "list";
88
                header?: { type: "text"; text: string };
89
                body: { text: string };
90
                footer?: { text: string };
91
                action: {
92
                  button: string;
93
                  sections: {
94
                    title?: string;
95
                    rows: { id: string; title: string; description?: string }[];
96
                  }[];
97
                };
98
              }
99
            | {
100
                type: "button";
101
                header?: { type: "text"; text: string };
102
                body: { text: string };
103
                footer?: { text: string };
104
                action: {
105
                  buttons: {
106
                    type: "reply";
107
                    reply: { id: string; title: string };
108
                  }[];
109
                };
110
              };
111
        };
112
      })
113
    | ({
114
        channel: "whatsapp";
115
        conversation?: string;
116
        customer?: string;
117
        auto?: false | true;
118
        sendAt?: string;
119
        source?: "bulk" | "satisfaction" | "biz-rules";
120
        lang?: string;
121
        shortcuts?: string[];
122
        kbArticles?: string[];
123
        to?: string;
124
        from?: string;
125
        payload?: {};
126
      } & {
127
        app: "whatsapp";
128
        meta: {
129
          template: string;
130
          components?: {
131
            header?:
132
              | { type: "text"; text: {} }
133
              | { type: "image" | "video" | "document"; attachment: string };
134
            body?: {};
135
            buttons?: { index: number; sub_type: "url" }[];
136
          };
137
        };
138
      })
139
    | {
140
        channel: "chat";
141
        conversation?: string;
142
        customer?: string;
143
        auto?: false | true;
144
        sendAt?: string;
145
        scheduled?: false | true;
146
        source?: "bulk" | "satisfaction";
147
        lang?: string;
148
        shortcuts?: string[];
149
        kbArticles?: string[];
150
        to?: string;
151
        from?: string;
152
        body?: string;
153
        payload?: {};
154
        attachments?: string[];
155
      }
156
    | {
157
        channel: "facebook";
158
        conversation?: string;
159
        customer?: string;
160
        auto?: false | true;
161
        sendAt?: string;
162
        scheduled?: false | true;
163
        source?: "bulk" | "satisfaction";
164
        lang?: string;
165
        shortcuts?: string[];
166
        kbArticles?: string[];
167
        to?: string;
168
        from?: string;
169
        body?: string;
170
        payload?: {};
171
      }
172
    | {
173
        channel: "twitter-tweet";
174
        conversation?: string;
175
        customer?: string;
176
        auto?: false | true;
177
        sendAt?: string;
178
        scheduled?: false | true;
179
        source?: "bulk" | "satisfaction";
180
        lang?: string;
181
        shortcuts?: string[];
182
        kbArticles?: string[];
183
        to?: string;
184
        from?: string;
185
        body?: string;
186
        payload?: {};
187
      }
188
    | {
189
        channel: "twitter-dm";
190
        conversation?: string;
191
        customer?: string;
192
        auto?: false | true;
193
        sendAt?: string;
194
        scheduled?: false | true;
195
        source?: "bulk" | "satisfaction";
196
        lang?: string;
197
        shortcuts?: string[];
198
        kbArticles?: string[];
199
        to?: string;
200
        from?: string;
201
        body?: string;
202
        payload?: {};
203
      }
204
    | {
205
        channel: "note";
206
        conversation?: string;
207
        customer?: string;
208
        sendAt?: string;
209
        scheduled?: false | true;
210
        source?: "bulk";
211
        lang?: string;
212
        shortcuts?: string[];
213
        kbArticles?: string[];
214
        body?: string;
215
        payload?: {};
216
        userMentions?: { user?: string; team?: string }[];
217
      }
218
    | {
219
        channel: "instagram";
220
        conversation?: string;
221
        customer?: string;
222
        auto?: false | true;
223
        sendAt?: string;
224
        scheduled?: false | true;
225
        source?: "bulk" | "satisfaction";
226
        lang?: string;
227
        shortcuts?: string[];
228
        kbArticles?: string[];
229
        to?: string;
230
        from?: string;
231
        body?: string;
232
        payload?: {};
233
      },
234
) {
235
  const url = new URL(`https://api.kustomerapp.com/v1/customers/${id}/drafts`);
236

237
  const response = await fetch(url, {
238
    method: "POST",
239
    headers: {
240
      "Content-Type": "application/json",
241
      Authorization: "Bearer " + auth.apiKey,
242
    },
243
    body: JSON.stringify(body),
244
  });
245
  if (!response.ok) {
246
    const text = await response.text();
247
    throw new Error(`${response.status} ${text}`);
248
  }
249
  return await response.json();
250
}
251