Skip to main content
NOTICEXanoScript is in beta and it is strongly recommended that you do not use it in production at this time.While there should be significant parity between the visual builder and XanoScript, we are still fine-tuning and adjusting XanoScript, and breaking changes may be introduced.
A note about Custom FunctionsAPIs and custom functions behave largely the same in XanoScript, so you can use this documentation to serve as a resource for both.The only additional resource you will need is some custom function specific XanoScript, which is available below. Starting at the input block, everything is the same as an API.
function "My Custom Function" {
  description = "Here's a description" 

Accessing XanoScript in your Function Stacks

Use the quick toggle above the input blocks to switch between XanoScript and the visual builder.

What does a XanoScript function stack look like?

This is a typical Get Record API endpoint.
Here is the XanoScript representation.
query "user/{user_id}" verb=GET {
  input {
    int user_id? filters=min:1
  }

  stack {
    db.get user {
      field_name = "id"
      field_value = $input.user_id
    } as model
  
    precondition if (`$model != null`) {
      error_type = "notfound"
      error = "Not Found"
    }
  }

  response {
    value = $model
  }
}

Part 1 - Definition and Inputs

query "user/{user_id}" verb=GET {
  input {
    int user_id? filters=min:1
  }
query "user/{user_id}" verb=GET { Here, we define what we are building (query is an API endpoint), give it a name user/{user_id}, and assign the verb verb=GET
  input {
    int user_id? filters=min:1
  }
Finally, we define our inputs: an int input called user_id. We’ve also applied a ? after user_id to indicate that this value is not required, and added a filter to enforce a minimum value of 1.

Part 2 - The function stack

The function stack is contained in an object that starts with stack { Each function name is designed to be human readable, just like the function stack. They will be immediately followed by an object that provides those function parameters. If the function outputs to a variable, that is defined after the object is closed. Here, we’re starting with a Get Record function on the table named user.
Function StackXanoScript
    db.get user {
    field_name = "id"
    field_value = $input.user_id
    } as model

Part 3 - The response

Once your inputs and function stack are defined, we can build our response. This begins with a response { object. Our response will be a JSON object, which is standard.
Function StackXanoScript
  response {
    value = $model
  }

Using Functions

Functions in XanoScript are designed to be as close to second nature as possible for someone who is already familiar with building in the function stack. We’ve designed the function names and options to ensure as much parity with the visual builder as possible. Functions will typically begin with a namespace — basically, a category that the function is a part of. Each namespace is separated with a period. As an example, the namespace below is what’s used for database operations — db.
db.
Sometimes, functions will use multiple namespaces, such as cloud.aws.opensearch. This is infrequent though — you will usually only find yourself using a single namespace.
After the namespace, we’d use the function name, such as:
db.get
This is the XanoScript command for Get Record. From here, things get a little more specific depending on the function you are using. For example, db.get is immediately followed by the name of the table we’re querying.
db.get user
After we’ve defined our function, we can start providing the parameters that function requires, if any. Parameters are wrapped in {} characters. Get Record asks for a field_name and a field_value. Parameters do not require any quotation marks or other special characters for the parameter name, but may require them for the value depending on the data type (for example, "hello" vs 123).
Function StackXanoScript
  response {
    value = $model
  }
Now that you have an understanding of how to write XanoScript in your function stacks, you can start building! There are a couple of different recommended ways to get started.
1

Build a function stack in the visual builder and review the generated XanoScript.

Add some functions to a function stack, and then click > XanoScript in the upper-right corner.Try changing some values or adding a simple function, and watch the visual builder update with the changes you’ve made. You can switch from building with XanoScript to the visual builder at any time, and always see your changes reflected.
2

Review the function reference and build with XanoScript from scratch.

Use the function reference to see how all of our functions are written using XanoScript, and try building your own!

function-reference