Start a workflow from a webhook
Call a workflow's webhook from your own code or another service: the request, the response, how to avoid duplicate runs, and how to reply on a run's task.
A webhook is a trigger with its own URL. When your code, another app, or an AI agent sends it a POST request, Polar starts a run of the workflow. Use one when something outside Polar should decide when the workflow runs.
Create a webhook
On the workflow's page, click Add trigger and pick Create a webhook. Polar shows the Webhook URL and Secret. Copy the secret then.
- To see the secret again, open the webhook's details and click Show secret.
- To replace it, pick Rotate secret from the webhook's ⋯ menu. The old secret stops working right away, and runs from this webhook that were still waiting to start are cancelled.
- A workflow can have several webhooks, each with its own URL and secret. Name them so you can tell them apart.
You can also ask Polar in a chat to add a webhook. It shows the URL and secret on a card. Polar's agent never sees the secret.
Send a request
Send a POST request to the webhook's URL, with the secret as a bearer token:
curl -X POST <webhook URL> \
-H "Authorization: Bearer <secret>" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1042-refunded" \
-d '{"input": "Order 1042 was refunded"}'Everything except the secret is optional:
| Field | Where | What it does |
|---|---|---|
input | Body | Text for the run. Polar adds it after the workflow's command. |
thread_id | Body | Sends input to a task this webhook already started, instead of starting a new run. See Reply on the same task. |
Idempotency-Key | Header | Your own ID for this event, up to 255 characters. Calls that repeat it run only once. See Avoid duplicate runs. |
The run from the call above starts with this message:
/your-command Order 1042 was refundedThe body is JSON and can be up to 64 KB. Polar ignores any other fields in it. A call with no body starts a run with no input.
Read the response
Polar answers right away with 202 Accepted. It doesn't wait for the run to finish.
{
"status": "launched",
"run_id": "run-…",
"thread_id": "th-…"
}| Status | What happened |
|---|---|
launched | The run started. |
queued | The run is saved and waiting for the workflow's computer. It starts as soon as that computer can take it. |
steered | Your reply was handed to the run already working on that task. |
duplicate | Polar already took a call with this Idempotency-Key. Nothing new started. |
run_id and thread_id name the run and its task. For duplicate, they are the first call's. Keep the thread_id if you want to reply to the task later.
A workflow that runs on Polar's Computer answers launched. A workflow that runs on your own computer answers queued, even when the computer is ready, and the run usually starts a few seconds later. If the computer is asleep or busy, the run waits. See Understand how workflows run.
To see how a run went, open it under Recent runs on the workflow's page.
Avoid duplicate runs
Send an Idempotency-Key header with your own ID for the event, like an order number. If the same key reaches the same webhook again, Polar starts nothing and answers duplicate with the first call's run_id and thread_id. That makes it safe to send a call again when you're not sure the first one arrived.
- Keys belong to one webhook. The same key sent to another webhook starts a run there.
- Polar remembers a key for about 7 days. After that, the same key starts a new run.
- A call Polar turned away because you were out of credits, or because too many runs were waiting, isn't remembered. Send it again with the same key.
- Without a key, every call starts a run.
Reply on the same task
To add to a run instead of starting a new one, send the thread_id from an earlier response along with an input:
curl -X POST <webhook URL> \
-H "Authorization: Bearer <secret>" \
-H "Content-Type: application/json" \
-d '{"thread_id": "th-…", "input": "The customer cancelled"}'Polar adds your input to that task as its next message. The workflow's command doesn't run again.
- If the task is still working, Polar reads the message after its current step, and the response says
steered. - If the task has stopped or finished, a new run starts on the same task, with everything it did before, and the response says
launched.
A webhook can reply only to tasks it started. A thread_id from another webhook, another workflow, or a task you started yourself gets an error back, and so does a task you deleted. If the first run on the task is still waiting for its computer, the reply is refused until that run starts.
Sign requests instead of sending the secret
If the service calling your webhook signs its requests the Standard Webhooks way, give it the secret as its signing secret. Polar checks the webhook-signature header against the request's body and its webhook-timestamp, which has to be within 5 minutes of now. For a signed request, the webhook-id header does the job of Idempotency-Key.
Errors
When Polar turns a call away, no run starts. Most errors have a body with an error code and a message:
{
"detail": {
"error": "webhook_unauthorized",
"message": "Missing or invalid webhook credentials."
}
}| Response | What it means |
|---|---|
401 webhook_unauthorized | The secret is missing or wrong, or the signature doesn't match. |
402 credit_limit_exceeded | You're out of credits. |
404 trigger_not_found | No webhook has this URL. It may have been deleted. |
404 thread_not_found | The thread_id isn't a task this webhook started, or the task was deleted. |
409 workflow_not_taking_events | The webhook is paused, or its workflow is archived or can't run right now. |
409 thread_not_started | The first run on that task hasn't started yet. Try again once it has. |
409 thread_attach_in_progress | Another reply is starting a run on that task. Try again in a moment. |
| 413 | The body is over 64 KB. Here detail is a sentence, with no error code. |
422 invalid_request | The body isn't valid JSON. |
| 422 | A thread_id came without an input, or the idempotency key is over 255 characters. Here detail is a sentence, with no error code. |
429 too_many_queued_runs | The workflow's computer is offline and too many runs are already waiting for it. Try again later, or set the workflow to run on Polar's Computer. |
429 rate_limited | Your webhooks got too many calls in a short time. Wait the number of seconds in the Retry-After header. |
Related
Choose what starts a workflow
How schedules, app events, and webhooks start a workflow, what each run is given, and what pausing a trigger does.
Understand how workflows run
What happens each time a workflow runs: its effort level, where it runs, what it does when your computer is busy or offline, and how it works with nobody watching.