非同期 & Webhook
大きなPDF、フルページスクリーンショット、高ボリュームの場合は、非同期でレンダリングします: ジョブをエンキュー、すぐにidを取得、その後ポーリングするか署名付きウェブフックを受け取ります。
# エンキュー
async: true(またはwebhookUrl)を任意のrenderまたはscreenshotリクエストに追加します:
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"])
# Webhook
webhookUrlを渡すと、MostlyRenderは完了時にジョブ結果をそこにPOSTします:
{
"id": "job_xyz789",
"status": "done",
"result": { "id": "rnd_abc", "url": "https://img.mostlyrender.com/…/rnd_abc.pdf", "output": "pdf" }
}
# 署名の検証
各ウェブフックは、x-mostlyrender-signatureヘッダーに生のボディのHMAC-SHA256署名を含み、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保護されています。