Skip to main content

Execute JavaScript with the Code block

Learn how to use JavaScript code in a workflow.

JavaScript is the primary method for manipulating and transforming data in Retool. Use Code blocks to write custom JavaScript code that can transform data and perform complex logic. You can also use popular JavaScript libraries to further extend the functionality of workflows.

Add a JavaScript Code block

To use JavaScript in a workflow, add a Code block to the canvas and select JavaScript.

Write and execute JavaScript code

You can build complex logic or manipulate data using JavaScript methods like map(). For example, you could transform an array of customer records using map():

Map
const data = query1.data;

return data.map((customer) => ({
fullName: customer.name,
emailAddress: customer.email,
}));

In general, Retool recommends you visually construct conditional statements with Branch blocks or filter query results using Filter blocks.

JavaScript Code blocks can also call functions—reusable blocks that operate outside the workflow control flow. Functions allow your workflow to perform actions only when required and can receive parameters to use.

Use JavaScript libraries

Retool includes support for a selection of popular JavaScript libraries which you can use in a workflow. You can browse and add libraries, configure their imports, and use them in your workflow.

Available libraries

You can use the following JavaScript libraries in Retool Workflows. Additional libraries are available for Self-hosted Retool deployments.

LibraryDescription
csvA mature CSV toolset a with simple API, full of options and tested against large datasets.
date-fnsModern JavaScript date utility library.
rambdaLightweight and faster alternative to Ramda with included TS definitions.
xlsxSheetJS Spreadsheet data parser and writer.
crypto-jsJavaScript library of crypto standards.
openaiNode.js library for the OpenAI API.
node-html-parserA very fast HTML parser, generating a simplified DOM, with basic element query support.
markdown-itA modern pluggable Markdown parser.
markedA Markdown parser built for speed.
exponential-backoffA utility that allows retrying a function with an exponential delay between attempts.
mathjsMath.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif.
object-assignES2015 Object.assign() ponyfill.
underscore.stringString manipulation extensions for Underscore.js javascript library.
ajvAnother JSON Schema Validator.
cloneDeep cloning of objects and arrays.
bignumber.jsA library for arbitrary-precision decimal and non-decimal arithmetic.
deep-equalnode's assert.deepEqual algorithm.
dateformatA node.js package for Steven Levithan's excellent dateFormat() function.
crc-32Pure-JS CRC-32.
xmlparseran xml2json package for nodejs.
moment-timezoneParse and display moments in any timezone.
lodashLodash modular utilities.
numbroFormat and manipulate numbers.
papaparseFast and powerful CSV parser for the browser that supports web workers and streaming large files. Converts CSV to JSON and JSON to CSV.
uuidRFC4122 (v1, v4, and v5) UUIDs.
ssh2-sftp-clientssh2 sftp client for node.
plotlySimple node.js wrapper for the plot.ly API.
jspdfPDF Document creation from JavaScript.
datetimeDate and time formatting.
algoliasearchA fully-featured and blazing-fast JavaScript API client to interact with Algolia API.
fast-xml-parserValidate XML, Parse XML, Build XML without C/C++ based libraries.
pdfkitA PDF generation library for Node.js.
ndjsonStreaming newline delimited json parser + serializer.
jszipCreate, read and edit .zip files with JavaScript http://stuartk.com/jszip.
intercom-clientOfficial Node bindings to the Intercom API.
pusherNode.js client to interact with the Pusher Channels REST API.
google-libphonenumberThe up-to-date and reliable Google's libphonenumber package for node.js.
recursive-diffFind diff between any two variables, where variables are valid JavaScript data types, such as string, numeric, array or object.
jmespathJMESPath implementation in JavaScript.
axiosPromise based HTTP client for the browser and node.js.

Manage libraries

Navigate to the Libraries tab to manage JavaScript libraries. This tab lists all currently enabled libraries and their version. Select a library to view more information, configure imports, or view its documentation.

Add a library

note

Retool does not currently support adding custom libraries.

Retool enables a selection of libraries by default, such as numbro and lodash. Click Add to browse all available libraries. You can search the list of libraries and enable them by clicking the checkbox.

Remove a library

To remove a library from your workflow, click ••• for the library and select Delete. Before removing a library, ensure it is not in use anywhere in your workflow to prevent run errors.

Configure library imports

Retool automatically configures the necessary imports for JavaScript and Python libraries and includes them in the configuration details modal. These import statements allow you to import libraries to use in the rest of your Code blocks.

You can edit the default imports if you want to use destructured imports or change variables, but you should write the rest of your code in Code blocks.

Use libraries in a workflow

You can reference libraries using JavaScript Code blocks or as inline JavaScript in other blocks using {{ }}. You can also use require() to include libraries directly within JavaScript Code blocks.

For example, you could use marked to convert Markdown into HTML. This can be useful to generate reports and send them as HTML-formatted rich emails.

return marked.parse(query1.data.message);

Use a library in a workflow

Configure block settings

You can configure the following settings for Code blocks.

Timeout after (ms)

The duration to wait before the block times out. Default is 10000.

Retry count

The maximum number of attempts to retry the query if an error is returned.

Interval

The minimum length of time to wait between query retries. This is useful if a query is triggering a rate limit.

Exponential

Whether to increase the interval between retries, also known as exponential backoff. This is useful for handling rate limits.

Finally

Whether to stop the workflow and return an error if the block fails, or continue to allow an error handler to run.

Coefficient

When exponential backoff is enabled, the constant factor by which the time delay between retry attempts increases after each failure.

Max interval

When exponential backoff is enabled, the maximum time to delay between retries.