Our /script/run Robot

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 required

    Specifies 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)"required

    A 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 no await 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 is true. 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, setting error_on_decline to true.

    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