Write JavaScript (upcoming)

Available in version 1.1.72 (not yet live).

Use the Write JavaScript building block to run custom code with the zw API. Your code runs inside the TaskBot runtime and can work with npm packages, variables and tables, state, device storage, and the browser context.

What You Can Do

Imports and Package Management

Install, import, manage, and use npm packages. Use standard import statements or zw.import(options, …) with custom options. API zw.import(), zw.packages.*

// @zw-run-locally

import dayjs from "[email protected]"; // auto-installs if needed
await zw.log("now", dayjs().toISOString());

// Example with options — 'isolate' scopes the package to this TaskBot only
const lodash = await zw.import({ isolate: true }, "[email protected]");
await zw.log("chunked result", lodash.chunk([1, 2, 3, 4], 2));

Reference: Imports and Package Management (upcoming)


Variables and Tables

Read from and write to variables and tables (native and Google Sheets). API zw.getRef(), zw.setRef()

const email = await zw.getRef({ ref_id: 3623, name: "Email" });
await zw.setRef({ ref_id: 3624, name: "Email copy", value: email });

Reference: Write and Read Variables and Tables


Local and Global State

Store and use values for the current run (local state) or across runs (global state). Stays on this device; never sent to ZeroWork servers. API zw.state.*, zw.globalState.*

// @zw-run-locally 

// Per-run local state, cleared when the run ends
const runState = zw.state.access();
runState.runStartedAt = Date.now();

// Shared desktop agent global state
const globalState = zw.globalState.access();
globalState.totalRuns = (globalState.totalRuns ?? 0) + 1;
// In-browser snapshot (survives serialization)
const snapshot = await zw.state.browser.getCopy();
snapshot.runStartedAt = Date.now();
await zw.state.browser.commit(snapshot);

Reference: Local and Global State (upcoming)


Device Storage

Persist key–value data on this device. Survives restarts and reinstalls. Stays on this device; never sent to ZeroWork servers. API zw.deviceStorage.*

const credentials = {
  username: await zw.deviceStorage.get("username_salesforce"),
  password: await zw.deviceStorage.get("password_salesforce"),
};
await zw.deviceStorage.set("last_login_salesforce", String(Date.now()));

Reference: Device Storage (upcoming)


Utilities

Add delays and write logs. API zw.delay(), zw.log(), zw.logTemp()

await zw.log("starting run");
await zw.delay({ min: 1500, max: 2500 });

Reference: Utilities (upcoming)


Browser and Context

Launch, quit, and manage the active browser context and pages. API zw.browserContext.*

// @zw-run-locally

// Example (abridged). See the reference for all options.
await zw.browserContext.launch({
  contextControls: { persistent: true, keepAlive: true }, // more controls available
  launchOptions: { headless: true /* proxy, userDataDir, ... */ },
  contextOptions: { /* recordVideo, geolocation, ... */ },
  scripts: [],
  cookies: [],
  onContextReady: () => { zw.state.access().contextLaunched = true },
  // see the reference for the full list of options
});

const contextInfo = await zw.browserContext.getContextInfo();
await zw.log("context", contextInfo);
await zw.browserContext.quit();

Reference: Browser Context (upcoming)


Metadata

Read TaskBot and agent details. API zw.getTaskbotInfo(), zw.getAgentInfo()

const taskbotInfo = await zw.getTaskbotInfo();
await zw.log("variables", taskbotInfo.variables);

const agent = zw.getAgentInfo();
await zw.log("agent version", agent.version);

Reference: Metadata (upcoming)


Notes

  • Local vs. browser Some methods only work when the code runs locally. To run locally, either enable the Run locally in the app checkbox in the block UI or include // @zw-run-locally anywhere in your snippet (top is recommended for visibility).

Last updated

Was this helpful?