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()
:
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.
- Available on Cloud and Self-hosted
- Available on Self-hosted only
Library | Description |
---|---|
csv | A mature CSV toolset a with simple API, full of options and tested against large datasets. |
date-fns | Modern JavaScript date utility library. |
rambda | Lightweight and faster alternative to Ramda with included TS definitions. |
xlsx | SheetJS Spreadsheet data parser and writer. |
crypto-js | JavaScript library of crypto standards. |
openai | Node.js library for the OpenAI API. |
node-html-parser | A very fast HTML parser, generating a simplified DOM, with basic element query support. |
markdown-it | A modern pluggable Markdown parser. |
marked | A Markdown parser built for speed. |
exponential-backoff | A utility that allows retrying a function with an exponential delay between attempts. |
mathjs | Math.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-assign | ES2015 Object.assign() ponyfill. |
underscore.string | String manipulation extensions for Underscore.js javascript library. |
ajv | Another JSON Schema Validator. |
clone | Deep cloning of objects and arrays. |
bignumber.js | A library for arbitrary-precision decimal and non-decimal arithmetic. |
deep-equal | node's assert.deepEqual algorithm. |
dateformat | A node.js package for Steven Levithan's excellent dateFormat() function. |
crc-32 | Pure-JS CRC-32. |
xmlparser | an xml2json package for nodejs. |
moment-timezone | Parse and display moments in any timezone. |
lodash | Lodash modular utilities. |
numbro | Format and manipulate numbers. |
papaparse | Fast and powerful CSV parser for the browser that supports web workers and streaming large files. Converts CSV to JSON and JSON to CSV. |
uuid | RFC4122 (v1, v4, and v5) UUIDs. |
ssh2-sftp-client | ssh2 sftp client for node. |
plotly | Simple node.js wrapper for the plot.ly API. |
jspdf | PDF Document creation from JavaScript. |
datetime | Date and time formatting. |
algoliasearch | A fully-featured and blazing-fast JavaScript API client to interact with Algolia API. |
fast-xml-parser | Validate XML, Parse XML, Build XML without C/C++ based libraries. |
pdfkit | A PDF generation library for Node.js. |
ndjson | Streaming newline delimited json parser + serializer. |
jszip | Create, read and edit .zip files with JavaScript http://stuartk.com/jszip. |
intercom-client | Official Node bindings to the Intercom API. |
pusher | Node.js client to interact with the Pusher Channels REST API. |
google-libphonenumber | The up-to-date and reliable Google's libphonenumber package for node.js. |
recursive-diff | Find diff between any two variables, where variables are valid JavaScript data types, such as string, numeric, array or object. |
jmespath | JMESPath implementation in JavaScript. |
axios | Promise based HTTP client for the browser and node.js. |
Library | Description |
---|---|
purecloud-platform-client-v2 | A JavaScript library to interface with the PureCloud Platform API. |
mustache | Logic-less {{mustache}} templates with JavaScript. |
sqlstring | Simple SQL escape and format for MySQL. |
postgres-interval | Parse Postgres interval columns. |
jspdf-autotable | Generate PDF tables with JavaScript (jsPDF plugin). |
Let us know if there are any JavaScript libraries you'd like Retool to include.
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
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);
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.