Skip to main content

/images/icons/xs_temp.svg Return

return {
  value = "value to return"
}
ParameterPurposeExample
valueThe value to return from the function"Hello World", 123, {key: "value"}
return {
  value = $api_response|get:"data"
}
  • Returns a value from the function execution
  • Can return any data type (string, number, object, array, etc.)
  • Terminates function execution

/images/icons/xs_temp.svg Debug Log

debug.log {
  value = "Value to send"
}
ParameterPurposeExample
valueThe value to output to the debug log"Debug message", $variable, `response
debug.log {
  value = "Processing user: "|add:$user.id
}
  • Outputs a value to the debug log
  • Useful for development and troubleshooting
  • Can log any data type
  • Does not affect function execution

/images/icons/xs_temp.svg Precondition

precondition ($a == 1) {
  error_type = "notfound"
  error = "Error message to return"
  payload = "Payload"
}
ParameterPurposeExample
error_typeThe type of error to throw

Can be one of the following:
- standard
- notfound
- accessdenied
- toomamyrequests
- unauthorized
- badrequest
- inputerror

errorThe error message to display"Resource not found", "Access denied"
payloadAdditional data to include with the error"Error details", {reason: "Invalid input"}
precondition ($user.role != "admin") {
  error_type = "accessdenied"
  error = "Admin access required"
  payload = {
    required_role: "admin",
    current_role: $user.role
  }
}

/images/icons/xs_temp.svg Try / Catch

try_catch {
  try {
    // statements that may throw
    function.run risky_function { input = { foo: "bar" } } as $result
  }
  catch {
    debug.log { value = $error }
  }
  finally {
    debug.log { value = "Cleanup actions" }
  }
}
  • Executes code in the try block
  • If an error occurs, the catch block runs
  • The finally block always runs (optional)

/images/icons/xs_temp.svg Throw Error

throw {
  name = "inputerror"
  value = "A custom error message"
}
  • Immediately stops execution and throws an error
  • Can be caught by a surrounding try_catch block

/images/icons/xs_temp.svg Post Process

post_process {
  stack {
    debug.log { value = "Post-processing after response" }
  }
}
  • Executes after the main response is sent
  • Useful for logging, cleanup, or async side effects

/images/icons/xs_temp.svg Send Email

api.request {
  url = "https://api.sendgrid.com/v3/mail/send"
  method = "POST"
  params = {
    personalizations: [{ to: [{ email: "to@example.com" }] }],
    from: { email: "from@example.com" },
    subject: "Hello from XanoScript",
    content: [{ type: "text/plain", value: "This is the email body." }]
  }
  headers = []|push:"Authorization: Bearer " ~ $env.sendgrid_key
} as $email_response
  • Use api.request to send emails via external providers (e.g., SendGrid, Mailgun)
  • Set API keys in environment variables
  • Check $email_response for status
  • Checks a condition and throws an error if the condition is true
  • Allows specifying error type, message, and additional payload
  • Useful for validation and access control
  • Stops execution if condition is met

/images/icons/xs_temp.svg Stop and Debug

debug.stop {
  value = "Value to return"
}
ParameterPurposeExample
valueThe value to return when stopping execution"Debug stop message", $variable, {status: "stopped"}
debug.stop {
  value = "Stopping execution"
}
  • Immediately stops function execution
  • Returns the specified value
  • Useful for debugging and development
  • Can return any data type

/images/icons/xs_temp.svg Group

group {
  stack {
    util.sleep {
      value = 1
    }
  
    util.sleep {
      value = 2
    }
  }
}
ParameterPurposeExample
groupContainer for organizing operationsgroup { ... }
stackExecutes operations in sequencestack { ... }
valueDuration to sleep in seconds1, 2, 0.5
group {
  stack {
    util.sleep {
      value = 0.5
    }
    
    debug.log {
      value = "Half second passed"
    }
    
    util.sleep {
      value = 1
    }
  }
}
  • Groups related operations together
  • Stack ensures sequential execution
  • Sleep pauses execution for specified duration
  • Operations in stack execute in order

/images/icons/xs_temp.svg Sleep

util.sleep {
  value = 1
}
ParameterPurposeExample
valueNumber of seconds to pause execution1, 0.5, 2.5
util.sleep {
  value = 0.5
}
  • Pauses execution for specified number of seconds
  • Accepts decimal values for sub-second precision
  • Useful for rate limiting or creating delays

/images/icons/xs_temp.svg CSV Stream

stream.from_csv {
  value = $input.csv_input
  separator = ","
  enclosure = '"'
  escape_char = '"'
} as csv_stream
ParameterPurposeExample
valueInput CSV data source$input.csv_input, $file.content
separatorCharacter used to separate fields",", ";", "\t"
enclosureCharacter used to enclose fields'"', "'"
escape_charCharacter used to escape special characters'"', "\"
asAlias to reference the streamcsv_stream
stream.from_csv {
  value = $input.users_data
  separator = ","
  enclosure = '"'
  escape_char = '"'
} as users_stream
  • Creates a stream from CSV formatted data
  • Configurable field separator, enclosure, and escape characters
  • Assigns stream to an alias for later reference
  • Useful for processing large CSV files

/images/icons/xs_temp.svg JSONL Stream

stream.from_jsonl {
  value = $input.JSONL_file
} as jsonl_stream
ParameterPurposeExample
valueInput JSONL data source$input.JSONL_file, $file.content
asAlias to reference the streamjsonl_stream
stream.from_jsonl {
  value = $input.log_entries
} as logs_stream
  • Creates a stream from JSON Lines formatted data
  • Each line must be a valid JSON object
  • Assigns stream to an alias for later reference
  • Useful for processing large datasets line by line

/images/icons/xs_temp.svg Set HTTP Header

util.set_header {
  value = "HTTP 1.1 200 OK"
  duplicates = "replace"
}
ParameterPurposeExample
valueHTTP header value to set"HTTP 1.1 200 OK", "Content-Type: application/json"
duplicatesHow to handle duplicate headers"replace", "append"
util.set_header {
  value = "Content-Type: application/json"
  duplicates = "replace"
}
  • Sets HTTP response headers
  • Controls how duplicate headers are handled
  • Useful for customizing API responses
  • Common for setting content types, status codes, and custom headers

/images/icons/xs_temp.svg Get All Variables

util.get_vars as all_variables
ParameterPurposeExample
asNew variable to contain the returned variablesall_variables, vars
util.get_vars as system_vars

debug.log {
  value = $system_vars
}
  • Returns an object containing all variables

/images/icons/xs_temp.svg Get All Inputs

util.get_all_input as all_input
ParameterPurposeExample
asAlias to reference the input dataall_input, request_data
util.get_all_input as request_data

debug.log {
  value = request_data
}
  • Retrieves all input data from the current request as defined in the inputs section

/images/icons/xs_temp.svg Get All Raw Input

util.get_input {
  encoding = "json"
} as all_raw_input
ParameterPurposeExample
encodingFormat of the input data"json", "raw", "text"
asAlias to reference the inputall_raw_input, request_body
util.get_input {
  encoding = "json"
} as request_body
  • Retrieves raw input data from the request

/images/icons/xs_temp.svg Get Environment Variables

util.get_env as all_env_vars
ParameterPurposeExample
asAlias to reference the environment variablesall_env_vars, env
util.get_env as env
  • Returns an object containing all environment variables

/images/icons/xs_temp.svg Calculate Distance

util.geo_distance {
  latitude_1 = 37.7749
  longitude_1 = 122.4194
  latitude_2 = 35.6762
  longitude_2 = 139.6503
} as distance
ParameterPurposeExample
latitude_1Latitude of first point37.7749, -33.8688
longitude_1Longitude of first point122.4194, 151.2093
latitude_2Latitude of second point35.6762, 51.5074
longitude_2Longitude of second point139.6503, -0.1278
asAlias to reference the calculated distancedistance, route_length
util.geo_distance {
  latitude_1 = $location1|get:"lat"
  longitude_1 = $location1|get:"lng"
  latitude_2 = $location2|get:"lat"
  longitude_2 = $location2|get:"lng"
} as route_distance
  • Calculates the distance (straight line) between two geographic coordinates

/images/icons/xs_temp.svg IP Address Lookup

util.ip_lookup {
  value = "1.1.1.1"
} as ip_location
ParameterPurposeExample
valueIP address to look up"1.1.1.1", "192.168.1.1", $request.ip
asAlias to reference the lookup resultsip_location, visitor_location
util.ip_lookup {
  value = $request|get:"ip"
} as visitor_info
  • Performs geolocation lookup for an IP address

/images/icons/xs_temp.svg Set Data Source

db.set_datasource {
  value = "test"
}
ParameterPurposeExample
valueName of the datasource to use"test", "production", "staging"
db.set_datasource {
  value = "staging"
}
  • Sets the active database datasource

/images/icons/xs_temp.svg Async Await

await {
  ids = []
  timeout = 10
} as async_returns
ParameterPurposeExample
idsArray of async operation IDs to wait for[], ["task1", "task2"], $pending_tasks
timeoutMaximum time to wait in seconds10, 30, 60
asAlias to reference the resultsasync_returns, task_results
await {
  ids = ["process1", "process2"]
  timeout = 30
} as operation_results
  • Waits for completion of asynchronous operations
  • Specifies maximum wait time
  • Collects results from multiple async operations
  • Returns results when all operations complete or timeout is reached

/images/icons/xs_temp.svg Template Engine

   util.template {
      value = """
        Write a personalized email to {{ $customer.firstName }} {{ $customer.lastName }} about their recent {{ $order.type }} purchase.
        
        Include:
        - Reference to their purchase history (they've ordered {{ $customer.purchaseCount }} times)
        - Mention that their {{ $order.item }} will be delivered on {{ $order.deliveryDate|date('F j, Y') }}
        - If {{ $customer.isVIP }}, offer them a {{ $promotions.VIPDiscount }}% discount on their next purchase
        - Thank them for being a customer since {{ $customer.joinDate|date('Y') }}
        
        Sign off with the name of their account manager: {{ $accountManager.name }}
        """
    } as $x1
ParameterPurposeExample
valueThe template you’re usingSee above
asThe variable to output the template toas $x1
I