Async e webhook
Per PDF di grandi dimensioni, screenshot full-page o volumi elevati, esegui il rendering asincronamente: accoda il lavoro, ottieni un id immediatamente, quindi esegui il polling o ricevi un webhook firmato.
# Accodamento
Aggiungi async: true (o un webhookUrl) a qualsiasi richiesta render o 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" }
La risposta è 202 Accepted con un id del lavoro.
# Sondaggio
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
}
Lo stato status passa da queued → processing → done (o error). Quando è done, result contiene il rendering { 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
Se passi un webhookUrl, MostlyRender invia il risultato del lavoro tramite POST quando finisce:
{
"id": "job_xyz789",
"status": "done",
"result": { "id": "rnd_abc", "url": "https://img.mostlyrender.com/…/rnd_abc.pdf", "output": "pdf" }
}
# Verifica la firma
Ogni webhook contiene una firma HMAC-SHA256 del corpo grezzo nell’intestazione x-mostlyrender-signature, con prefisso sha256=. Ricalcolala con il tuo segreto di firma e confronta in tempo costante:
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));
}
Rispondi con qualsiasi 2xx per confermare. La consegna viene ritentata poche volte in caso di errore; le destinazioni webhook sono protette da SSRF esattamente come gli URL degli screenshot.