MostlyRender

Asincrono e webhook

Per PDF grandi, screenshot full-page o alto volume, renderizza asincronamente: accoda il job, ricevi un id immediatamente, quindi esegui il polling o ricevi un webhook firmato.

# Accoda

Aggiungi async: true (o un webhookUrl, o un notifyEmail) a qualsiasi richiesta di render o screenshot. Fornisci webhookUrl per un callback firmato, notifyEmail per ricevere un’email quando il job termina, oppure nessuno dei due e semplicemente esegui il polling:

bash
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"
  }'
json
{ "id": "job_xyz789", "status": "queued" }

La risposta è 202 Accepted con un job id.

# Poll

bash
curl https://api.mostlyrender.com/v1/renders/job_xyz789 \
  -H "Authorization: Bearer mr_live_…"
json
{
  "id": "job_xyz789",
  "status": "done",
  "kind": "render",
  "result": { "id": "rnd_abc", "url": "https://img.mostlyrender.com/…/rnd_abc.pdf", "output": "pdf" },
  "error": null
}

Lo status passa da queued → processing → done (o error). Quando done, result contiene il render { id, url, output }.

python
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 fa un POST del risultato del job lì quando termina:

json
{
  "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 raw nell’header x-mostlyrender-signature, con prefisso sha256=. Ricalcolala con il tuo segreto di firma e confronta in tempo costante:

js
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 alcune volte in caso di errore; i target dei webhook sono protetti da SSRF proprio come gli URL degli screenshot.