0

Update message attributes by ID

by
Published Oct 17, 2025

Updates attributes for 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 attributes by ID
7
 * Updates attributes for a message based on the unique message ID.
8
 */
9
export async function main(
10
  auth: Kustomer,
11
  id: string,
12
  body: {
13
    conversation?: string;
14
    externalId?: string;
15
    preview?: string;
16
    subject?: string;
17
    status?: "sent" | "received" | "error";
18
    error?: {
19
      status?: number;
20
      code?: string;
21
      title?: string;
22
      detail?: string;
23
      source?: {};
24
      meta?: {};
25
      links?: {};
26
    };
27
    errorAt?: string;
28
    sentAt?: string;
29
    size?: number;
30
    attachments?: {
31
      _id: string;
32
      name: string;
33
      contentType: string;
34
      contentLength: number;
35
      sourceId?: string;
36
    }[];
37
    meta?: {};
38
    custom?: {};
39
    related?: string;
40
    sentiment?: { polarity: 0 | 1 | -1; confidence: number };
41
    createdAt?: string;
42
    modifiedAt?: string;
43
    updatedAt?: string;
44
    createdBy?: string;
45
    modifiedBy?: string;
46
    lang?: string;
47
  },
48
) {
49
  const url = new URL(`https://api.kustomerapp.com/v1/messages/${id}`);
50

51
  const response = await fetch(url, {
52
    method: "PATCH",
53
    headers: {
54
      "Content-Type": "application/json",
55
      Authorization: "Bearer " + auth.apiKey,
56
    },
57
    body: JSON.stringify(body),
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65