Skip to main content

What are Lambda Functions?

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 higher order filters.

Using the Lambda AI Assistant

1

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 'AI Assistant' 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.

Using NPM Packages

Before you beginIt is highly recommended that you include version numbers in your imports to ensure code stability; this will allow you time to verify updates to packages and avoid any potential issues.
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:
Native Node libraries that are native can be accessed with a node: prefix instead of npm:
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.
Private packages are imported using query parameters on the package name:
This is detailed in the example when you open the Lambda editor. The above example assumes you have an environment variable called NPM_REGISTRY_TOKEN, although it doesn’t have to be an environment variable it’s definitely best practice. If your package is ESM ("type": "module" in package.json), require() will fail. await import(...) works and is the safest default. So if you’re unsure what your package exports, use await import().

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.

Reading Files

Read Text Files

Read Binary Files

Read File Stream

Writing Files

Write Text Files

Write Binary Files

Append to Files

File Operations

Check if File Exists

Copy Files

Rename/Move Files

Delete Files

Directory Operations

Create Directory

Read Directory Contents

Remove Directory

File Information

Get File Stats

File Permissions

Temporary Files and Directories

Create Temporary File

Create Temporary Directory

Working with Paths

Deno provides a path module for working with file paths:

Important Notes for Xano Lambda Environment

  1. In Xano’s Lambda environment, filesystem operations are primarily useful for temporary file operations
  2. Use the /tmp directory for temporary file storage
  3. Files in the Lambda environment are ephemeral - they will not persist between function calls
  4. Filesystem operations are useful for:
    • Processing uploaded files
    • Generating temporary files for processing
    • Creating logs or debug information