Skip to main content

How to Call Custom Functions in XanoScript

Custom functions in XanoScript are reusable logic blocks defined in the functions/ directory. To execute a custom function from your API, another function, or a task, use the function.run statement.

Syntax

function.run <function_name> {
	input = {
		<param1>: <value1>,
		<param2>: <value2>,
		// ...additional parameters
	}
} as <result_variable>
  • <function_name>: Path to your function, e.g., maths/calculate_total.
  • input: Object containing all required and optional parameters as defined in the function’s input block.
  • as <result_variable>: Stores the function’s response for further use.

Example

Suppose you have a function maths/calculate_total:
function maths/calculate_total {
	input {
		int quantity
		decimal price_per_item
	}
	stack {
		var $total {
			value = $input.quantity * $input.price_per_item
		}
	}
	response = $total
}
To call it:
function.run maths/calculate_total {
	input = {
		quantity: 5,
		price_per_item: 20
	}
} as $result

Usage Tips

  • Always match input parameter names and types to the function’s definition.
  • Use the returned variable ($result above) in subsequent logic or as your API response.
  • You can call functions from any other logic.

Error Handling

If the function uses precondition or input filters, invalid inputs will throw an error. Handle errors using try_catch if needed.

Best Practices

  • Organize functions in subfolders for clarity.
  • Document your function’s inputs and outputs.
I