言語別 SDK
JavaScript / TypeScript
JavaScript / TypeScript から Sendery の公開済みテンプレートを送信します。
インストール
npm install @sendery/[email protected]動作環境
Node.js 22.12 以上が必要です。ES モジュールのインポートを使用し、TypeScript の型定義を含みます。
設定
公開済みテンプレートを選び、プロジェクトの API キーを作成します。キーはサーバーの環境変数 SENDERY_API_KEY に保存してください。
Shell
export SENDERY_API_KEY="your_project_api_key"メールを送信
your-template を公開済みテンプレートのキーに、data をそのテンプレートの変数に置き換えてください。
受付済みメールの id と status が返ります。
JavaScript and TypeScript
import { Sendery } from '@sendery/sdk';
const apiKey = process.env.SENDERY_API_KEY;
if (!apiKey) throw new Error('Set SENDERY_API_KEY on your server.');
const sendery = new Sendery(apiKey);
const receipt = await sendery.send({
to: '[email protected]',
template: 'your-template',
data: { name: 'Alex', action_url: 'https://example.com/start' },
});
console.log(receipt.id);メールの状態を取得
返された ID で配信状態を確認します。
JavaScript and TypeScript
const message = await sendery.get(receipt.id);
console.log(message.status);送信を再試行
一時的なエラーには retry(3) で最大 3 回追加で試行できます。毎回同じ冪等性キーとメールのデータを使ってください。
JavaScript and TypeScript
const email = sendery.prepare({
to: '[email protected]',
template: 'your-template',
data: { name: 'Alex', action_url: 'https://example.com/start' },
}, 'your-idempotency-key');
const receipt = await email.retry(3).send();エラー処理
JavaScript and TypeScript
import { SenderyError } from '@sendery/sdk';
try {
const receipt = await email.retry(3).send();
console.log(receipt.id);
} catch (error) {
if (error instanceof SenderyError) {
console.error(error.status, error.code, error.errors);
// error.retryAfter is a delay in seconds, when provided.
}
throw error;
}特定のバージョンを送信
公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。
JavaScript
const receipt = await sendery.prepare({
to: '[email protected]',
template: 'your-template',
data: { name: 'Alex', action_url: 'https://example.com/start' },
}).version(3).send();添付ファイル
ファイルのバイト列を attachment() に渡します。base64への変換はヘルパーが行います。
最大10ファイル、合計5 MBまで送信できます。対応形式と上限は 添付ファイルのリファレンス を参照してください。
使用例
import { Sendery, attachment } from '@sendery/sdk';
import { readFile } from 'node:fs/promises';
const sendery = new Sendery(process.env.SENDERY_API_KEY);
const file = await readFile('/path/document.pdf');
await sendery.prepare({
to: '[email protected]',
template: 'your-template',
data: { name: 'Alex', action_url: 'https://example.com/start' },
attachments: [attachment('document.pdf', file, 'application/pdf')],
}, 'your-idempotency-key').retry().send();