非同期とウェブフック
大きなPDF、フルページスクリーンショット、または高ボリュームの場合は、非同期にレンダリングします:ジョブをエンキューし、すぐにIDを取得し、その後ポーリングするか署名付きウェブフックを受け取ります。
# エンキュー
renderまたはscreenshotリクエストにasync: true(またはwebhookUrlまたはnotifyEmail)を追加します。署名付きコールバック用にwebhookUrlを指定するか、ジョブ完了時にメール通知を受け取るためにnotifyEmailを指定します。またはどちらでもなくポーリングします:
curl https://api.mostlyrender.com/v1/renders \
-H "Authorization: Bearer mr_live_…" \
-H "Content-Type: application/json" \
-d '{
"template": "tpl_abc",
"output": "pdf",
"async": true,
"webhookUrl": "https://your.app/hooks/mostlyrender"
}'
{ "id": "job_xyz789", "status": "queued" }
レスポンスは202 AcceptedとジョブIDです。
# ポーリング
curl https://api.mostlyrender.com/v1/renders/job_xyz789 \
-H "Authorization: Bearer mr_live_…"
{
"id": "job_xyz789",
"status": "done",
"kind": "render",
"result": { "id": "rnd_abc", "url": "https://img.mostlyrender.com/…/rnd_abc.pdf", "output": "pdf" },
"error": null
}
statusはqueued → processing → done(またはerror)に移行します。doneの場合、resultはレンダリング{ id, url, output }を保持します。
job = mr.render_async("tpl_abc", output="pdf", webhook_url="https://your.app/hooks/mostlyrender")
final = mr.wait_for_render(job["id"])
print(final["result"]["url"])
# ウェブフック
webhookUrlを渡すと、MostlyRenderはジョブ完了時にそこにPOSTで結果を送信します:
{
"id": "job_xyz789",
"status": "done",
"result": { "id": "rnd_abc", "url": "https://img.mostlyrender.com/…/rnd_abc.pdf", "output": "pdf" }
}
# 署名を検証する
各ウェブフックは、生のボディのHMAC-SHA256署名をx-mostlyrender-signatureヘッダーにsha256=のプレフィックス付きで保持しています。署名シークレットで再計算し、定時間比較で比較します:
import crypto from 'node:crypto';
function verify(rawBody, header, secret) {
const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex');
return crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected));
}
2xxで応答して確認します。失敗時は数回再試行されます。ウェブフック対象はスクリーンショットURLと同様にSSRFガード対象です。