Zapier
Zapier’s Webhooks by Zapier app calls the API and hears when a job finishes. Zapier offers it on its paid plans. The overview explains the parts these settings use.
Zapier passes files between steps as links, so the job fetches the file from its link, and the work takes two Zaps:
- The second Zap, built first, starts when a job finishes: it gets the converted file and does something with it.
- The first Zap starts the job, with the second Zap’s address as the job’s
webhook.
Nothing waits on a conversion, however long it takes.
The second Zap: when a job finishes
Section titled “The second Zap: when a job finishes”- Start a Zap with Webhooks by Zapier and the Catch Hook trigger. Copy its URL (
https://hooks.zapier.com/hooks/catch/…). - Check it’s ours before you act on it: add Webhooks by Zapier’s GET action for
https://api.formatjet.com/v1/jobs/followed by the job’s ID from the hook (data.job.id), with the headerAuthorization:Bearer fj_live_…. Use its answer from here on, never the hook’s: a request that didn’t come from us can’t make the Zap act on a file or a status we didn’t give. It can still name one of your real jobs again if someone learns the hook’s URL, so keep that URL private, and have the Zap skip a job it has already handled and one whosemetadataisn’t this Zap’s. (Checking theFormatJet-Signatureheader proves the request is ours. It needs the Catch Raw Hook trigger, which keeps the headers and the raw body, and a code step, as in Check the signature.) - Add a Filter that goes on only when the answer’s
statusiscompletedand itsmodeislive: test jobs’ events saytest, and anyone with a test key can send them anywhere. - Add the step that uses the file, such as a storage app’s upload. For its file, map the answer’s output
url: it works without a key until itsexpires_at.
To give the Catch Hook a sample, turn the first Zap on with a test key, as below, and run it once: test jobs cost nothing.
The first Zap: start the job
Section titled “The first Zap: start the job”After your trigger (a new file in a folder, an email attachment), add Webhooks by Zapier’s POST action:
| Setting | Value |
|---|---|
| URL | https://api.formatjet.com/v1/jobs |
| Payload Type | Raw |
| Data | The JSON below, with the file mapped from the trigger into url and the Catch Hook’s URL in webhook |
| Headers | Authorization: Bearer fj_live_…. Content-Type: application/json. FormatJet-Client: zapier-webhooks |
{ "input": { "url": "<the file, mapped from the trigger>" }, "steps": [ { "op": "convert", "conversion": "word-to-pdf" } ], "metadata": { "zap": "contracts" }, "webhook": "<the Catch Hook URL of the second Zap>"}Use a slug from GET /v1/operations in place of word-to-pdf. metadata is yours: it comes back with the job, so the second Zap can tell your Zaps’ jobs apart. The answer is the job, with status awaiting_upload while we fetch the file.
We fetch the file from its link, up to your plan’s file size, so the link must open without signing in. If the job fails with import_failed, its error.detail says why: a link that answers 401 or 403 needs a shared or public link from the storage app instead.
Here is the same flow in curl, waiting for the job rather than hearing from it:
#!/usr/bin/env bash# Needs curl and jq.set -euo pipefailAPI=https://api.formatjet.com/v1AUTH="Authorization: Bearer $FORMATJET_API_KEY"
# 1. A job from the file's address, as a platform that passes files as links makes it.job=$(curl -sS --fail-with-body "$API/jobs" -H "$AUTH" -H "Content-Type: application/json" \ -H "FormatJet-Client: zapier-webhooks" \ -d '{"input": {"url": "https://formatjet.com/docs/samples/report.pdf"}, "steps": [{"op": "convert", "conversion": "pdf-to-markdown"}], "metadata": {"workflow": "reports"}}')id=$(jq -r .id <<< "$job")
# 2. Ask until it ends: each answer comes as soon as the job ends, or after 30 seconds.status=$(jq -r .status <<< "$job")until [[ $status == completed || $status == failed || $status == canceled ]]; do job=$(curl -sS --fail-with-body "$API/jobs/$id?wait=30" -H "$AUTH") status=$(jq -r .status <<< "$job")done[[ $status == completed ]] || { jq .error <<< "$job"; exit 1; }
# 3. Download the output with the key: the API redirects to the file's own URL, and curl# leaves the key off when a redirect leads to another host.curl -sS --fail-with-body -L "$API/jobs/$id/outputs/0" -H "$AUTH" -o report.mdA webhook endpoint instead
Section titled “A webhook endpoint instead”The job’s webhook sends that job’s events. To send every job’s, or every job of some conversions, add the Catch Hook URL as a webhook endpoint instead, in the dashboard’s Webhooks page or with POST /v1/webhooks, narrowed with conversions (up to 50) or metadata. Then the first Zap leaves out webhook. The overview has the request.
An event can arrive twice, with the same id: acting once per job covers that too. Zapier answers each at once, which is what we need: we wait 10 seconds for an answer.