How to Call Custom Functions in XanoScript
Custom functions in XanoScript are reusable logic blocks defined in thefunctions/
directory. To execute a custom function from your API, another function, or a task, use the function.run
statement.
Syntax
<function_name>
: Path to your function, e.g.,maths/calculate_total
.input
: Object containing all required and optional parameters as defined in the function’sinput
block.as <result_variable>
: Stores the function’s response for further use.
Example
Suppose you have a functionmaths/calculate_total
:
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 usesprecondition
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.