# flow

Triggers a Shopify Flow workflow with a custom payload. Use this tag to integrate DataJet scripts with Shopify Flow automations. The flow needs to be created in Shopify Flow and the trigger name needs to be: Event Payload Trigger V2.

Read more about DataJet and Shopify Flow [here](https://docs.datajet-app.com/integrations/shopify-flow/v2).

```liquid
{% json my_payload %}
  {
    "customer_id": "{{ customer.id }}",
    "order_total": {{ order.total_price }},
    "action": "send_reward"
  }
{% endjson %}

{% flow payload: my_payload as result %}

{% if result.ok %}
  {% log "Flow triggered successfully" %}
{% else %}
  {% log "Failed to trigger flow" %}
{% endif %}
```

#### Syntax

```liquid
{% flow payload: payload_variable as result_variable %}
```

#### Parameters

| Parameter | Required | Description                                |
| --------- | -------- | ------------------------------------------ |
| `payload` | Yes      | Object containing data to send to the Flow |

#### Result Object

The result variable contains:

| Property | Type    | Description                               |
| -------- | ------- | ----------------------------------------- |
| `ok`     | boolean | `true` if Flow was triggered successfully |
| `body`   | object  | Response body from Flow trigger (if any)  |

#### Flow Receives

When triggered, the Shopify Flow receives:

| Field           | Description                        |
| --------------- | ---------------------------------- |
| `Script ID`     | The ID of the DataJet script       |
| `Script Handle` | The handle of the DataJet script   |
| `Run ID`        | The current execution run ID       |
| `Payload`       | Your custom payload as JSON string |

#### Use Cases

**Trigger post-order processing:**

```liquid
{% json order_payload %}
  {
    "order_id": "{{ order.id }}",
    "customer_email": "{{ order.email }}",
    "line_items": {{ order.line_items | map: "sku" | json }}
  }
{% endjson %}

{% flow payload: order_payload as result %}
```

**Send customer data to Flow for segmentation:**

```liquid
{% json customer_payload %}
  {
    "customer_id": "{{ customer.id }}",
    "total_spent": {{ customer.total_spent }},
    "orders_count": {{ customer.orders_count }},
    "tags": {{ customer.tags | json }}
  }
{% endjson %}

{% flow payload: customer_payload as result %}

{% unless result.ok %}
  {% log "Warning: Could not trigger customer segmentation flow" %}
{% endunless %}
```

**Chain DataJet script with Flow actions:**

```liquid
{% comment %} Process inventory data {% endcomment %}
{% assign low_stock_products = products | where_exp: "p", "p.inventory_quantity < 10" %}

{% if low_stock_products.size > 0 %}
  {% json alert_payload %}
    {
      "alert_type": "low_stock",
      "product_count": {{ low_stock_products.size }},
      "products": {{ low_stock_products | map: "title" | json }}
    }
  {% endjson %}

  {% flow payload: alert_payload as result %}
  {% log "Low stock alert sent to Flow" %}
{% endif %}
```

#### Notes

* Requires Shopify Flow to be installed and configured
* Each flow trigger consumes 1 credit
* Payload is automatically serialized to JSON
* Use Shopify Flow to handle notifications, tagging, or other actions based on the payload
* The Flow trigger uses DataJet's custom trigger "Event Payload Trigger V2"
