Skip to content

Commit ab2c7fe

Browse files
committed
Add Stats API
1 parent 484d833 commit ab2c7fe

7 files changed

Lines changed: 590 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
## [Unreleased]
2+
3+
- Add StatsApi with get, byDomain, byCategory, byEmailServiceProvider, byDate endpoints
4+
15
## [4.4.0] - 2025-12-08
6+
27
- Add ES module support by specifying import path in package.json by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/112
38
- Bump new minor version as previous changes were pretty huge and shouldn't be released under patch version
49

510
## [4.3.2] - 2025-11-27
11+
612
- Rollback to v4.3.0
713

814
## [4.3.1] - 2025-11-25
15+
916
- Contact fields by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/89
1017
- Fix optional account by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/91
1118
- build(deps-dev): bump nodemailer from 6.9.9 to 7.0.7 by @dependabot[bot] in https://github.com/mailtrap/mailtrap-nodejs/pull/92
@@ -14,21 +21,22 @@
1421
- Fix #84 by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/96
1522
- Fix #82 by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/97
1623
- Contact imports by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/95
17-
- Contact exports by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/98
24+
- Contact exports by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/98
1825
- Create contact event by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/99
1926
- Billing api by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/103
2027
- Fix axios error parsing by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/104
2128
- Update README.md to enhance installation and usage instructions, impr… by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/107
2229
- Fix 106 by @narekhovhannisyan in https://github.com/mailtrap/mailtrap-nodejs/pull/108
2330

24-
2531
## [4.3.0] - 2025-10-02
32+
2633
- feat: add Permissions type to projects API for enhanced access control by @narekhovhannisyan in https://github.com/railsware/mailtrap-nodejs/pull/80
2734
- update readme by @yanchuk in https://github.com/railsware/mailtrap-nodejs/pull/66
2835
- build(deps): bump axios from 1.8.2 to 1.12.0 by @dependabot[bot] in https://github.com/railsware/mailtrap-nodejs/pull/86
2936
- Dynamic user agent by @narekhovhannisyan in https://github.com/railsware/mailtrap-nodejs/pull/85
3037

3138
## [4.2.0] - 2025-07-08
39+
3240
- Add support for [Batch Sending API](https://github.com/railsware/mailtrap-nodejs/pull/63).
3341
- Add support for [Contacts API](https://github.com/railsware/mailtrap-nodejs/pull/64).
3442
- Add support for [Contact Lists API](https://github.com/railsware/mailtrap-nodejs/pull/65).
@@ -37,27 +45,32 @@
3745
- Make `testInboxId` optional in the `MailtrapClient` configuration (https://github.com/railsware/mailtrap-nodejs/pull/70).
3846

3947
## [4.1.0] - 2025-04-18
48+
4049
- Add support for `reply_to` in Sending API (in https://github.com/railsware/mailtrap-nodejs/pull/58, thanks to @aolamide).
4150

4251
## [4.0.0] - 2025-02-28
52+
4353
- BREAKING CHANGE: Missing params for the Testing API (here) are treated as errors (throw new Error(...)), not warnings.
4454
- BREAKING CHANGE: Removes send methods from the `BulkSendingAPI` and `TestingAPI` classes. There should be only one send method on the `MailtrapClient`.
4555
- The `general` and `testing` APIs are created lazily, after the first access to the corresponding getters.
4656
- Updated the MailtrapClient to accept two more params: bulk: Boolean and sandbox: Boolean. They are changing the behavior of the send method of the client in the following way:
4757
- Security updates for dependencies
4858

4959
## [3.4.0] - 2024-06-10
60+
5061
- Add support for Bulk product API.
5162
- Refer to the [`examples/bulk`](examples/bulk) folder for code examples.
5263
- Restructure examples folder.
5364

5465
## [3.3.0] - 2024-01-31
66+
5567
- Add support for Testing product API.
5668
- All public routes from API V2 are now available in SDK.
5769
- Refer to the [`examples`](examples) folder for code examples.
5870
- Security updates.
5971

6072
## [3.2.0] - 2023-08-30
73+
6174
- Add `mailtrap-nodemailer-transporter` for sending emails using HTTP API via `nodemailer`.
6275
- Security updates.
6376

examples/general/stats.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { MailtrapClient } from "mailtrap"
2+
3+
const TOKEN = "<YOUR-TOKEN-HERE>";
4+
const ACCOUNT_ID = 123456
5+
6+
const client = new MailtrapClient({ token: TOKEN, accountId: ACCOUNT_ID });
7+
8+
const statsClient = client.general.stats
9+
10+
const testGetStats = async () => {
11+
try {
12+
const result = await statsClient.get({
13+
start_date: "2026-01-01",
14+
end_date: "2026-01-31",
15+
})
16+
console.log("Stats:", JSON.stringify(result, null, 2))
17+
} catch (error) {
18+
console.error(error)
19+
}
20+
}
21+
22+
const testGetStatsWithFilters = async () => {
23+
try {
24+
const result = await statsClient.get({
25+
start_date: "2026-01-01",
26+
end_date: "2026-01-31",
27+
sending_domain_ids: [1, 2],
28+
sending_streams: ["transactional"],
29+
categories: ["Transactional", "Marketing"],
30+
email_service_providers: ["Gmail", "Yahoo"],
31+
})
32+
console.log("Filtered stats:", JSON.stringify(result, null, 2))
33+
} catch (error) {
34+
console.error(error)
35+
}
36+
}
37+
38+
const testGetStatsByDomains = async () => {
39+
try {
40+
const result = await statsClient.byDomain({
41+
start_date: "2026-01-01",
42+
end_date: "2026-01-31",
43+
})
44+
console.log("Stats by domains:", JSON.stringify(result, null, 2))
45+
} catch (error) {
46+
console.error(error)
47+
}
48+
}
49+
50+
const testGetStatsByCategories = async () => {
51+
try {
52+
const result = await statsClient.byCategory({
53+
start_date: "2026-01-01",
54+
end_date: "2026-01-31",
55+
})
56+
console.log("Stats by categories:", JSON.stringify(result, null, 2))
57+
} catch (error) {
58+
console.error(error)
59+
}
60+
}
61+
62+
const testGetStatsByEmailServiceProviders = async () => {
63+
try {
64+
const result = await statsClient.byEmailServiceProvider({
65+
start_date: "2026-01-01",
66+
end_date: "2026-01-31",
67+
})
68+
console.log("Stats by email service providers:", JSON.stringify(result, null, 2))
69+
} catch (error) {
70+
console.error(error)
71+
}
72+
}
73+
74+
const testGetStatsByDate = async () => {
75+
try {
76+
const result = await statsClient.byDate({
77+
start_date: "2026-01-01",
78+
end_date: "2026-01-31",
79+
})
80+
console.log("Stats by date:", JSON.stringify(result, null, 2))
81+
} catch (error) {
82+
console.error(error)
83+
}
84+
}
85+
86+
(async () => {
87+
try {
88+
await testGetStats()
89+
await testGetStatsWithFilters()
90+
await testGetStatsByDomains()
91+
await testGetStatsByCategories()
92+
await testGetStatsByEmailServiceProviders()
93+
await testGetStatsByDate()
94+
} catch (error) {
95+
console.error("Error running stats examples:", error)
96+
}
97+
})()

0 commit comments

Comments
 (0)