Skip to content

Template api integration #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ Refer to the [`examples`](examples) folder for the source code of this and other

### Contacts API

- [Contacts](examples/contacts/everything.ts)
- [Contacts CRUD](examples/contacts/everything.ts)

### Contact Lists API

- [Contact Lists](examples/contact-lists/everything.ts)
- [Contact Lists CRUD](examples/contact-lists/everything.ts)

### Templates API

- [Templates CRUD](examples/templates/everything.ts)

### Sending API

Expand All @@ -76,10 +80,10 @@ Refer to the [`examples`](examples) folder for the source code of this and other

### Batch Sending API

- [Send a batch of transactional emails](examples/batch/transactional.ts)
- [Send a batch of bulk emails](examples/batch/bulk.ts)
- [Send a batch of sandbox emails](examples/batch/sandbox.ts)
- [Send a batch of emails from template](examples/batch/template.ts)
- [Transactional emails](examples/batch/transactional.ts)
- [Bulk emails](examples/batch/bulk.ts)
- [Sandbox emails](examples/batch/sandbox.ts)
- [Emails from template](examples/batch/template.ts)

### Email testing API

Expand All @@ -92,6 +96,7 @@ Refer to the [`examples`](examples) folder for the source code of this and other
### Bulk sending API

- [Send mail](examples/bulk/send-mail.ts)
- [Transport](examples/bulk/transport.ts)

### Nodemailer Transport

Expand Down
44 changes: 44 additions & 0 deletions examples/templates/everything.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 templatesFlow() {
// Create a new template
const newTemplate = await client.templates.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.templates.getList();
console.log("All templates:", allTemplates);

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

// Update the template
const updatedTemplate = await client.templates.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.templates.delete(newTemplate.id);
console.log("Template deleted successfully");
}

templatesFlow().catch(console.error);

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

import TemplatesBaseAPI from "../../../lib/api/Templates";

describe("lib/api/Templates: ", () => {
const accountId = 100;
const templatesAPI = new TemplatesBaseAPI(axios, accountId);

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