Lambda functions allow you to execute JavaScript or TypeScript inside of your Xano function stacks. You may prefer to do this if you are porting old workflows to Xano and already have the code written, or maybe you just prefer to write code instead of using the function stack.
You can also use Lambda functions to leverage custom NPM packages.
How do I write Lambda functions in Xano?
Special Variables
Lambdas have the ability to reference all the available data just like normal function stack statements. All special variables are prefixed with a $ symbol.
Xano variables are accessible through the $var special variable. To access a Xano variable named title, you would reference it as $var.title.
Xano inputs are accessible through the $input special variable. To access a Xano input named score, you would reference it as $input.score.
Xano environment variables are accessible through the $env special variable. To access a Xano environment variable named ip, you would reference it as $env.ip.
The authenticated user details are accessible through the $auth special variable. The most common members of this variable include $auth.id and $auth.extras. If there is no authenticated user, then $auth.id will evaluate as 0.
Context Variables
Depending on how you use a Lambda, you may have support to access some additional variables, known as context variables. These follow the same naming convention as special variables by using a $ prefix. The most common context variables will be $this, $index, $parent, and $result. The meaning of these variables are best described within the examples of the .
Using the Lambda AI Assistant
Using NPM Packages
If you have an NPM package you'd like to use in your Lambda functions, you can import it using the following format:
When we want to utilize the functions imported from the package listed, we can do so like this:
return Decamelize($input.test);
Native Node libraries that are native can be accessed with a node: prefix instead of npm:
const { request } = await import("node:https");
Please note that not all NPM packages may function properly inside of Xano. If you encounter an issue importing a specific package, please reach out to our support team for further clarification.
Working with Files
Xano provides a comprehensive set of filesystem functions that allow you to read, write, and manipulate files and directories. These functions are available through the global Deno namespace.
// Read entire file as Uint8Array
const data = await Deno.readFile("/path/to/file.bin");
Read File Stream
// Open a file for reading as a stream
const file = await Deno.open("/path/to/large-file.txt");
// Create a reader from the file
const reader = file.readable.getReader();
// Read chunks
const { value, done } = await reader.read();
// Close the file when done
file.close();
Writing Files
Write Text Files
// Write string to file (creates or overwrites)
await Deno.writeTextFile("/path/to/output.txt", "Hello, world!");
Write Binary Files
// Write binary data to file
const data = new Uint8Array([104, 101, 108, 108, 111]);
await Deno.writeFile("/path/to/output.bin", data);
Append to Files
// Append to an existing file
await Deno.writeTextFile("/path/to/log.txt", "New log entry\n", { append: true });
File Operations
Check if File Exists
try {
const fileInfo = await Deno.stat("/path/to/file.txt");
const exists = fileInfo.isFile; // true if it's a file
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// File does not exist
} else {
// Other error
}
}
// Create a single directory
await Deno.mkdir("/path/to/dir");
// Create nested directories (like mkdir -p)
await Deno.mkdir("/path/to/nested/dir", { recursive: true });
Read Directory Contents
// List files and directories in a directory
for await (const entry of Deno.readDir("/path/to/dir")) {
console.log(entry.name, entry.isFile ? "file" : "directory");
}
const fileInfo = await Deno.stat("/path/to/file.txt");
console.log(fileInfo.size); // Size in bytes
console.log(fileInfo.mtime); // Last modification time
console.log(fileInfo.birthtime); // Creation time
console.log(fileInfo.isFile); // Is it a file
console.log(fileInfo.isDirectory); // Is it a directory
File Permissions
// Check if we have read permission
const canRead = await Deno.permissions.query({ name: "read", path: "/path/to/file.txt" });
Temporary Files and Directories
Create Temporary File
// Create a temp file and return its path
const tempFile = await Deno.makeTempFile();
Create Temporary Directory
// Create a temp directory and return its path
const tempDir = await Deno.makeTempDir();
Working with Paths
Deno provides a path module for working with file paths:
Give the assistant context by running your function stack first.
If you don't do this, you can still use the AI assistant, but it will make certain inferences that may not be correct.
2
Look for the button and click it to enable the assistant.
3
Ask the assistant for help as needed.
In this example, we're asking the assistant to write a function that imports the Decamelize library and applies it to our 'test' input.
4
Choose how to proceed with the assistant's suggestions.
You can click to add the suggestions to the Lambda function code, or you can copy it manually and place it as needed.
Be sure to rate the assistant's suggestion(s) using the and buttons. We'll use this information to improve the behavior of the assistant in future iterations.