Generate documents asynchronously with webhooks

Modified on Thu, 3 Dec at 12:35 PM

Use asynchronous generation when another system creates many documents, or when it should not wait for each document. Create accepts the request right away, generates the document in the background, and calls a webhook on your system when the document is ready or when generation failed.

Before you begin

  • The calling system needs an access token. See API secrets.
  • The request body is the same as for normal generation. See Generate documents through the API.
  • Your system needs a webhook address that Create can reach from the internet.

Queue one document

POST https://app.omnidocs.cloud/api/v1/units/{unitId}/recipes/{recipeId}/generate/async
Authorization: Bearer [access_token]
Content-Type: application/json

{
  "data": { ... },
  "webhooks": {
    "onSuccess": [ { "url": "https://example.com/create/done" } ],
    "onError": [ { "url": "https://example.com/create/failed" } ]
  }
}

Create answers with 202 Accepted:

{
  "success": true,
  "message": "Document generation job accepted",
  "generateId": "c6cc4c046ec847eaa1589ce88a619b53"
}

Keep the generateId. The webhook uses it to tell you which document it is about.

Query parameters

  • mimeType: the output format. Use application/pdf for PDF. Without it, the template's normal format is used.
  • fileName: the file name of the generated document.
  • includeMetadata=true: adds the template ID and the data you sent to the success webhook.

Webhooks

webhooks has two lists:

  • onSuccess: called when the document is ready.
  • onError: called when generation failed.

Each webhook has:

  • url (required): the address Create calls.
  • requestType: Post (default), Put or Patch.
  • headers: extra headers Create sends, for example a key your system checks.
{
  "url": "https://example.com/create/done",
  "requestType": "Post",
  "headers": { "X-Api-Key": "[your key]" }
}

Your endpoint must answer with a success status code (2xx).

Success webhook

{
  "status": "success",
  "documentGenerationId": "cf137ce118b648619ddc1dbbd7b4f160",
  "url": "https://app.omnidocs.cloud/api/v1/generate/[file id]?fileName=Decision+letter"
}

Download the document from url. With includeMetadata=true, the webhook also contains metadata with recipeId and the data you sent.

Error webhook

{
  "status": "error",
  "error": {
    "reason": "Unmatched data in payload.",
    "errors": {
      "version": 1,
      "result": [
        {
          "label": "Decision",
          "result": { "status": "invalid", "errors": [ { "type": null, "interpolationValues": [] } ] },
          "givenValue": null
        }
      ]
    }
  },
  "documentGenerationId": "eb56914edf5949db804b6969dfdd9a1f"
}

reason explains what went wrong. result names the field (label) and the value that caused it, when Create knows them.

Queue many documents at once

Send a list of requests to the batch endpoint. Each item has the same shape as a single request, with its own data and webhooks.

POST https://app.omnidocs.cloud/api/v1/units/{unitId}/recipes/{recipeId}/generate/batch/async
[
  { "data": { ... }, "webhooks": { "onSuccess": [ { "url": "https://example.com/create/done" } ] } },
  { "data": { ... }, "webhooks": { "onSuccess": [ { "url": "https://example.com/create/done" } ] } }
]

Create answers with 202 Accepted and a list with one result per item, each with its own generateId. The query parameters apply to every item. A batch request can be at most 15 MB.

Good to know

  • Use the address that matches where your organization is hosted: https://app.omnidocs.cloud, https://app.eu.omnidocs.cloud, or your own address if Create is self-hosted. See API secrets.
  • The 202 response only means the job was accepted. Whether the document was created is reported by the webhook.
  • Without webhooks, there is no way to receive the result. Always set at least onSuccess and onError.
  • In rare cases the same webhook can arrive more than once. Use documentGenerationId to recognise a webhook you have already handled.
  • The endpoints are also described in the API reference: app.omnidocs.cloud/openapi, or app.eu.omnidocs.cloud/openapi for organizations on .eu.omnidocs.cloud.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article