Call the engine API from a script
Create an API client, get a token, run a workflow and check the result, all with curl.
On this page
Scripts, CI jobs and other systems call the engine through its REST API. This guide creates a named API client, exchanges its key for a token, runs a workflow and reads the execution.
sequenceDiagram
participant Person as Admin or operator
participant Script
participant Engine as MedHook engine
Person->>Engine: Settings > API clients > New API client
Engine-->>Person: API key (shown once)
Script->>Engine: POST /api/auth/token {"apiKey"}
Engine-->>Script: Bearer token (2 hours)
Script->>Engine: POST /api/workflows/{id}/execute
Engine-->>Script: Execution (completed or failed)
Script->>Engine: GET /api/executions/{id}
Engine-->>Script: The same execution, read againSteps
1. Create an API client
An admin or an operator opens Settings > API clients and selects New API client. Give it a name (lowercase letters, digits, ., _ or -), then pick:
- Role: the smallest that works.
viewerreads ids, status, ACKs and errors but never message content,operatorcan run workflows and read messages,admincan do everything except approve deploys. - Workspace: this workspace only, or all workspaces (admins only).
- Expires after: 7, 30 or 90 days.
The defaults (Operator, this workspace, 30 days) suit most scripts, so often you only type a name. The engine shows the key once, with Copy key and a ready curl for the next step that already holds this engine's address and the key (masked on screen; Copy command copies it whole). Copy it into your secrets manager before you close the dialog: the engine keeps only its hash, and the list shows just the key's first characters. Rotate key issues a new one and stops the old key; Revoke stops the client within seconds. Both are in the audit log.
The same works from a shell in the engine container, for scripted setup:
docker compose exec engine create-engine-api-client nightly-sync --role operator --expires 90dOn Kubernetes (GKE and other clusters), run the same command in the engine deployment:
kubectl exec -n medhook deploy/medhook-engine -- \
create-engine-api-client nightly-sync --role operator --expires 90dAdd --workspace <slug> to hold the client to one workspace (see Workspaces below).
Store the printed key in your secrets manager. The engine keeps only its hash.
Admins and operators can also call the API clients routes (/api/api-clients) when signed in. An API client can't create other API clients.
Who can create keys
| Who | Can make | Sees and manages |
|---|---|---|
| Admin | Any role, for this workspace or every workspace | Every client |
| Operator | operator or viewer keys, for the workspace they are in | operator and viewer clients of the workspaces where they're an operator |
| Viewer | Nothing | Nothing (the page says who to ask) |
Why the limits: a key acts with the role it was given, so a key never carries more access than the person who made it. An operator can't make an Admin key (403 ROLE_ABOVE_CALLER) or a key that works in every workspace (403 WORKSPACE_REQUIRED). Rotating keeps a client's role, so an operator can't rotate, re-date or revoke an Admin client either (403 ROLE_ABOVE_CALLER), and a client outside their workspaces answers 404 as if it didn't exist. The page offers only what you may make, and explains a refusal in plain words. Every create, rotate, expiry change and revoke is in the audit log with the person who did it.
From the UI, as an operator:
- Switch to the workspace the key is for.
- Open Settings > API clients and select New API client.
- Type a name, keep Operator (or pick Viewer), then Create client.
- Copy the key, or Copy command for the token request, then Done.
From the CLI, run in the engine container (needs shell access, so usually an admin): create-engine-api-client <name> --role operator --workspace <slug> --expires 30d. The CLI isn't limited by the person's engine role.
2. Get a token
export ENGINE_URL=https://engine.example.test
TOKEN=$(curl -s -X POST "$ENGINE_URL/api/auth/token" \
-H "Content-Type: application/json" \
-d '{"apiKey": "'"$MEDHOOK_API_KEY"'"}' | jq -r .token)The token lasts 2 hours, or less if the client expires first. Get a new one when a call returns 401.
The same answer carries a refresh_token. A long-running job can trade it for a new token without the API key, and must keep the new refresh_token each time: each one works once. See Refresh tokens.
RESP=$(curl -s -X POST "$ENGINE_URL/api/auth/token" \
-H "Content-Type: application/json" \
-d '{"grant_type": "refresh_token", "refresh_token": "'"$MEDHOOK_REFRESH_TOKEN"'"}')
TOKEN=$(echo "$RESP" | jq -r .token)
MEDHOOK_REFRESH_TOKEN=$(echo "$RESP" | jq -r .refresh_token)Exchange a key once and reuse the token for its 2 hours rather than asking for one per call. The exchange is rate limited, and a 429 (RATE_LIMITED) carries Retry-After:
| Limit | Per minute | Why |
|---|---|---|
| One API key (or the root secret) | 5 | Each key has its own count, so several CI jobs behind one NAT don't share one. |
| One IP address, any keys | 30 | A ceiling on a single source. |
| Failed exchanges from one IP | 5 | After 5 unknown, revoked or expired keys in a minute, that IP gets 429 for every exchange, a valid key included, until a minute after the last failure. |
3. Run a workflow
curl -s -X POST "$ENGINE_URL/api/workflows/wf_example/execute" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"input": {"patientId": "TESTPATIENT"}}'The call returns when the run ends, with execution.id, execution.status and each step's result in execution.nodeResults. A long run holds the request open, up to the workflow timeout (WORKFLOW_TIMEOUT_MS, 30 minutes). Retry later on 429 (the queue is full) or 503 (SHUTTING_DOWN, or HISTORY_UNAVAILABLE when the engine could not record the run, so it did not start). See Execute workflow.
To stop a run, call Cancel execution. A running execution answers 202 cancelling and stops before its next step. A pending one is cancelled at once.
4. Check the result
curl -s "$ENGINE_URL/api/executions/exec_example" -H "Authorization: Bearer $TOKEN"Re-read an execution at any time, for example one a trigger started. Poll while status is pending or running. It ends as completed, partial (some destinations failed), failed or cancelled. See Get execution.
Call a webhook trigger with the same token
A webhook trigger with no HMAC secret (every built-in template's, until you add one) accepts the same bearer token, from a client that can reach the workflow's workspace:
curl -s -X POST "$ENGINE_URL/api/triggers/webhook/patient-created" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"firstName": "Test", "lastName": "Testpatient"}'Without a token the answer is 401 SIGNATURE_REQUIRED, whose remediation names both ways in: send a bearer token, or set an HMAC secret on the trigger and have the sender sign the raw body in x-hub-signature-256 (or x-webhook-signature). Senders that can't get a token, such as a SaaS system's outbound webhook, use the secret. See Triggers.
Deploy from CI
A new workflow is a disabled draft (POST /api/workflows with enabled: true is 409 NOT_DEPLOYED), and it goes live through POST /api/workflows/{id}/deploy. An API token never skips the deploy checklist or its approval, whatever the client's role: override is refused with 403. From CI:
- Save the change (
PUT /api/workflows/{id}), with anoperatororadminclient. - Run a passing test of the new version:
POST /api/workflows/{id}/dry-runwith a samplemessage(text, or the JSON body itself). - Request approval:
POST /api/workflows/{id}/deploywith{"action": "request-approval"}. - A signed-in admin approves it in Versions (API tokens can't approve).
- Deploy:
POST /api/workflows/{id}/deploywith{"action": "deploy"}. A409lists what is stillmissing.
See Deploy workflow.