-
Notifications
You must be signed in to change notification settings - Fork 17
MT-22401: Add Email Campaigns API #148
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
Rabsztok
wants to merge
2
commits into
main
Choose a base branch
from
MT-22401-nodejs-email-campaigns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { MailtrapClient } from "mailtrap"; | ||
|
|
||
| const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
| // ID of a verified sending domain on the account (required to create a campaign), | ||
| // as returned by the Sending Domains endpoints. | ||
| const SENDING_DOMAIN_ID = "<YOUR-SENDING-DOMAIN-ID-HERE>"; | ||
|
|
||
| // The Email Campaigns API is token-scoped — no `accountId` is needed. | ||
| const client = new MailtrapClient({ token: TOKEN }); | ||
|
|
||
| async function emailCampaignsFlow() { | ||
| try { | ||
| const scheduledAt = new Date(Date.now() + 60 * 60 * 1000).toISOString(); | ||
|
|
||
| // List campaigns (newest first). The response is a `{ data, pagination }` | ||
| // envelope; pagination is page-token based. | ||
| const list = await client.emailCampaigns.getList({ | ||
| per_page: 50, | ||
| token: 1, | ||
| search: "spring", // filter by name | ||
| }); | ||
| console.log("Campaigns:", JSON.stringify(list.data, null, 2)); | ||
| console.log("Pagination:", JSON.stringify(list.pagination, null, 2)); | ||
|
|
||
| // Create a campaign. It starts in the `draft` state. Single-campaign | ||
| // responses are wrapped in a `{ data }` envelope. | ||
| const created = await client.emailCampaigns.create({ | ||
| name: "Spring Sale", | ||
| domain_id: Number(SENDING_DOMAIN_ID), | ||
| from_display_name: "Acme Marketing", | ||
| from_local_part: "news", | ||
| reply_to: { | ||
| display_name: "Acme Support", | ||
| local_part: "support", | ||
| domain: "acme.com", | ||
| }, | ||
| template_attributes: { subject: "Spring is here — 30% off" }, | ||
| }); | ||
| console.log("Created campaign:", JSON.stringify(created.data, null, 2)); | ||
|
|
||
| const campaignId = created.data.id; | ||
|
|
||
| // Get a single campaign by ID. | ||
| const one = await client.emailCampaigns.get(campaignId); | ||
| console.log("One campaign:", JSON.stringify(one.data, null, 2)); | ||
|
|
||
| // Update the campaign (PATCH — only the provided fields change). The | ||
| // template is edited in place; add the design and pick the audience via | ||
| // contact list/segment IDs. Sending can be throttled with `gradual` mode. | ||
| const updated = await client.emailCampaigns.update(campaignId, { | ||
| name: "Spring Sale (updated)", | ||
| template_attributes: { | ||
| subject: "Hi {{first_name}}, spring is here — 30% off", | ||
| body_html: | ||
| '<html><body><h1>Hi {{first_name}}!</h1><p><a href="__unsubscribe_url__">Unsubscribe</a></p></body></html>', | ||
| merge_tags: ["first_name"], | ||
| }, | ||
| contact_list_ids: [1], | ||
| delivery_mode: "gradual", | ||
| delivery_options: { emails_per_hour: 1000 }, | ||
| }); | ||
| console.log("Updated campaign:", JSON.stringify(updated.data, null, 2)); | ||
|
|
||
| // Schedule the draft to send later. The time is reported back in | ||
| // `current_state_metadata.scheduled_at`. | ||
| const scheduled = await client.emailCampaigns.schedule(campaignId, { | ||
| datetime: scheduledAt, | ||
| }); | ||
| console.log( | ||
| "Scheduled for:", | ||
| scheduled.data.current_state_metadata.scheduled_at | ||
| ); | ||
|
|
||
| // Cancel the scheduled send — the campaign returns to `draft`. | ||
| // (`reset` also returns a scheduled campaign to `draft`.) | ||
| const cancelled = await client.emailCampaigns.cancel(campaignId); | ||
| console.log("State after cancel:", cancelled.data.current_state); | ||
|
|
||
| // Start sending immediately. | ||
| const started = await client.emailCampaigns.start(campaignId); | ||
| console.log("State after start:", started.data.current_state); | ||
|
|
||
| // Terminate the in-flight send. | ||
| const terminated = await client.emailCampaigns.terminate(campaignId); | ||
| console.log("State after terminate:", terminated.data.current_state); | ||
|
|
||
| // Get aggregated stats for the campaign, optionally narrowed to a date | ||
| // window via `start_date`/`end_date`. Counts and rates are all `0` until | ||
| // the campaign has been started. | ||
| const stats = await client.emailCampaigns.getStats(campaignId); | ||
| console.log("Campaign stats:", JSON.stringify(stats.data, null, 2)); | ||
|
|
||
| // Delete the campaign. Returns nothing (204 No Content). | ||
| await client.emailCampaigns.delete(campaignId); | ||
| console.log("Deleted campaign:", campaignId); | ||
| } catch (error) { | ||
| console.error( | ||
| "Error in emailCampaignsFlow:", | ||
| error instanceof Error ? error.message : String(error) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| emailCampaignsFlow(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import axios from "axios"; | ||
|
|
||
| import EmailCampaignsBaseAPI from "../../../lib/api/EmailCampaigns"; | ||
|
|
||
| describe("lib/api/EmailCampaigns: ", () => { | ||
| const emailCampaignsAPI = new EmailCampaignsBaseAPI(axios); | ||
|
|
||
| describe("class EmailCampaignsBaseAPI(): ", () => { | ||
| describe("init: ", () => { | ||
| it("initializes with all necessary params.", () => { | ||
| expect(emailCampaignsAPI).toHaveProperty("getList"); | ||
| expect(emailCampaignsAPI).toHaveProperty("create"); | ||
| expect(emailCampaignsAPI).toHaveProperty("get"); | ||
| expect(emailCampaignsAPI).toHaveProperty("update"); | ||
| expect(emailCampaignsAPI).toHaveProperty("delete"); | ||
| expect(emailCampaignsAPI).toHaveProperty("start"); | ||
| expect(emailCampaignsAPI).toHaveProperty("schedule"); | ||
| expect(emailCampaignsAPI).toHaveProperty("cancel"); | ||
| expect(emailCampaignsAPI).toHaveProperty("terminate"); | ||
| expect(emailCampaignsAPI).toHaveProperty("reset"); | ||
| expect(emailCampaignsAPI).toHaveProperty("getStats"); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.