Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ Email Marketing:

General API:

- Templates CRUD – [`templates/everything.ts`](examples/templates/everything.ts)
- Templates CRUD, paginated (experimental) – [`templates/everything.ts`](examples/templates/everything.ts)
- Email templates CRUD (`/api/email_templates`) – [`templates/email-templates.ts`](examples/templates/email-templates.ts)
- Suppressions (create, find & delete) – [`sending/suppressions.ts`](examples/sending/suppressions.ts)
- Tracking Opt-outs (list, create & delete) – [`sending/tracking-opt-outs.ts`](examples/sending/tracking-opt-outs.ts)
- Billing info – [`general/billing.ts`](examples/general/billing.ts)
Expand All @@ -299,3 +300,12 @@ Everyone interacting in the Mailtrap project's codebases, issue trackers, chat r

Versions of this package up to 2.0.2 were an [unofficial client](https://github.com/vchin/mailtrap-client) developed by [@vchin](https://github.com/vchin). Package version 3 is a completely new package.

### Upgrading to 5.0

`client.templates` now calls the paginated `/api/templates` endpoints instead of `/api/email_templates`:

- `getList()` takes optional `{ token, per_page }` and resolves `{ data, pagination }` instead of a bare array. Pages hold at most 100 templates; pass `pagination.next_token` with the same `per_page` for the rest.
- `get`, `create` and `update` resolve `{ data }` instead of the bare template. `body_html` and `body_text` are `null` when the template has no such body.
- `create` and `update` send the fields as a flat body, so `category` is required on `create` while `body_html` and `body_text` are optional.

The `/api/templates` endpoints are experimental, and their shapes may change in a minor release before general availability. To keep the 4.x behavior, rename `client.templates` to `client.emailTemplates`: it calls the stable `/api/email_templates` endpoints with the same methods and shapes.
44 changes: 44 additions & 0 deletions examples/templates/email-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { MailtrapClient } from "mailtrap";

const TOKEN = "<YOUR-TOKEN-HERE>";
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>";

const client = new MailtrapClient({
token: TOKEN,
accountId: ACCOUNT_ID
});

async function emailTemplatesFlow() {
// Create a new template
const newTemplate = await client.emailTemplates.create({
name: "Welcome Email",
subject: "Welcome to Our Service!",
category: "Promotional",
body_html: "<h1>Welcome!</h1><p>Thank you for joining our service.</p>",
body_text: "Welcome! Thank you for joining our service."
});
console.log("Created template:", newTemplate);

// Get all templates
const allTemplates = await client.emailTemplates.getList();
console.log("All templates:", allTemplates);

// Get a specific template
const template = await client.emailTemplates.get(newTemplate.id);
console.log("Template details:", template);

// Update the template
const updatedTemplate = await client.emailTemplates.update(newTemplate.id, {
name: "Updated Welcome Email",
subject: "Welcome to Our Amazing Service!",
body_html: "<h1>Welcome!</h1><p>Thank you for joining our amazing service.</p>"
});
console.log("Updated template:", updatedTemplate);

// Delete the template
await client.emailTemplates.delete(newTemplate.id);
console.log("Template deleted successfully");
}

emailTemplatesFlow().catch(console.error);

25 changes: 16 additions & 9 deletions examples/templates/everything.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { MailtrapClient } from "mailtrap";

// The /api/templates endpoints are experimental: their request and response
// shapes may change before general availability.

const TOKEN = "<YOUR-TOKEN-HERE>";
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>";

Expand All @@ -17,26 +20,30 @@ async function templatesFlow() {
body_html: "<h1>Welcome!</h1><p>Thank you for joining our service.</p>",
body_text: "Welcome! Thank you for joining our service."
});
console.log("Created template:", newTemplate);
console.log("Created template:", newTemplate.data);

// Get all templates
const allTemplates = await client.templates.getList();
console.log("All templates:", allTemplates);
// List every template, one page at a time (page-token pagination)
let token: number | null = 1;
while (token !== null) {
const page = await client.templates.getList({ per_page: 50, token });
console.log("Templates:", page.data);
token = page.pagination.next_token;
}

// Get a specific template
const template = await client.templates.get(newTemplate.id);
console.log("Template details:", template);
const template = await client.templates.get(newTemplate.data.id);
console.log("Template details:", template.data);

// Update the template
const updatedTemplate = await client.templates.update(newTemplate.id, {
const updatedTemplate = await client.templates.update(newTemplate.data.id, {
name: "Updated Welcome Email",
subject: "Welcome to Our Amazing Service!",
body_html: "<h1>Welcome!</h1><p>Thank you for joining our amazing service.</p>"
});
console.log("Updated template:", updatedTemplate);
console.log("Updated template:", updatedTemplate.data);

// Delete the template
await client.templates.delete(newTemplate.id);
await client.templates.delete(newTemplate.data.id);
console.log("Template deleted successfully");
}

Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/lib/api/EmailTemplates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from "axios";

import EmailTemplatesBaseAPI from "../../../lib/api/EmailTemplates";

describe("lib/api/EmailTemplates: ", () => {
const accountId = 100;
const emailTemplatesAPI = new EmailTemplatesBaseAPI(axios, accountId);

describe("class EmailTemplatesBaseAPI(): ", () => {
describe("init: ", () => {
it("initializes with all necessary params.", () => {
expect(emailTemplatesAPI).toHaveProperty("create");
expect(emailTemplatesAPI).toHaveProperty("getList");
expect(emailTemplatesAPI).toHaveProperty("get");
expect(emailTemplatesAPI).toHaveProperty("update");
expect(emailTemplatesAPI).toHaveProperty("delete");
});
});
});
});
Loading
Loading