0

Bulk create messages

by
Published Oct 17, 2025

Creates a bulk batch of messages. Any one of the following roles is required for this endpoint: |Legacy Role|Equivalent Permission Set Role| |-----|--------| |org.user.message.write|org.permission.message.create|

Script kustomer Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Kustomer = {
3
  apiKey: string;
4
};
5
/**
6
 * Bulk create messages
7
 * Creates a bulk batch of messages.
8

9
Any one of the following roles is required for this endpoint:
10

11
|Legacy Role|Equivalent Permission Set Role|
12
|-----|--------|
13
|org.user.message.write|org.permission.message.create|
14
 */
15
export async function main(
16
  auth: Kustomer,
17
  body: {
18
    id?: string;
19
    externalId?: string;
20
    customer?: string;
21
    conversation?: string;
22
    reference?: { message: string; type: "reply" | "replyAll" | "forward" };
23
    channel:
24
      | "email"
25
      | "sms"
26
      | "chat"
27
      | "facebook"
28
      | "twitter-dm"
29
      | "twitter-tweet"
30
      | "voice"
31
      | "instagram"
32
      | "whatsapp";
33
    preview?: string;
34
    subject?: string;
35
    direction?: "in" | "out";
36
    app: string;
37
    size?: number;
38
    related?: string;
39
    status?: "sent" | "received" | "error";
40
    error?: {
41
      status?: number;
42
      code?: string;
43
      title?: string;
44
      detail?: string;
45
      source?: {};
46
      meta?: {};
47
      links?: {};
48
    };
49
    errorAt?: string;
50
    auto?: false | true;
51
    sentAt?: string;
52
    source?: "bulk" | "satisfaction";
53
    shortcuts?: string[];
54
    kbArticles?: string[];
55
    attachments?:
56
      | string[]
57
      | {
58
          _id: string;
59
          name: string;
60
          contentType: string;
61
          contentLength: number;
62
          sourceId?: string;
63
        }[];
64
    location?: {
65
      name?: string;
66
      address?: string;
67
      address2?: string;
68
      address3?: string;
69
      latitude?: number;
70
      longitude?: number;
71
      countryCode?: string;
72
      countryName?: string;
73
      regionCode?: string;
74
      regionName?: string;
75
      cityName?: string;
76
      zipCode?: string;
77
      areaCode?: string;
78
    };
79
    meta?: {};
80
    custom?: {};
81
    sentiment?: { polarity: 0 | 1 | -1; confidence: number };
82
    createdAt?: string;
83
    modifiedAt?: string;
84
    createdBy?: string;
85
    modifiedBy?: string;
86
    importedAt?: string;
87
    lang?: string;
88
    queue?: {} | {};
89
  }[],
90
) {
91
  const url = new URL(`https://api.kustomerapp.com/v1/messages/bulk`);
92

93
  const response = await fetch(url, {
94
    method: "POST",
95
    headers: {
96
      "Content-Type": "application/json",
97
      Authorization: "Bearer " + auth.apiKey,
98
    },
99
    body: JSON.stringify(body),
100
  });
101
  if (!response.ok) {
102
    const text = await response.text();
103
    throw new Error(`${response.status} ${text}`);
104
  }
105
  return await response.json();
106
}
107