Run Scripts
🤖/script/run runs scripts in Assemblies.
This Robot allows you to run arbitrary JavaScript
as part of the Assembly
execution process. The Robot is invoked automatically when there are Assembly
Instructions containing ${...}
:
{
"robot": "/image/resize",
"width": "${Math.max(file.meta.width, file.meta.height)}"
}
You can also invoke this Robot directly, leaving out the ${...}
:
{
"robot": "/script/run",
"script": "Math.max(file.meta.width, file.meta.height)"
}
When accessing arrays, the syntax is the same as in any JavaScript program:
{
"robot": "/image/resize",
"width": "${file.meta.faces[0].width * 2}"
}
Compared to only accessing an Assembly Variable:
{
"robot": "/image/resize",
"width": "${file.meta.faces.0.width}"
}
For more information, see Dynamic Evaluation.
Parameters
-
use
String / Array of Strings / Object requiredSpecifies which Step(s) to use as input.
-
You can pick any names for Steps except
":original"
(reserved for user uploads handled by Transloadit) -
You can provide several Steps as input with arrays:
"use": [ ":original", "encoded", "resized" ]
💡 That’s likely all you need to know about
use
, but you can view Advanced use cases. -
-
script
String⋅ example:"Math.max(min, file.meta.width)"
requiredA string of JavaScript to evaluate. It has access to all JavaScript features available in a modern browser environment.
The script is expected to return a
JSON.stringify
-able value in the same tick, so noawait
or callbacks are allowed (yet).If the script does not finish within 1000ms it times out with an error. The return value or error is exported as
file.meta.result
. If there was an error,file.meta.isError
istrue
. Note that the Assembly will not crash in this case. If you need it to crash, you can check this value with a 🤖/file/filter Step, settingerror_on_decline
totrue
.You can check whether evaluating this script was free by inspecting
file.meta.isFree
. It is recommended to do this during development as to not see sudden unexpected costs in production.
Related blog posts
- Introducing the /script/run Robot for code evaluation December 15, 2021
- Celebrating transloadit’s 2021 milestones and progress January 31, 2022
- Easy instant website screenshots via Transloadit CDN May 11, 2022
- How to blur faces in photos with Transloadit July 4, 2023