尚未偵測到更新。GitHub 發布版本後會顯示在這裡。
個人 GitHub 帳號下公開儲存庫的擁有者可以登入並驗證所有權,自行管理介紹,無需管理員核准。
登入並產生驗證檔案,在 30 分鐘內將其提交至此儲存庫的預設分支。其他分支或分叉儲存庫中的檔案無效。然後在下方檢查檔案,即可無需管理員核准直接管理專案。請保留檔案以維持管理權限。
如需登入,將前往登入頁面。登入後請回到此處,再次點擊所選的驗證按鈕。
在專案資料夾中執行 /nomorevibe verify,即可驗證所有權並自行管理介紹。
一分鐘完成驗證Mailry SDK Library for JavaScript / TypeScript
翻譯準備中
取自儲存庫 README
正在載入完整 README。
檢視完整 README ↗The official Node.js and TypeScript client for the Mailry.co public API. Send email from your mailboxes, read and manage the messages they hold, upload attachments and look up your domains and email accounts.
fetch, FormData and Blob)Node.js 18 or later. It also runs on Bun, Deno and edge runtimes that provide fetch.
npm install mailry
# or
pnpm add mailry
# or
yarn add mailry
# or
bun add mailry
Create an API key in the Mailry dashboard, then:
import { Mailry } from 'mailry';
const mailry = new Mailry('your_api_key');
const { data: accounts } = await mailry.emailAccounts.list();
await mailry.inbox.send({
emailId: accounts[0].id,
to: '[email protected]',
subject: 'Hello from Mailry',
plainBody: 'Hello!',
htmlBody: '<p>Hello!</p>',
});
CommonJS:
const { Mailry } = require('mailry');
If you leave out the API key, the client reads it from the MAILRY_API_KEY environment variable:
const mailry = new Mailry();
Keep your API key on the server. Never ship it in browser code.
const mailry = new Mailry({
apiKey: 'your_api_key',
baseUrl: 'https://api.mailry.co',
timeout: 60_000,
maxRetries: 2,
headers: { 'X-Request-Source': 'billing-service' },
fetch: customFetch,
});
| Option | Default | Description |
|---|---|---|
apiKey | process.env.MAILRY_API_KEY | Your Mailry API key |
baseUrl | process.env.MAILRY_BASE_URL or https://api.mailry.co | API base URL, for example http://localhost:4000 |
timeout | 60000 | Request timeout in milliseconds |
maxRetries | 2 | Retries for rate limited or transient failures |
headers | {} | Extra headers sent with every request |
fetch | globalThis.fetch | Custom fetch implementation |
Every method also takes an optional last argument to override these per request:
const controller = new AbortController();
await mailry.domains.list({}, { timeout: 5_000, maxRetries: 0, signal: controller.signal });
POST /public/inbox/send
const result = await mailry.inbox.send({
emailId: 'b3f1c2d4-...',
to: ['[email protected]', 'John <[email protected]>'],
subject: 'Weekly report',
plainBody: 'The report is attached.',
htmlBody: '<p>The report is attached.</p>',
});
console.log(result.message);
| Field | Type | Required | Description |
|---|---|---|---|
emailId | string | Yes | ID of the mailbox you send from (see emailAccounts.list()) |
to | string | string[] | Yes | Recipients, up to 50 per request |
cc | string | string[] | No | Carbon copy recipients |
subject | string | Yes | Subject line |
plainBody | string | One of | Plain text body |
htmlBody | string | One of | HTML body |
attachments | Attachment[] | No | Up to 10 files, 5MB each: doc, docx, pdf, xlsx, xls, txt |
At least one of plainBody or htmlBody is required. Each call counts against your daily API send quota.
An attachment is either a File or an object with a filename and content. content can be a Buffer, Uint8Array, ArrayBuffer, Blob or string. The content type is inferred from the file extension unless you set contentType.
import { readFile } from 'node:fs/promises';
await mailry.inbox.send({
emailId,
to: '[email protected]',
subject: 'Your invoice',
htmlBody: '<p>Your invoice is attached.</p>',
attachments: [
{ filename: 'invoice.pdf', content: await readFile('./invoice.pdf') },
{ filename: 'notes.txt', content: 'Thanks for your business!' },
{ filename: 'data.xlsx', content: spreadsheetBuffer, contentType: 'application/vnd.ms-excel' },
],
});
POST /public/inbox/upload-attachment
Upload an office document (doc, docx, xls, xlsx, ppt, pptx, pdf, up to 25MB) ahead of time and get back its ID.
const { data } = await mailry.inbox.uploadAttachment({
filename: 'contract.pdf',
content: await readFile('./contract.pdf'),
});
console.log(data.id, data.fileName, data.fileSize);
GET /public/inbox
Messages come newest first, one row per conversation. With search, which matches subject, sender and body, each matching message is returned on its own.
import { MessageFolder } from 'mailry';
const { data, pagination } = await mailry.inbox.list({
emailId: 'b3f1c2d4-...',
folder: MessageFolder.Inbox,
isStarred: true,
search: 'invoice',
page: 1,
limit: 20,
});
for (const message of data) {
console.log(message.id, message.from, message.subject, message.isReaded);
}
| Field | Type | Description |
|---|---|---|
emailId | string | Only messages of this mailbox |
domainId | string | Only messages of mailboxes on this domain |
folder | MessageFolder | Inbox (1), Sent (2), Draft (3) or Spam (4) |
isStarred | boolean | true returns only starred messages |
search | string | Matches subject, sender and body |
page | number | Page number, starting at 1 |
limit | number | Page size, up to 100 |
GET /public/inbox/{id}
Returns one message with its bodies and attachments, and marks it as read.
const { data: message } = await mailry.inbox.get(messageId);
console.log(message.subject, message.textBody);
for (const attachment of message.attachments ?? []) {
console.log(attachment.originalFileName, attachment.fileUrl);
}
Each attachment's fileUrl is a signed link that expires after a short time. Download it right away rather than storing it.
GET /public/inbox/{id}/thread
Returns every message in the same conversation as the given one, oldest first, and marks them as read.
const { data: messages } = await mailry.inbox.thread(messageId);
PATCH /public/inbox/star
Star or unstar up to 100 messages at a time. Returns how many were updated.
await mailry.inbox.star([firstId, secondId]);
const { data } = await mailry.inbox.star(firstId, false);
console.log(data.updated);
PATCH /public/inbox/spam
Move up to 100 received messages to the Spam folder, or pass false to move them back to the Inbox. Sent and draft messages are left untouched. Returns how many were moved.
await mailry.inbox.markSpam(messageId);
await mailry.inbox.markSpam(messageId, false);
DELETE /public/inbox
Permanently delete messages and their attachments, freeing the storage they used.
await mailry.inbox.delete([firstId, secondId]);
DELETE /public/inbox/attachment/{id}
Delete an attachment you uploaded and no longer need, or one on a stored message.
const { data: uploaded } = await mailry.inbox.uploadAttachment(file);
await mailry.inbox.deleteAttachment(uploaded.id);
GET /public/email
const { data, pagination } = await mailry.emailAccounts.list({
domainId: 'a1b2c3d4-...',
search: 'support',
page: 1,
limit: 20,
});
for (const account of data) {
console.log(account.id, account.email, account.firstName, account.lastName);
}
GET /public/domain
import { DomainStatus } from 'mailry';
const { data } = await mailry.domains.list({ search: 'acme' });
const ready = data.filter((domain) => domain.status === DomainStatus.Active);
List methods return one page along with pagination (currentPage, totalPage, totalData). Page size is capped at 100 by the API.
To walk through every page, use listAll(), which fetches pages as you iterate:
for await (const domain of mailry.domains.listAll()) {
console.log(domain.domainName);
}
for await (const account of mailry.emailAccounts.listAll({ domainId })) {
console.log(account.email);
}
for await (const message of mailry.inbox.listAll({ folder: MessageFolder.Inbox })) {
console.log(message.subject);
}
Every error thrown by the SDK extends MailryError, which has status, body and headers. Errors returned by the API also have details, a list of every validation message.
import {
Mailry,
MailryError,
AuthenticationError,
BadRequestError,
RateLimitError,
} from 'mailry';
try {
await mailry.inbox.send({ emailId, to, subject, plainBody });
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Check your API key');
} else if (error instanceof BadRequestError) {
console.error('Invalid request:', error.details);
} else if (error instanceof RateLimitError) {
console.error(`Slow down, retry in ${error.retryAfter}ms`);
} else if (error instanceof MailryError) {
console.error(error.status, error.message);
} else {
throw error;
}
}
| Error class | When |
|---|---|
BadRequestError | 400 or 422: invalid input, quota exceeded |
AuthenticationError | 401: missing or invalid API key |
PermissionDeniedError | 403: the key lacks permission |
NotFoundError | 404 |
PayloadTooLargeError | 413: attachment too large |
RateLimitError | 429: over 120 requests per minute |
InternalServerError | 5xx |
ConnectionError | The API could not be reached |
TimeoutError | The request exceeded timeout |
MailryError | Base class, also used for invalid SDK arguments |
The API allows 120 requests per minute per API key. Rate limited requests (429) are retried automatically, honouring the Retry-After header. Read requests (GET) are also retried on network errors, timeouts and 5xx responses. Sends are never retried after they reach the server, so an email is not delivered twice. Set maxRetries: 0 to turn retries off.
All request and response types are exported:
import type {
SendEmailParams,
SendEmailResponse,
Message,
MessageAttachment,
ListMessagesParams,
EmailAccount,
Domain,
MailryListResponse,
Attachment,
} from 'mailry';
pnpm install
pnpm test
pnpm typecheck
pnpm build
See the full API reference at api.mailry.co/docs/public.