Xano Documentation
  • 👋Welcome to Xano!
  • 🌟Frequently Asked Questions
  • 🔐Security & Compliance (Trust Center)
  • 🙏Feature Requests
  • 💔Known Issues
  • Before You Begin
    • Using These Docs
    • Where should I start?
    • Set Up a Free Xano Account
    • Key Concepts
    • The Development Life Cycle
    • Navigating Xano
    • Plans & Pricing
  • The Database
    • Designing your Database
    • Database Basics
      • Using the Xano Database
      • Field Types
      • Relationships
      • Database Views
      • Export and Sharing
      • Data Sources
    • Migrating your Data
      • Airtable to Xano
      • Supabase to Xano
      • CSV Import & Export
    • Database Performance and Maintenance
      • Storage
      • Indexing
      • Maintenance
      • Schema Versioning
  • 🛠️The Function Stack
    • Building with Visual Development
      • APIs
        • Swagger (OpenAPI Documentation)
      • Custom Functions
        • Async Functions
      • Background Tasks
      • Triggers
      • Middleware
      • Configuring Expressions
      • Working with Data
    • Functions
      • AI Tools
      • Database Requests
        • Query All Records
          • External Filtering Examples
        • Get Record
        • Add Record
        • Edit Record
        • Add or Edit Record
        • Patch Record
        • Delete Record
        • Bulk Operations
        • Database Transaction
        • External Database Query
        • Direct Database Query
        • Get Database Schema
      • Data Manipulation
        • Create Variable
        • Update Variable
        • Conditional
        • Switch
        • Loops
        • Math
        • Arrays
        • Objects
        • Text
      • Security
      • APIs & Lambdas
        • Realtime Functions
        • External API Request
        • Lambda Functions
      • Data Caching (Redis)
      • Custom Functions
      • Utility Functions
      • File Storage
      • Cloud Services
    • Filters
      • Manipulation
      • Math
      • Timestamp
      • Text
      • Array
      • Transform
      • Conversion
      • Comparison
      • Security
    • Data Types
      • Text
      • Expression
      • Array
      • Object
      • Integer
      • Decimal
      • Boolean
      • Timestamp
      • Null
    • Environment Variables
    • Additional Features
      • Response Caching
  • Testing and Debugging
    • Testing and Debugging Function Stacks
    • Unit Tests
    • Test Suites
  • CI/CD
  • File Storage
    • File Storage in Xano
    • Private File Storage
  • Realtime
    • Realtime in Xano
    • Channel Permissions
    • Realtime in Webflow
  • Maintenance, Monitoring, and Logging
    • Statement Explorer
    • Request History
    • Instance Dashboard
      • Memory Usage
  • Building Backend Features
    • User Authentication & User Data
      • Separating User Data
      • Restricting Access (RBAC)
      • OAuth (SSO)
    • Webhooks
    • Messaging
    • Emails
    • Custom Report Generation
    • Fuzzy Search
    • Chatbots
  • Xano Features
    • Snippets
    • Instance Settings
      • Release Track Preferences
      • Static IP (Outgoing)
      • Change Server Region
      • Direct Database Connector
      • Backup and Restore
      • Security Policy
    • Advanced Back-end Features
      • Xano Link
      • Developer API (Deprecated)
    • Metadata API
      • Master Metadata API
      • Tables and Schema
      • Content
      • Search
      • File
      • Request History
      • Workspace Import and Export
      • Token Scopes Reference
  • Xano AI
    • Building a Backend Using AI
    • Get Started Assistant
    • AI Database Assistant
    • AI Lambda Assistant
    • AI SQL Assistant
    • API Request Assistant
    • Template Engine
    • Streaming APIs
  • AI Tools
    • MCP Servers
      • Connecting Clients
      • MCP Functions
  • Xano Transform
    • Using Xano Transform
  • Xano Actions
    • What are Actions?
    • Browse Actions
  • Team Collaboration
    • Realtime Collaboration
    • Managing Team Members
    • Branching & Merging
    • Role-based Access Control (RBAC)
  • Agencies
    • Xano for Agencies
    • Agency Features
      • Agency Dashboard
      • Client Invite
      • Transfer Ownership
      • Agency Profile
      • Commission
      • Private Marketplace
  • Enterprise
    • Xano for Enterprise
    • Enterprise Features
      • Microservices
      • Tenant Center
      • Compliance Center
      • Security Policy
      • Instance Activity
      • Deployment
      • RBAC (Role-based Access Control)
      • Xano Link
  • Your Xano Account
    • Account Page
    • Billing
    • Referrals & Commissions
  • Troubleshooting & Support
    • Error Reference
    • Troubleshooting Performance
      • When a single workflow feels slow
      • When everything feels slow
      • RAM Usage
      • Function Stack Performance
    • Getting Help
      • Granting Access
      • Community Code of Conduct
      • Community Content Modification Policy
  • Special Pricing
    • Students & Education
    • Non-Profits
  • Security
    • Best Practices
Powered by GitBook
On this page
  • What are Lambda Functions?
  • How do I write Lambda functions in Xano?
  • Using the Lambda AI Assistant
  • Using NPM Packages
  • Working with Files
  • Reading Files
  • Writing Files
  • File Operations
  • Directory Operations
  • File Information
  • Temporary Files and Directories
  • Working with Paths
  • Important Notes for Xano Lambda Environment

Was this helpful?

  1. The Function Stack
  2. Functions
  3. APIs & Lambdas

Lambda Functions

Last updated 18 days ago

Was this helpful?

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 .

Using the Lambda AI Assistant

Using NPM Packages

Before you begin

It 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:

const { default: Decamelize } = await import ("npm:decamelize");

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.

Reading Files

Read Text Files

// Read entire file as string
const content = await Deno.readTextFile("/path/to/file.txt");

Read Binary Files

// 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
  }
}

Copy Files

await Deno.copyFile("/path/source.txt", "/path/destination.txt");

Rename/Move Files

await Deno.rename("/path/oldname.txt", "/path/newname.txt");

Delete Files

await Deno.remove("/path/to/file.txt");

Directory Operations

Create Directory

// 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");
}

Remove Directory

// Remove empty directory
await Deno.remove("/path/to/empty-dir");

// Remove directory with contents
await Deno.remove("/path/to/dir", { recursive: true });

File Information

Get File Stats

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:

import { join, dirname, basename, extname } from "https://deno.land/std/path/mod.ts";

const filePath = join("dir", "subdir", "file.txt"); // OS-appropriate path joining
const dir = dirname("/path/to/file.txt"); // "/path/to"
const base = basename("/path/to/file.txt"); // "file.txt"
const ext = extname("/path/to/file.txt"); // ".txt"

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

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

👍
👎