-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy paththreads.ts
More file actions
47 lines (38 loc) · 1.27 KB
/
Copy paththreads.ts
File metadata and controls
47 lines (38 loc) · 1.27 KB
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
import { MailtrapClient } from "mailtrap";
const TOKEN = "<YOUR-TOKEN-HERE>";
const INBOX_ID = Number("<YOUR-INBOX-ID-HERE>");
// Set to a real thread id to try deletion below. Left empty, the example is
// read-only (list + get).
const THREAD_ID = "";
// Inbound is scoped to the token's account, so no accountId is required.
const client = new MailtrapClient({ token: TOKEN });
const threadsClient = client.inbound.threads;
async function threadsFlow() {
try {
const list = await threadsClient.getList(INBOX_ID);
console.log("Threads:", list);
// Fetch the next page with the returned cursor.
if (list.last_id) {
console.log(
"Next page:",
await threadsClient.getList(INBOX_ID, { last_id: list.last_id })
);
}
if (list.data.length > 0) {
console.log(
"First thread:",
await threadsClient.get(INBOX_ID, list.data[0].id)
);
}
if (!THREAD_ID) {
console.log("Set THREAD_ID above to try deletion.");
return;
}
// Deletes a real thread — only run against one you own.
await threadsClient.delete(INBOX_ID, THREAD_ID);
console.log("Deleted thread", THREAD_ID);
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
}
}
threadsFlow();