How to create and use variables in API JSON and JS code steps
In JavaScript Code and API JSON steps, users can create variables to store values once and use them dynamically. At runtime, viaSocket replaces the variable with its actual value, making workflows flexible, reusable, and secure.
How to create a variable
You can create variables in both the JavaScript Code step and the HTTP API Request step to use dynamic values without hardcoding them.
Steps to create a variable
Open your workflow editor and add the desired action step (Custom JS Code or HTTP API Request).
Click on the Code editor (for JS) or the Body editor (for API).
Below the editor, go to the Data tab and select ➕ Add new variable.
_compressed.png)
In the Variable Name field, enter a descriptive name for your variable.
_compressed.png)
Click Add to create the variable.
Assign a value to the variable. You can also map dynamic data from previous steps using the chip.
_compressed.png)
How to use the variable
To use a variable in your JavaScript code or API JSON:
_compressed.png)
Place your cursor where you want the value.
Click on Data.
Select the variable you want to map from the list of created variables.
At runtime, viaSocket automatically replaces the variable with its assigned or mapped value.
Data type of a variable
In viaSocket, the data type of a variable is automatically determined based on the value you assign:
Numbers → number
Text → string
Boolean (true/false) → boolean
Array, JSON, Object → corresponding array or object
This ensures the variable works correctly in both JavaScript and API JSON steps.
Variable scope
The scope of a variable is limited to the step in which it is created.
A variable can only be accessed and used within the same JavaScript Code step or API JSON step where it was defined.
It cannot be used directly in other steps of the workflow.
To use data across multiple steps, pass the output of one step to the next instead of relying on step-specific variables.
Best Practices
Use descriptive names like customerEmail, orderId, authToken.
Each variable must have a unique name; variable names cannot be reused in the same step.
Assign default values or map dynamic data as needed to maintain flexibility.
Reuse variables within a step whenever possible to reduce redundancy and improve readability.