Skip to main content

Introduction

The Custom Functions primitive lets you define reusable logic using XanoScript. Each Custom Function can be inserted into any other place you’re building logic in Xano; even other Custom Functions. Custom Functions will typically:
  • Declare their name and description
  • Accept inputs
  • Run one or more operations in a stack
  • Return a response (optional)

Anatomy

Every XanoScript Custom Function follows a predictable structure. Here’s a quick visual overview of its main building blocks — from declaration at the top to settings at the bottom.

You can find more detail about each section by continuing below.

Declaration

Every Custom Function starts with a declarative header that specifies its type, name, and description.
XanoScript
function utilities/create_camel_case_slug {
description = "Converts a text string into a camelCase slug by removing special characters, splitting it into words, and then capitalizing the first letter of each word except the first. This is useful for creating clean, programmatic identifiers from user-generated text."\
...
}
ElementRequiredDescription
functionDeclares an Custom Function primitive.
api_nameThe unique path for the custom function (e.g., utilities/create_camel_case_slug). Take note of the path defining a folder with utilities/ as well; you can do this to organize your custom functions into folders.
descriptionnoOptional human-readable description of the custom function.

Section 1: Inputs

The input block defines the data that will be sent to the Custom Function. You can declare types, optionality, and filters:

Hover over this image to see the XanoScript version

For each input, you can:
  • Declare its type (text, email, password, etc.)
  • Mark it as optional (?)
  • Apply filters (filters=trim|lower)

Learn more about the available data types


Section 2: Stack

The stack block contains the actual logic that will be executed when the Custom Function is executed.

Hover over this image to see the XanoScript version


Each block inside stack corresponds to a function available in Xano’s visual builder:
  • conditional — Executes different logic based on a condition
  • foreach — Loop through a list of items
  • text.append — Append a value to a text variable
  • text.capitalize — Capitalize a text value
  • text.split — Split a text value into an array
  • text.regex_replace — Replace a text value with a regex
  • text.to_lower — Convert a text value to lowercase
  • text.count — Count the number of items in an array
  • text.slice — Slice an array
The syntax mirrors how you’d configure these functions visually, but expressed textually. The actual behavior is the same — refer to the function’s existing docs for complete details.

Review all available functions and their XanoScript in the function reference


Section 3: Response

The response block defines what data your API returns:

Hover over this image to see the XanoScript version

  • The value assignment determines the JSON returned by the Custom Function.
  • Variables captured in the stack (e.g., $camel_case_slug) can be returned here.

Settings

Custom Function primitives support several optional settings that control authentication, tagging, caching, and version history. These settings are defined at the root level of the API block, after the input, stack, and response blocks. They affect how the endpoint behaves, how it’s documented, and how responses are cached.
SettingTypeRequiredDescription
descriptionstringnoA human-readable description of the API. Appears in the builder and documentation.
tagsarray[string]noA list of tags used to categorize and organize the API in your workspace.
historyobjectnoConfigures version inheritance and history behavior. {inherit: true} allows this API to inherit history settings from the workspace.
cacheobjectnoConfigures caching behavior for this API. See below for supported fields.
The cache block configures caching behavior for the API:
FieldTypeDescription
ttlnumber (seconds)Time-to-live for cache entries. A value of 0 disables caching.
inputbooleanWhether the request body and query parameters are factored into the cache key.
authbooleanWhether authentication state (e.g., user ID) is included in the cache key.
datasourcebooleanWhether the datasource context is factored into the cache key.
ipbooleanWhether the request IP address is included in the cache key.
headersarray[string]A list of headers whose values should be included in the cache key.
envarray[string]A list of environment variables whose values should be included in the cache key.

Detailed Example

Below, you’ll see a complete example of a typical signup API endpoint.
XanoScript
function utilities/create_camel_case_slug {
  description = "Converts a text string into a camelCase slug by removing special characters, splitting it into words, and then capitalizing the first letter of each word except the first. This is useful for creating clean, programmatic identifiers from user-generated text."
  input {
    text text {
      description = "The input text to be converted into a camelCase slug."
    }
  }

  stack {
    var $words_array {
      description = "Clean and split the input text into an array of words."
      value = "/[^a-zA-Z0-9s]/"|regex_replace:"":$input.text|to_lower|split:" "|filter:"return $this != '';"
    }
  
    var $camel_case_slug {
      description = "Initialize the slug with the first word."
      value = ""
    }
  
    conditional {
      if (($words_array|count) > 0) {
        var.update $camel_case_slug {
          value = $words_array|first
        }
      }
    }
  
    var $words_array_sliced {
      description = "Get the rest of the words to be processed."
      value = ($words_array|count) > 1 ? ($words_array|slice:1:-1) : []
    }
  
    foreach ($words_array_sliced) {
      each as $word {
        text.append $camel_case_slug {
          description = "Capitalize each subsequent word and append it to the slug."
          value = $word|capitalize
        }
      }
    }
  }

  response {
    value = $camel_case_slug
  }

  tags = ["utility functions"]
}

What’s Next

Now that you understand how to define APIs in XanoScript, here are a few great next steps:

Explore the function reference

Learn about the built-in functions available in the stack to start writing more complex logic.
https://mintlify.s3.us-west-1.amazonaws.com/xano-997cb9ee/images/vscode.svg

Try it out in VS Code

Use the XanoScript VS Code extension with Copilot to write XanoScript in your favorite IDE.

Learn about Background Tasks

Similar to APIs and Custom Functions, Tasks are easy to build logic, but they run in the background on a specific schedule.
I