SDK สำหรับ Node.js / TypeScript
ติดตั้ง @craftintertech/mailcraft แล้วส่งอีเมลฉบับแรกจาก Node.js พร้อมระบบลองส่งซ้ำอัตโนมัติและการตรวจลายเซ็น Webhook
SDK ทางการสำหรับ Node.js 18 ขึ้นไป เขียนด้วย TypeScript ไม่มี dependency ตอนรัน (ใช้ fetch ที่มากับ Node) ใช้ได้ทั้ง ESM และ CommonJS
ติดตั้ง
npm install @craftintertech/mailcraft
# หรือ pnpm add / yarn add / bun add
ส่งอีเมลฉบับแรก
สร้าง API Key ที่ Mail Agent → แท็บ API Keys (ต้องมีสิทธิ์ email:send) แล้วเก็บไว้ในตัวแปรสภาพแวดล้อม MAILCRAFT_API_KEY อย่าเขียนคีย์ลงในโค้ดโดยตรง
import { MailCraft } from '@craftintertech/mailcraft';
const mailcraft = new MailCraft(); // อ่านคีย์จาก MAILCRAFT_API_KEY
const res = await mailcraft.send(
{
from: 'ร้านตัวอย่าง <no-reply@mail.example.com>',
to: 'customer@example.org',
subject: 'ยืนยันคำสั่งซื้อ #10042',
htmlbody: '<h1>ขอบคุณที่สั่งซื้อ</h1><p>เรากำลังเตรียมจัดส่งสินค้าให้คุณ</p>',
textbody: 'ขอบคุณที่สั่งซื้อ เรากำลังเตรียมจัดส่งสินค้าให้คุณ',
},
{ idempotencyKey: 'order-10042-receipt' },
);
console.log(res.message_id, res.credits_remaining);
ถ้าโปรเจกต์ยังเป็น CommonJS ใช้ const { MailCraft } = require('@craftintertech/mailcraft'); ได้เหมือนกัน
- ชื่อฟิลด์ตรงกับ REST API ทุกตัว (
htmlbody,textbody,reply_to,mime_headersฯลฯ) - ที่อยู่อีเมลเขียนเป็นสตริง
"ชื่อ <a@b.com>"หรือออบเจ็กต์{ address, name }ได้ จะส่งค่าเดียวหรือเป็นอาร์เรย์ก็ได้ fromต้องเป็นอีเมลบนโดเมนผู้ส่งที่ยืนยันแล้ว
แนบไฟล์
ส่ง Buffer หรือ Uint8Array ได้เลย SDK จะแปลงเป็น base64 ให้เอง
import { readFile } from 'node:fs/promises';
await mailcraft.send({
from: 'billing@mail.example.com',
to: 'customer@example.org',
subject: 'ใบเสร็จรับเงิน INV-001',
textbody: 'แนบใบเสร็จมากับอีเมลนี้',
attachments: [
{ name: 'INV-001.pdf', content: await readFile('INV-001.pdf'), mime_type: 'application/pdf' },
],
});
เทมเพลต ตั้งเวลาส่ง ส่งเป็นชุด และยกเลิก
const r = await mailcraft.send({
from: 'hello@mail.example.com',
to: { address: 'somchai@example.org', name: 'สมชาย' },
template_alias: 'welcome',
template_data: { name: 'สมชาย' },
send_at: new Date('2027-01-02T02:00:00Z'), // 09:00 เวลาไทย
list_unsubscribe: true,
});
await mailcraft.cancel(r.message_id); // ยกเลิกก่อนถึงเวลาส่ง
await mailcraft.sendBatch([
{ from: 'hello@mail.example.com', to: 'a@example.org', template_alias: 'welcome', template_data: { name: 'เอ' } },
{ from: 'hello@mail.example.com', to: 'b@example.org', template_alias: 'welcome', template_data: { name: 'บี' } },
]);
const status = await mailcraft.get(r.message_id); // สถานะการนำส่งรายผู้รับ
จัดการข้อผิดพลาด
ทุกข้อผิดพลาดเป็น MailCraftError ซึ่งมี status, code, field, retryAfter และ body ให้ตรวจได้
import { MailCraftError, InsufficientCreditsError, PermissionDeniedError } from '@craftintertech/mailcraft';
try {
await mailcraft.send(message);
} catch (err) {
if (err instanceof InsufficientCreditsError) {
// 402: เครดิตไม่พอ — แจ้งทีมให้เติมเครดิต
console.error(`เครดิตเหลือ ${err.balance} แต่ต้องใช้ ${err.required}`);
} else if (err instanceof PermissionDeniedError && err.code === 'sandbox_restricted') {
// 403: บัญชียังอยู่ในโหมด Sandbox
} else if (err instanceof MailCraftError) {
console.error(err.status, err.code, err.message);
} else {
throw err;
}
}
| HTTP | คลาส / code |
|---|---|
| 400 / 422 | ValidationErrorinvalid_input |
| 401 | AuthenticationErrorunauthorized |
| 402 | InsufficientCreditsErrorinsufficient_credits |
| 403 | PermissionDeniedErrorsandbox_restricted, missing_scope, forbidden |
| 409 | ConflictErroridempotency_key_reused |
| 413 | PayloadTooLargeErrormessage_too_large |
| 429 | RateLimitErrorrate_limited |
| 5xx | ServerErrorserver_error |
| — | ConnectionErrornetwork_error, timeout, aborted |
ลองส่งซ้ำอัตโนมัติ
เมื่อเจอ 429, 500, 502, 503, 504 หรือเครือข่ายขัดข้อง SDK จะลองใหม่ให้สูงสุด 2 ครั้ง โดยรอตาม Retry-After ถ้ามี ไม่อย่างนั้นจะเว้นระยะแบบ exponential backoff + jitter
ทุกครั้งที่ส่งอีเมล SDK แนบ Idempotency-Key ไปด้วย (สร้างให้อัตโนมัติถ้าคุณไม่ได้ระบุ) การลองใหม่จึงไม่ทำให้ผู้รับได้อีเมลซ้ำและไม่หักเครดิตซ้ำ แนะนำให้ใช้รหัสอ้างอิงของระบบคุณเอง เช่นเลขออร์เดอร์ เพื่อกันซ้ำข้ามการรีสตาร์ตแอปด้วย
const mailcraft = new MailCraft({
apiKey: process.env.MAILCRAFT_API_KEY,
timeout: 30_000, // มิลลิวินาทีต่อหนึ่งครั้ง
maxRetries: 2, // 0 = ปิดการลองซ้ำ
maxRetryDelay: 60_000, // ถ้า Retry-After นานกว่านี้ จะโยน RateLimitError ทันทีแทนการรอ
});
ตรวจลายเซ็น Webhook
ดูวิธีเต็มได้ที่ ตรวจสอบลายเซ็น Webhook
import { parseEvent } from '@craftintertech/mailcraft';
const event = parseEvent(rawBody, req.headers['mailcraft-signature'], process.env.MAILCRAFT_WEBHOOK_SECRET!);