document/content/docs/introduction/guide/dashboard/workflow/sandbox.en.mdx
Runs simple JavaScript code for complex data processing. Code executes in a sandbox with no access to network requests, DOM, or async operations. For advanced use cases, use an HTTP node instead.
Important Notes
fastgpt-sandbox image and set the CODE_SANDBOX_URL environment variable.Add the variables your code needs via custom inputs. In the code's main function, destructure them by matching name.
As shown above, the custom inputs include data1 and data2, which can be destructured with the same names in the main function.
You must return an object.
In custom outputs, add variable names to retrieve values from the corresponding object keys. For example, the image above returns:
{
result: data1,
data2
}
This object has 2 keys: result and data2 (JS shorthand where key = data2, value = data2). You can then add 2 custom output variables to access the values under each key.
Returns after a 1-second delay:
async function main({data1, data2}){
await delay(1000)
return {
result: "111"
}
}
function main({input}){
return {
result: countToken(input)
}
}
Useful for converting SVG images to base64 format for display.
function main({input}){
return {
/*
param1: input - the string to convert
param2: base64 prefix
*/
result: strToBase64(input,'data:image/svg+xml;base64,')
}
}
Works the same as Node.js crypto's createHmac method.
function main({secret}){
const {sign,timestamp} = createHmac('sha256',secret)
return {
sign,timestamp
}
}