はじめに
最初のメールを送信
テンプレートを公開し、API キーを作成して最初のメールを送信します。
1. プロジェクトを準備
送信を含むプランでプロジェクトを作成します。サンドボックスでは実際に配信せずにテストできます。本番送信には送信設定を完了し、実際の送信を有効にしてください。
2. テンプレートを公開
キーが welcome のテンプレートを作成し、本文に {{ name }}、ボタンの URL に {{ action_url }} を追加して公開します。以下の例では両方の変数を渡します。別のテンプレートを使う場合は、エディターでキーと必要な変数を確認してください。
3. API キーを作成
プロジェクトの API キー画面でキーを作成し、サーバーの環境変数 SENDERY_API_KEY に保存します。ブラウザーのコードやソース管理には含めないでください。
Shell
export SENDERY_API_KEY="your_project_api_key"4. メールを送信
cURL の例を実行するか、利用する言語の SDK をインストールしてサンプルを実行します。本番配信を試す場合は、宛先をご自身のアドレスに変更してください。welcome-123 は同じメールの再試行にのみ再利用し、別の送信には新しいキーを使います。
cURL
curl https://sendery.co/api/v1/emails \
-H "Authorization: Bearer $SENDERY_API_KEY" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Idempotency-Key: welcome-123' \
-d '{
"to": "[email protected]",
"template": "welcome",
"data": {"name": "Alex", "action_url": "https://example.com/start"}
}'PHP
require 'vendor/autoload.php';
use Sendery\Client;
$sendery = new Client(getenv('SENDERY_API_KEY'));
$receipt = $sendery->send(
to: '[email protected]',
template: 'welcome',
data: ['name' => 'Alex', 'action_url' => 'https://example.com/start'],
);
echo $receipt['id'];JavaScript
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: 'welcome',
data: { name: 'Alex', action_url: 'https://example.com/start' },
});
console.log(receipt.id);Python
import os
from sendery import Sendery
sendery = Sendery(os.environ["SENDERY_API_KEY"])
receipt = sendery.send(
to="[email protected]",
template="welcome",
data={"name": "Alex", "action_url": "https://example.com/start"},
)
print(receipt["id"])Java
import co.sendery.Sendery;
import java.util.Map;
public class SendWelcome {
public static void main(String[] args) {
var sendery = new Sendery(System.getenv("SENDERY_API_KEY"));
var receipt = sendery.send("[email protected]", "welcome", Map.of(
"name", "Alex",
"action_url", "https://example.com/start"
));
System.out.println(receipt.id());
}
}Kotlin
import co.sendery.Sendery
fun main() {
val sendery = Sendery(System.getenv("SENDERY_API_KEY"))
val receipt = sendery.send("[email protected]", "welcome", mapOf(
"name" to "Alex",
"action_url" to "https://example.com/start"
))
println(receipt.id())
}5. 結果を確認
202 はリクエストの受付を示します。返された id を保存して配信状態を確認します。サンドボックスでは状態が simulated になり、実際のメールは配信されません。
202 · application/json
{
"id": "35a6b227-1748-412e-8651-430492201670",
"status": "queued"
}6. メールの状態を取得
以下の ID を送信時に返された ID に置き換えてください。
Shell
curl https://sendery.co/api/v1/emails/35a6b227-1748-412e-8651-430492201670 \
-H "Authorization: Bearer $SENDERY_API_KEY" \
-H 'Accept: application/json'