Skip to content

Commit fa1fb87

Browse files
feat: add example script for creating and fetching contact exports using MailtrapClient
1 parent 138bd48 commit fa1fb87

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { MailtrapClient } from "mailtrap";
2+
3+
const TOKEN = "<YOUR-TOKEN-HERE>";
4+
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>";
5+
6+
const client = new MailtrapClient({
7+
token: TOKEN,
8+
accountId: Number(ACCOUNT_ID),
9+
});
10+
11+
async function createContactExport() {
12+
try {
13+
// Get contact lists and use first one if available
14+
const lists = await client.contactLists.getList();
15+
const listId = Array.isArray(lists) && lists.length > 0 ? lists[0].id : undefined;
16+
17+
// Create filters array per API docs:
18+
// - Use list_id filter with array of list IDs if list available
19+
// - Add subscription_status filter to export only subscribed contacts
20+
const filters = listId
21+
? [
22+
{ name: "list_id", operator: "equal" as const, value: [listId] },
23+
{ name: "subscription_status", operator: "equal" as const, value: "subscribed" },
24+
]
25+
: [
26+
{ name: "subscription_status", operator: "equal" as const, value: "subscribed" },
27+
];
28+
29+
const created = await client.contactExports.create({ filters });
30+
console.log("Export created:", JSON.stringify(created, null, 2));
31+
32+
// Fetch export to check status and get download URL when finished
33+
const fetched = await client.contactExports.get(created.id);
34+
console.log("Export fetched:", JSON.stringify(fetched, null, 2));
35+
} catch (error) {
36+
console.error(
37+
"Error creating contact export:",
38+
error instanceof Error ? error.message : String(error)
39+
);
40+
}
41+
}
42+
43+
createContactExport();
44+
45+

0 commit comments

Comments
 (0)