Sendery
Sendery

フレームワーク連携

Next.js

Sendery の JavaScript SDK を使い、Next.js のサーバーからトランザクションメールを送信します。

インストール
npm install @sendery/[email protected]
ソースコード

動作環境

App Router と Node.js 22.12 以上で JavaScript SDK を使います。プロジェクトの API キーを作成してください。

API キーを設定

開発時は .env.local、本番ではホスティングのシークレット設定に保存します。変数に NEXT_PUBLIC_ を付けないでください。以下のヘルパーには npm install server-only で server-only マーカーパッケージをインストールします。

.env.local
SENDERY_API_KEY=your_project_api_key

サーバーから送信

サーバー側で sendEmail(input, emailKey) を呼び出します。input に宛先、公開済みテンプレートのキー、変数を渡してください。再試行では同じ emailKey と入力を使います。

TypeScript
// lib/send-email.ts
import 'server-only';
import { Sendery, type SendEmailInput } from '@sendery/sdk';

export async function sendEmail(input: SendEmailInput, emailKey: string) {
  const apiKey = process.env.SENDERY_API_KEY;
  if (!apiKey) throw new Error('Set SENDERY_API_KEY on your server.');

  const sendery = new Sendery(apiKey);
  return sendery.prepare(input, emailKey).retry().send();
}

特定のバージョンを送信

公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。

TypeScript
// lib/send-email.ts
import 'server-only';
import { Sendery, type SendEmailInput } from '@sendery/sdk';

export async function sendEmail(input: SendEmailInput, emailKey: string) {
  const apiKey = process.env.SENDERY_API_KEY;
  if (!apiKey) throw new Error('Set SENDERY_API_KEY on your server.');

  const sendery = new Sendery(apiKey);
  return sendery.prepare(input, emailKey).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();