Skip to content

Simulate an async job that calls back

Before you begin

Plan
Included in Pro and Team. How plans and limits work
Role
Admin or higher.
You need
  • A project, and a receiver for the callback such as the project's inbox

Many APIs answer POST /jobs with 202 Accepted and a job id, then call the callback_url from the request when the work is done. This recipe reproduces that on https://orders-api.mockhub.dev: the callback carries the id the mock generated, and goes to the address each request names.

  1. Create a mock for POST /jobs with the status 202 and a templated body:

    { "jobId": "{{random.uuid}}", "status": "processing" }

    See Dynamic responses.

  2. Open the project’s Webhooks tab and select New webhook. Name it Job done.

  3. Under What fires it, choose When a mock is served, pick the POST /jobs mock and set Wait before calling (seconds) to 5.

  4. Set Send it to to The address the request gives. Under Read it from choose Body field, and enter callback_url.

  5. Under Allowed hosts, select Add host and enter the host your receiver runs on, such as hooks.acme.test. Add at least one host even if a request may name the project’s inbox; the inbox itself needs no matching entry. To always send to the inbox, set Send it to to A fixed address and select Send to this project’s inbox instead.

  6. Write the Body template:

    { "jobId": "{{response.body.jobId}}", "status": "done", "at": "{{firedAt}}" }
  7. Select Create webhook, then send a request:

    Terminal window
    curl -X POST https://orders-api.mockhub.dev/jobs \
    -H 'Content-Type: application/json' \
    -d '{"callback_url": "https://hooks.acme.test/jobs"}'

The caller gets 202 with a job id at once. Five seconds later https://hooks.acme.test/jobs receives the same id with "status": "done". Under Deliveries the delivery reads From the request beside its address.

  • The callback_url host must be listed under Allowed hosts, or the delivery is Failed. See A webhook or alert was never delivered.
  • The callback reads at most 256 KiB of the mock’s answer.
  • A webhook that sends to the address a request gives cannot be fired by hand; send a request to the mock instead.
  • The mock does not track jobs: a later GET /jobs/{id} answers what its own mock says, whatever the callback reported.

Report a problem with this page