0

Update message by ID

by
Published Oct 17, 2025

Updates a message based on the unique message ID.

Script kustomer Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Kustomer = {
3
  apiKey: string;
4
};
5
/**
6
 * Update message by ID
7
 * Updates a message based on the unique message ID.
8
 */
9
export async function main(
10
  auth: Kustomer,
11
  id: string,
12
  replace: string | undefined,
13
  body: {
14
    conversation?: string;
15
    externalId?: string;
16
    preview?: string;
17
    subject?: string;
18
    status?: "sent" | "received" | "error";
19
    error?: {
20
      status?: number;
21
      code?: string;
22
      title?: string;
23
      detail?: string;
24
      source?: {};
25
      meta?: {};
26
      links?: {};
27
    };
28
    errorAt?: string;
29
    sentAt?: string;
30
    size?: number;
31
    attachments?: {
32
      _id: string;
33
      name: string;
34
      contentType: string;
35
      contentLength: number;
36
      sourceId?: string;
37
    }[];
38
    meta?: {};
39
    custom?: {};
40
    related?: string;
41
    sentiment?: { polarity: 0 | 1 | -1; confidence: number };
42
    createdAt?: string;
43
    modifiedAt?: string;
44
    updatedAt?: string;
45
    createdBy?: string;
46
    spam?: false | true;
47
    modifiedBy?: string;
48
    lang?: string;
49
    reactions?: {
50
      subjectId: string;
51
      subjectType: "customer" | "user";
52
      name: "love";
53
      createdAt?: string;
54
    }[];
55
  },
56
) {
57
  const url = new URL(`https://api.kustomerapp.com/v1/messages/${id}`);
58
  for (const [k, v] of [["replace", replace]]) {
59
    if (v !== undefined && v !== "") {
60
      url.searchParams.append(k, v);
61
    }
62
  }
63
  const response = await fetch(url, {
64
    method: "PUT",
65
    headers: {
66
      "Content-Type": "application/json",
67
      Authorization: "Bearer " + auth.apiKey,
68
    },
69
    body: JSON.stringify(body),
70
  });
71
  if (!response.ok) {
72
    const text = await response.text();
73
    throw new Error(`${response.status} ${text}`);
74
  }
75
  return await response.json();
76
}
77