> For the complete documentation index, see [llms.txt](https://docs.datajet-app.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datajet-app.com/liquid/tags/return.md).

# return

Returns a value from a function or sends a response from HTTP, shipping-rate and function scripts. Stops script execution immediately after returning.

```liquid
{% json response %}
  {
    "success": true,
    "message": "Operation completed"
  }
{% endjson %}

{% return response %}
```

#### Syntax

```liquid
{% return value %}
```

#### Parameters

| Parameter | Required | Description                                                  |
| --------- | -------- | ------------------------------------------------------------ |
| `value`   | Yes      | The value to return (variable, object, string, number, etc.) |

#### Behavior

* Immediately stops script execution
* Sets the return value accessible to the caller
* In functions: the returned value is output where the function was called
* In HTTP scripts: the returned value becomes the response body
* In shipping-rate scripts: the returned value defines the shipping rates
* Only one value can be returned

#### Examples

**Return from a function:**

```liquid
{% comment %} Function: calculate_discount {% endcomment %}
{% assign discount = total | times: 0.1 %}
{% return discount %}
```

```liquid
{% comment %} Calling the function {% endcomment %}
{% capture result %}{% function "calculate_discount", total: 100 %}{% endcapture %}
{% log "Discount: " | append: result %}
{% comment %} Output: Discount: 10 {% endcomment %}
```

**Return an object from a function:**

```liquid
{% comment %} Function: validate_order {% endcomment %}
{% if order.total > 0 and order.email != blank %}
  {% json result %}
    {
      "valid": true,
      "errors": []
    }
  {% endjson %}
{% else %}
  {% json result %}
    {
      "valid": false,
      "errors": ["Invalid order data"]
    }
  {% endjson %}
{% endif %}

{% return result %}
```

**Return simple values:**

```liquid
{% comment %} Return a string {% endcomment %}
{% return "success" %}

{% comment %} Return a number {% endcomment %}
{% assign total = items | size %}
{% return total %}

{% comment %} Return a boolean {% endcomment %}
{% if customer.vip %}
  {% return true %}
{% else %}
  {% return false %}
{% endif %}
```

**HTTP script response:**

```liquid
{% comment %} Return JSON response for HTTP endpoint {% endcomment %}
{% json response %}
  {
    "status": "ok",
    "data": {
      "order_id": {{ order.id }},
      "processed": true,
      "timestamp": "{{ 'now' | date: '%Y-%m-%dT%H:%M:%SZ' }}"
    }
  }
{% endjson %}

{% return response %}
```

**Shipping rates response:**

```liquid
{% json rates %}
  {
    "rates": [
      {% if cart.total_weight < 1000 %}
      {
        "service_name": "Standard Shipping",
        "service_code": "standard",
        "total_price": 500,
        "currency": "{{ shop.currency }}",
        "description": "5-7 business days"
      },
      {% endif %}
      {
        "service_name": "Express Shipping",
        "service_code": "express",
        "total_price": 1500,
        "currency": "{{ shop.currency }}",
        "description": "1-2 business days"
      }
    ]
  }
{% endjson %}

{% return rates %}
```

**Early return with validation:**

```liquid
{% comment %} Function: process_order {% endcomment %}

{% comment %} Validate input {% endcomment %}
{% if order == blank %}
  {% json error %}
    {"error": "Order is required"}
  {% endjson %}
  {% return error %}
{% endif %}

{% if order.line_items.size == 0 %}
  {% json error %}
    {"error": "Order has no items"}
  {% endjson %}
  {% return error %}
{% endif %}

{% comment %} Process the order... {% endcomment %}
{% assign processed = true %}

{% json success %}
  {
    "success": true,
    "order_id": {{ order.id }}
  }
{% endjson %}

{% return success %}
```

**Conditional return based on logic:**

```liquid
{% comment %} Function: get_customer_tier {% endcomment %}

{% if customer.total_spent > 10000 %}
  {% return "platinum" %}
{% elsif customer.total_spent > 5000 %}
  {% return "gold" %}
{% elsif customer.total_spent > 1000 %}
  {% return "silver" %}
{% else %}
  {% return "bronze" %}
{% endif %}
```

**Return array data:**

```liquid
{% comment %} Function: get_product_skus {% endcomment %}
{% json skus %}[]{% endjson %}

{% for item in order.line_items %}
  {% push skus, item.sku %}
{% endfor %}

{% return skus %}
```

**Return with computed values:**

```liquid
{% comment %} Function: calculate_order_summary {% endcomment %}
{% assign subtotal = 0 %}
{% assign item_count = 0 %}

{% for item in order.line_items %}
  {% assign line_total = item.price | times: item.quantity %}
  {% assign subtotal = subtotal | plus: line_total %}
  {% assign item_count = item_count | plus: item.quantity %}
{% endfor %}

{% assign tax = subtotal | times: 0.08 %}
{% assign total = subtotal | plus: tax %}

{% json summary %}
  {
    "subtotal": {{ subtotal }},
    "tax": {{ tax }},
    "total": {{ total }},
    "item_count": {{ item_count }}
  }
{% endjson %}

{% return summary %}
```

#### Use Cases

| Context               | Purpose                                      |
| --------------------- | -------------------------------------------- |
| Functions             | Return computed values to the calling script |
| HTTP Scripts          | Send response body to the HTTP caller        |
| Shipping Rate Scripts | Define available shipping rates and prices   |

#### Notes

* Only one argument is allowed - you cannot return multiple values directly
* To return multiple values, wrap them in an object using the `json` tag
* Script execution stops immediately after `return` - code after it won't run
* If no return is called in a function, the function returns `null`
* For complex responses, always use `json` tag to structure the data
* The returned value from functions is a string - use `| parse_json` if you need to access object properties in the caller
