Script Webhooks
What a webhook is
Any script or workflow can be run by another system with one HTTP call: an ERP job, a partner's server, a form, a Power Automate flow, or a curl command. The JSON you send becomes the script's context, and the call can either wait for the answer or come back at once with an id to look up later.
What you need
- The script's ID. The number shown next to the script's name in the Script Engine.
- An API key. See API keys.
The quickest way to get both is the Webhook button in the script's header in the Script Engine. It opens Trigger this script via HTTP, with the script's address and a Copy button, a Return mode choice, and a ready-to-paste Example that changes with the mode. For a workflow, the Manual / API trigger has Copy curl (JSON) and Copy curl (file upload).
API keys
Calls are signed with a DataMagik API key. To create one:
- Click your name at the top right to open your profile, then Security Settings.
- Under Bearer Tokens (API Keys), enter a Bearer Token Name, such as "Acme ASN feed".
- Choose Expires In (Days): from 30 days to 3 years (1 year is preselected), or Never expires. An expired key stops working, so note the date.
- Click Create Bearer Token, and copy the key. It starts with
dcp_and is shown only once.
A key acts as the person who created it. It can run a script only if that person has the DataMagik - Builder or DataMagik - Viewer permission, and it stops working if that person's account is made inactive. For a partner or another system, create the key under a user set up for that purpose rather than a person's own account, and give each caller its own key so you can tell them apart in Executions and switch one off without the others.
Send the key in the X-API-Key header. Authorization: Bearer dcp_… also works.
The call
POST https://<your DataMagik address>/api/script-engine/scripts/<script id>/execute
curl -X POST 'https://<your DataMagik address>/api/script-engine/scripts/482/execute' \
-H 'X-API-Key: dcp_your_key' \
-H 'Content-Type: application/json' \
-d '{"order_id": "SO-1001", "notify": true}'
The script reads the body as context:
function main(context) {
const orderId = context.order_id; // "SO-1001"
if (context.notify) {
// ...
}
return { received: orderId };
}
The request body
- Any JSON object is passed to the script as it is.
- An empty body is allowed; the script gets an empty
context. - A JSON array, string or number is refused with
400. Wrap it in an object, such as{"rows": [...]}. - The
input_contextwrapper. A body with a top-levelinput_contextfield is read as a wrapper: the script gets what is insideinput_context, and the wrapper may also carryversion_id(run a specific saved version) andpriority. DataMagik's own screens call scripts this way. Any other body is passed through whole, so a partner field namedpriorityorversion_idreaches the script untouched. - Raw callers can choose a version or priority with the
X-Script-Version-IdandX-Script-Priorityheaders.
Uploading files
Send multipart/form-data instead of JSON to upload files, such as a CSV, a spreadsheet or an EDI file. Each form field becomes a context field, and the files arrive in context.files, each as {field, filename, contentType, size, base64}.
curl -X POST 'https://<your DataMagik address>/api/script-engine/scripts/482/execute' \
-H 'X-API-Key: dcp_your_key' \
-F 'customer=ACME' \
-F 'file=@orders.csv'
Up to 10 files per call, 10 MB per file and 20 MB in all.
Getting the answer back
There are two ways to call, chosen per request. The Return mode in the Webhook panel switches between them: Queue and poll and Wait for the result.
Queue and poll (the default)
Without ?wait=sync, DataMagik waits up to about 3 seconds for the script.
If it finished in that time, you get 200 with the whole run:
{
"success": true,
"execution_id": "6f1c…",
"completed": true,
"execution": {
"status": "completed",
"output_data": { "received": "SO-1001" },
"error_message": null,
"execution_time_ms": 412,
"api_calls_made": 1,
"console_logs": ["..."]
}
}
Here success: true means the call was accepted. Whether the script worked is in execution.status: a script that threw an error also comes back as 200, with status failed and the reason in error_message.
If it is still running, you get 202 with the id to look it up:
{
"success": true,
"execution_id": "6f1c…",
"message": "Script execution queued",
"status_url": "/api/script-engine/executions/6f1c…",
"timeout_seconds": 300
}
Looking up a run
Poll the status_url with the same API key until the status is final:
curl 'https://<your DataMagik address>/api/script-engine/executions/6f1c…' \
-H 'X-API-Key: dcp_your_key'
The answer is {"success": true, "execution": {...}}, with the run's status, output_data, error_message, error_code, console_logs and timings. The status is one of:
| Status | Meaning |
|---|---|
queued | Waiting for a worker. |
processing | Running. |
completed | Finished. The return value is in output_data. |
failed | The script threw an error, or the run could not be completed. |
timeout | The script ran past its timeout. |
cancelled | The run was cancelled before it started. |
An id that does not exist, or belongs to another company, returns 404. /api/script-engine/executions/<id>/stream sends the same information as server-sent events instead, for callers that can hold a stream open.
Wait for the result: ?wait=sync
Add ?wait=sync when the calling system cannot poll and needs the answer in the reply to its own request:
curl -X POST 'https://<your DataMagik address>/api/script-engine/scripts/482/execute?wait=sync' \
-H 'X-API-Key: dcp_your_key' \
-H 'Content-Type: application/json' \
-d '{"order_id": "SO-1001"}'
The request is held until the script finishes, and the script's return value is the whole response body, with no wrapper around it. return { invoice: "INV-1042", total: 91.5 } answers {"invoice":"INV-1042","total":91.5}; a script that returns nothing answers {}. The run's id is in the X-Execution-Id response header, and the script's own run time in X-Execution-Time-Ms.
| Code | Meaning |
|---|---|
200 | Finished. The body is the return value. |
422 | The script ran and failed. The body is {success: false, error, error_code, execution_id, status}. |
429 | Your company already has 8 waiting calls in progress (error_code SYNC_CONCURRENCY_LIMIT). Retry shortly, or call without ?wait=sync. |
503 | The run queue is unavailable, so the run never started (QUEUE_UNAVAILABLE). |
504 | The wait ran out (SYNC_WAIT_TIMEOUT). The body has the execution_id and status_url. |
A call waits at most about 9½ minutes, because network equipment does not hold a request open for longer, even though a script's own timeout can be up to 45 minutes. Add &wait_timeout=30 to wait less (in seconds); a larger value is reduced to the limit. A 504 does not cancel the run: it carries on under its own timeout and its result appears in Executions under the execution_id. If a script routinely takes longer than the limit, use the default mode and poll, or have the script deliver its result itself, by email or by dropping a file.
If a script returns { success: false, ... }, the caller gets 200 with that body, and DataMagik counts the run as a failure in Executions and Integrations.
Workflows: Respond to Caller
A workflow is called the same way. With ?wait=sync, the caller gets the items of the last step that ran, or exactly what a Respond to Caller step says: JSON, text or a file, with the status code and headers you choose. See Workflows in Production.
Errors before the script runs
| Code | Cause |
|---|---|
400 | The script ID is not a number, the body is not a JSON object, a multipart upload is over the limits, or the version ID is malformed. |
401 | The API key is missing, wrong, or expired, or its user is inactive. |
403 | The key's user has neither DataMagik - Builder nor DataMagik - Viewer. |
404 | No such script in your company, or the script was deleted ("This script was deleted, so it cannot be run."), or the requested version is not one of this script's. |
500 | The run could not be queued. Retry later. |
Where the calls show up
Every call is a run in Executions, with its input, console output, return value and any error. Calls made with an API key are marked with the key's name, and the Started by filter's API key (webhook) choice lists only them. To watch one partner's traffic, add its keys to an Integration under Webhook callers.
If a call fails, read the response body first: key and body problems come back straight away. For errors inside the script, open the run in Executions.
Related
- Script Schedules: run a script by the clock.
- Inbound Email: run a script when an email arrives.
- Hosted File Gateway: let partners upload files over SFTP or HTTPS.
- Script Credentials: keep the passwords and keys your script uses to call other systems.