NicoChatNicoChatDocsSearch the docs…
Sign in

JavaScript Function

NicoChat now allows your users to carry out complex flow logic using the JavaScript action. Users can write JavaScript code to build various complex flows and formulas that NicoChat does not yet support natively.

2026-09-07

Selecting the JavaScript action node

Create an Action node block, click on Advanced Actions and select the JavaScript Function node.

JavaScript Function Node

A JavaScript function works exactly like a function. All parameters are first parsed in the JavaScript block before they can be used. The top part of the action is used for the input.

Parameter Name: add the name of the variable you are parsing.

Parameter Value: add the Contact Field you want to parse into the JavaScript function.

Test Value: add a test value that can be used here to run tests.

The function block supports multiple JavaScript libraries.

These JavaScript libraries extend the functionality of the native function block.

These are the available libraries, with the version the block loads:

Library
Version
What it is for
Lodash
4.17.21
array, object and string helpers: map, groupBy, cloneDeep
Collect.js
4.36.1
chainable collections, in the Laravel style
CryptoJS
4.2.0
hashing and encryption: MD5, SHA, HMAC, AES
Day.js
1.11.13
dates and times with a light API — the default choice for new code
Compromise
14.14.0
English NLP: pulls names, dates and nouns out of a text
Chrono
2.6.1
reads a date written in natural language ("tomorrow at 3pm") and returns a Date
Cheerio
1.0.0-rc.12
traverses and scrapes HTML with jQuery syntax
jsonwebtoken
9.0.3
signs and verifies JWTs
Jsrsasign
11.1.0
cryptography, X.509 certificates and JWS/JWT in pure JavaScript
libphonenumber-js
1.10.21
validates and formats phone numbers by country
Moment.js
2.30.1
dates and times; the project is in legacy mode, prefer Day.js
Sentiment
5.0.2
scores the sentiment of a text, from negative to positive

Tick in the block the libraries the code will use — only the selected ones are available at run time.

In the function block, you can write your JavaScript code.

After writing the code, you always need to return the value, because the function block works like a regular function.

Note: native JavaScript methods that require a browser, such as console.log, do not work in the JavaScript Function node.

You can map the results of your code to update certain Contact Fields.

Note: when handling JSONs, all of them need to be parsed before being used inside the code.

How parameters reach the code

Every parameter value reaches the code as text, even when the variable holds a number, a list or a JSON. Before looping over an array or reading an object’s keys, convert it with JSON.parse.

js
// carrinho chega como texto: '[{"preco":30,"qtd":2},{"preco":15,"qtd":1}]'
const itens = JSON.parse(carrinho);

return {
  total: itens.reduce(function (s, i) { return s + i.preco * i.qtd; }, 0),
  quantidade: itens.length
};

The parameter name already exists as a variable inside the code — do not declare it again. Writing const carrinho = ... for a parameter named carrinho breaks the run.

The code does not resolve {{variable}}: whatever is written in the editor is literal text. Contact data comes in only through the Parameter Value field, and that is where the variable is replaced.

The return value and the mapping

Whatever the function returns is delivered inside result. In the mapping, the total key of a returned object has the path $.result.total — if you write just total or $.total, NicoChat completes it to the right form.

Return in the code
Path in the mapping
What gets saved
return 45;
$.result
the number 45
return { total: 45 };
$.result.total
the number 45
return ['a', 'b'];
$.result[0]
the text a
return { itens: [...] };
$.result.itens
the whole list, as JSON

Mapping $.result on its own saves the whole object or array into a single variable. To store each piece in a different field, return an object and map key by key.

Limits of the block

  • No network access. To call an API, use the External Request (API) block and bring the result here.
  • Short execution time, around 5 seconds. An infinite loop or heavy processing is cut off.
  • eval is not allowed.
  • Nothing that depends on a browser: console.log, document and window do not exist here.

Working with JSON

There is no dedicated JSON block in NicoChat: summing, counting, finding the largest value, extracting a key or removing an item is work for this block. After JSON.parse, it is plain JavaScript.

What you want
How to do it
Sum or count
reduce and length
Largest or smallest value
Math.max and Math.min over a map
Pull one column out of the list
map
List an object’s keys
Object.keys
Get one item by the value of a key
find
Remove items by a condition
filter

The two places where you write JavaScript

Besides this block, there is JavaScript in the pre-processing of the External Request (API) block. The two do not behave the same, and mixing them up is the most common cause of an empty mapping.

JavaScript Function
API pre-processing
Input
the parameters you declare, always as text — JSON needs JSON.parse
the payload variable, with the API response ready to use
Return
goes into result — map $.result.<key>
stays at the root — map $.<key>
What for
transform data that is already in the flow
handle an API response before mapping it