Functions and libraries

Write a helper once and call it from every code block and run conditional of the flow, or import a JavaScript library such as crypto-js or dayjs.

Functions

Open Functions on the left. Whatever you define there can be called by name from every code block and run conditional in the flow. It is checked for syntax errors when you save.

The Functions panel with the assertion helpers and the Import library button
The Functions panel. New flows start with the assertion helpers.
// Money as text: money(12.5) → "$12.50"
function money(value) {
  return "$" + Number(value).toFixed(2);
}

// Log in from anywhere and keep the token.
async function login(user, password) {
  const r = await http({ method: "POST", url: $baseUrl + "/auth/login", body: { username: user, password } });
  $token = JSON.parse(r.body).accessToken;
  return r.status;
}

New flows start with the assertion helpers (assertStatus, assertJsonPath…), built on check(); change them or add your own. An older flow gets them with Insert assertion helpers. Checks in code.

Libraries

Functions → + Import library adds a JavaScript file to the flow. Its code is stored in the flow, so the flow and its export keep working on any machine. Each library shows as a badge above the functions: hover it to see how to use it, × removes it. Libraries load left to right, before your functions, so your functions can use them.

What works

To update a library, import the new file under the same name.

// crypto-js.min.js imported as CryptoJS
const token = CryptoJS.AES.decrypt($cipher, $key).toString(CryptoJS.enc.Utf8);
log(CryptoJS.SHA256(token).toString());

// dayjs.min.js imported as dayjs
$tomorrow = dayjs().add(1, "day").format("YYYY-MM-DD");

Node built-ins

Many things need no library. require() offers these Node built-in modules, none of them for files or the network: assert, buffer, crypto, events, path, punycode, querystring, string_decoder, url, util and zlib.

A library runs with your permissions, like the rest of a flow's code (Code runs with your permissions). Import only libraries from a source you trust.
const crypto = require("crypto");
$signature = crypto.createHmac("sha256", $apiSecret).update($payload).digest("hex");

// Base64 with Node alone
const encoded = Buffer.from("user:pass").toString("base64");
const decoded = Buffer.from(response.body, "base64").toString("utf8");