function

Functions allow you to create short, reusable scripts that can be called from any of your other scripts. They are useful for encapsulating logic you use in multiple places - API calls, data transformations, calculations, and more.

To create a function, select the Add new function script button.

Syntax

{% function "function_handle", param1:value1, param2:value2 as result %}
  • "function_handle" — the handle (or ID) of the function script to call.

  • param1:value1, param2:value2 — named parameters. Each becomes a variable inside the function.

  • as result — captures the function's return value into a variable.

Parameters

Named parameters let you pass values directly into the function scope. Each key:value pair becomes a variable accessible inside the function code.

Values can be variables, string literals, or any Liquid expression:

{% assign user_email = "[email protected]" %}

{% function "send_email", email:user_email, subject:"Welcome!" as email_result %}

Inside the function, email and subject are available as regular variables.

You can pass as many parameters as needed:

{% comment %}Inside webhook orders/paid script{% endcomment %}
{% assign order = payload %}
{% function "create_order_note", order_id:order.id, message:"Note text", priority:"high", notify:true as note_result %}
{% log note_result %}

Return values

Functions return a value using the return tag. The returned value is captured by the as clause.

Function code (handle: add_numbers):

Calling the function:

Output: The sum is: 8

The as sum_result clause saves the return value so you can use it later in your script — log it, pass it to another function, or use it in conditions:

Without as

The as variable clause is optional. If omitted, the function's return value is written directly to the output:

This also outputs 8, but you cannot reference the result later in your script.

Example: Send metrics to Klaviyo

Function code (handle: send_metric_to_klaviyo):

Notice that email, property, and metric are used directly as variables — they are injected into the function scope when the function is called.

Calling the function from another script:

After execution, fn_result contains the result of the HTTP call made inside send_metric_to_klaviyo.

Example: Reusable GraphQL helper

Function code (handle: get_product_title):

Calling the function:

Example: Calling a function inside a loop

Functions are especially useful inside loops where you need to perform the same operation for each item:

Legacy format: JSON payload

The old format using a single JSON payload variable is still supported. All keys from the payload object are spread into the function scope.

Note: Named parameters and the JSON payload format cannot be mixed in a single call. Use one or the other.

Limitations

  • No nested function calls — you cannot call a function from inside another function.

  • Task variables are not available inside functions. Pass any values you need as parameters.

  • Global variables are accessible inside functions.

Last updated