> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tuco.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Recover Shopify abandoned checkouts over iMessage

> Text shoppers who abandon checkout from your own iMessage line, using only Shopify Flow and the Tuco API. Four workflows, one goal bot, no middleware.

**You recover Shopify abandoned checkouts over iMessage by building four Shopify Flow workflows that call the Tuco API, plus one Tuco goal bot that answers replies.** The shopper gets a short text from your own line. If they reply, the bot sends their checkout link. If they order, every pending text is cancelled. No middleware is involved.

## What you need

| Need | Why |
| - | - |
| Shopify **Grow, Advanced or Plus** plan | The Flow action "Send HTTP request" is only on those plans. It is not on Basic. |
| A Tuco workspace with at least one active line, and an API key | Tuco, then **Settings**, then **API keys**. See [API keys](https://docs.tuco.ai/api-reference/api-keys.md). |
| Customer phone numbers in E.164 (`+14155550123`) | Tuco needs E.164. Shopify's docs do not state the format, so check a few customers. |
| Shopify protected-customer-data access for Flow | The checkout URL needs it. If the URL is blank in a test run, check **Settings**, then **Apps**, then **Flow** permissions. |

> [!WARNING]
>
> **Consent.** Workflow 1 only continues for customers whose Shopify SMS marketing state is `SUBSCRIBED`. Whether that is enough, and whether abandoned-cart texts count as marketing where you sell (TCPA in the US, the Spam Act in Australia, others), is your legal decision. This is not legal advice. Every message in this guide ends with "Reply STOP to opt out." and Tuco puts STOP repliers on Do Not Contact automatically. See [iMessage compliance](https://docs.tuco.ai/guides/imessage-compliance-tcpa.md).

**Message limits.** Each customer gets at most 3 messages if they never reply: an intro and 2 follow-ups. After that, a 7-day cooldown tag stops a repeat abandoner from restarting the sequence. The cooldown length is the one setting you tune: the `Wait 7 days` step in Workflow 4.

## How it fits together

```text theme={null}
W1  Customer abandons checkout -> skip if tuco-cooldown
                               -> store checkout data in 4 customer metafields
                               -> add tags tuco-abandoned-checkout + tuco-cooldown
W2  Customer tags added        -> remove the tag
                               -> intro text (no link)
                               -> tag the lead in Tuco, save cart as custom fields
                               -> follow-up 1 (+1 day, with link)
                               -> follow-up 2 (+3 days, link + optional code)
W3  Order created              -> cancel every pending text to that phone
                               -> tag shopify-customer
W4  Customer tags added        -> wait 7 days -> remove tuco-cooldown (re-arms W1)
Goal bot "Shopify · Abandoned checkout" answers replies and sends the link.
```

**Why two workflows.** The "Customer abandons checkout" trigger cannot call HTTP. It can only add tags and write metafields. So W1 parks the data on the customer, and the tag fires W2. "Customer tags added" allows HTTP, Wait, Remove tag and Update metafield.

**Why the first text has no link.** Tuco refuses a link in the first message to a new contact (`400 LINK_IN_FIRST_MESSAGE`), and iOS does not make links from unknown senders tappable until they reply. So text 1 asks a question. Links go in the follow-ups, and the bot sends the link the moment the shopper replies.

**First text.** Expect the first text roughly 15 to 20 minutes after a shopper leaves checkout. We measured this once (the Customer abandons checkout trigger fired about 16 minutes after the shopper left checkout in our test). Shopify does not document the timing.

**Timing.** Follow-ups go out 1 day and 3 days after the abandonment. Each is skipped automatically if the shopper replied (Tuco `skipIfReplied`). All pending texts are cancelled when they order. Sends happen between 09:00 and 20:00 in your shop's time zone.

## Step 1. Create the API key and the Flow secret

1. In Tuco, go to **Settings**, then **API keys**, and create a key named "Shopify Flow". Copy it once.
2. In Shopify admin, go to **Apps**, then **Flow**, then **Settings**, then **Secrets**. Add a secret with handle `tuco_api_key` and the key as the value. You need the "Manage connections and secrets" staff permission. Flow hides it in run logs.
3. Every HTTP action below uses two headers: `Authorization` = `Bearer {{secrets.tuco_api_key}}` and `Content-Type` = `application/json`.

## Pasting into Flow: rules that bite

* **No `{` or `}` inside `{{ ... }}`.** Flow's Liquid ends an output tag at the first `}`. The bodies do all brace cleaning inside `{% assign %}` tags. Keep this rule if you edit a template.
* **Metafield values are single-line text.** Paste with no trailing newline.
* **Click the editor's first line, then paste.** Clicking the panel background makes Flow try to paste steps onto the canvas.
* **Fill the Body before the URL field**, then re-check the URL. In testing the URL lost its value when the body was pasted after it.

## Step 2. Metafield definitions: not required

Skip creating definitions. The **Update customer metafield** action writes unstructured metafields: you give it Namespace, Key and Type directly. W2 reads them back through the customer's metafield list, which needs no definition.

## Step 3. Build Workflow 1, "Tuco · 1 · Abandoned checkout → mark"

1. In Flow, click **Create workflow** and name it. Trigger: **Customer abandons checkout**.
2. Add a **Condition** with these criteria, joined with **AND**:
   * `abandonment.customerHasNoOrderSinceAbandonment` is true
   * `customer.defaultPhoneNumber.smsMarketingConsent.state` equals `SUBSCRIBED` (the consent gate). Do not use `customer.phone` or `customer.smsMarketingConsent`: Flow lists them as deprecated.
   * A list criterion: **None of** `customer` / `tags`, item **Equal to** `tuco-cooldown`
3. In the "Then" branch, add four **Update customer metafield** actions. For each, use Namespace `tuco`, the Key below, Type **Single line text**, and paste the code into **Value**.
4. Last action: **Add customer tags** with `tuco-abandoned-checkout` and `tuco-cooldown` in one action.
5. Turn the workflow on.

> [accordion] Key checkout\_url
>
> ```liquid theme={null}
> {{ abandonment.abandonedCheckoutPayload.abandonedCheckoutUrl | strip }}
> ```

> [accordion] Key cart\_summary
>
> ```liquid theme={null}
> {%- assign items = abandonment.abandonedCheckoutPayload.lineItems -%}
> {%- assign n = items | size -%}
> {%- for item in items -%}
> {%- if forloop.first -%}
> {%- comment -%} cleaning happens in assign: Flow's Liquid ends an output tag at the first "}" {%- endcomment -%}
> {%- assign firstTitle = item.title | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 50, "…" -%}
> {{ firstTitle }}
> {%- endif -%}
> {%- endfor -%}
> {%- if n > 1 %} + {{ n | minus: 1 }} more{% endif -%}
> ```

> [accordion] Key cart\_items
>
> ```liquid theme={null}
> {%- capture itemsText -%}
> {%- for item in abandonment.abandonedCheckoutPayload.lineItems -%}
> {%- comment -%} cleaning happens in assign: Flow's Liquid ends an output tag at the first "}" {%- endcomment -%}
> {%- assign t = item.title | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "…" -%}
> {%- assign v = item.variantTitle | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 40, "…" -%}
> {{ item.quantity }}× {{ t }}{% if v != blank and v != "Default Title" %} ({{ v }}){% endif %}{% unless forloop.last %}, {% endunless %}
> {%- endfor -%}
> {%- endcapture -%}
> {{ itemsText | strip | truncate: 1000, "…" }}
> ```

> [accordion] Key cart\_total
>
> ```liquid theme={null}
> {{ abandonment.abandonedCheckoutPayload.subtotalPriceSet.presentmentMoney.amount }} {{ abandonment.abandonedCheckoutPayload.subtotalPriceSet.presentmentMoney.currencyCode }}
> ```

`cart_summary` uses a `for` loop and `forloop.first`, because Flow's Liquid has no list indexing.

## Step 4. Build Workflow 2, "Tuco · 2 · Text the shopper"

1. Trigger: **Customer tags added**. Condition: `tags` contains `tuco-abandoned-checkout`.
2. **Do not add customer tags anywhere in this workflow.** It would loop.
3. Action: **Remove customer tags** with `tuco-abandoned-checkout`. This re-arms W1 for the customer's next abandonment.
4. Add four **Send HTTP request** actions. The easiest way is **Import from cURL**. Make sure the secret from Step 1 exists first (**Flow**, then **Settings**, then **Secrets**, handle `tuco_api_key`), because the pasted command refers to `{{secrets.tuco_api_key}}`. For each action: **Send HTTP request**, then **Import from cURL**, paste the whole command below, then **Import**. Flow fills the method, URL, both headers and the multi-line Liquid body in one go. Then set the 4XX and 5XX dropdowns as listed and re-check the URL and body.

| # | URL | Paste | On client error (4XX) | On server error (5XX or 429) |
| - | - | - | - | - |
| 1 | `https://app.tuco.ai/api/messages` | intro | **Fail** | **Retry** |
| 2 | `https://app.tuco.ai/api/leads/tags` | tags | **Fail** | **Retry** |
| 3 | `https://app.tuco.ai/api/messages` | follow-up 1 | **Fail** | **Retry** |
| 4 | `https://app.tuco.ai/api/messages` | follow-up 2 | **Fail** | **Retry** |

A 4XX means the request is wrong (bad phone, link in a first message, bad key). Retrying the same body never fixes it, so Fail and read the run log. 5XX and 429 are temporary, so Retry. Retries are safe: every `/api/messages` body carries an `idempotencyKey` (`shopify-ac-<customer id>-<yyyymmdd>-intro`, `-fu1` or `-fu2`). Tuco keeps keys for 24 hours and a replay returns the original response.

Every W2 body starts with a `for mf in customer.metafields` loop that reads the four values W1 stored. You do not need the variable picker.

> [!TIP]
>
> If the Flow run log shows those values empty, use the variable picker instead: **Add a variable**, then **customer**, then **metafield**, then **Use an unstructured metafield**, namespace `tuco`, key `checkout_url`. Replace the matching `assign` in each body with the Liquid Flow inserts.

> [accordion] HTTP 1: intro (no link)
>
> ```bash theme={null}
> curl -X POST 'https://app.tuco.ai/api/messages' -H 'Content-Type: application/json' -H 'Authorization: Bearer {{secrets.tuco_api_key}}' -d '{%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {%- assign summary = cartSummary | replace: ".", " " | strip | default: "something" -%}
> {%- capture idemKey -%}shopify-ac-{{ customer.legacyResourceId }}-{{ "now" | date: "%Y%m%d" }}-intro{%- endcapture -%}
> {%- capture message -%}
> Hi {{ fn }}, it’s {{ shopName }}. Saw you left {{ summary }} in your cart. Any questions I can help with before you check out? Reply STOP to opt out.
> {%- endcapture -%}
> {
>   "recipientPhone": {{ phone | json }},
>   "recipientName": {{ recipientName | json }},
>   "idempotencyKey": {{ idemKey | json }},
>   "message": {{ message | strip | json }},
>   "timezone": {{ shop.ianaTimezone | json }},
>   "sendWindowStart": "09:00",
>   "sendWindowEnd": "20:00"
> }'
> ```

> [accordion] HTTP 2: tag the lead and save the cart
>
> ```bash theme={null}
> curl -X POST 'https://app.tuco.ai/api/leads/tags' -H 'Content-Type: application/json' -H 'Authorization: Bearer {{secrets.tuco_api_key}}' -d '{%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {
>   "phone": {{ phone | json }},
>   "add": "shopify-abandoned-checkout",
>   "customFields": {
>     "shopify_checkout_url": {{ checkoutUrl | strip | truncate: 1900, "" | json }},
>     "shopify_cart_items": {{ cartItems | strip | truncate: 1900, "…" | json }},
>     "shopify_cart_total": {{ cartTotal | strip | truncate: 100, "" | json }},
>     "shopify_abandoned_at": {{ "now" | date: "%Y-%m-%dT%H:%M:%S%z" | json }}
>   }
> }'
> ```

> [accordion] HTTP 3: follow-up 1 (+1 day)
>
> ```bash theme={null}
> curl -X POST 'https://app.tuco.ai/api/messages' -H 'Content-Type: application/json' -H 'Authorization: Bearer {{secrets.tuco_api_key}}' -d '{%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {%- assign summary = cartSummary | strip -%}
> {%- capture idemKey -%}shopify-ac-{{ customer.legacyResourceId }}-{{ "now" | date: "%Y%m%d" }}-fu1{%- endcapture -%}
> {%- capture message -%}
> Hi {{ fn }}, your cart{% if summary != blank %} with {{ summary }}{% endif %} is still saved at {{ shopName }}. {% if checkoutUrl != blank %}You can finish checking out here: {{ checkoutUrl | strip }} Happy to answer any questions.{% else %}Reply here and I’ll send you the link.{% endif %} Reply STOP to opt out.
> {%- endcapture -%}
> {
>   "recipientPhone": {{ phone | json }},
>   "recipientName": {{ recipientName | json }},
>   "idempotencyKey": {{ idemKey | json }},
>   "message": {{ message | strip | json }},
>   "scheduledDate": {{ "now" | date_plus: "1 day" | date: "%Y-%m-%dT%H:%M:%S%z" | json }},
>   "skipIfReplied": "2d",
>   "timezone": {{ shop.ianaTimezone | json }},
>   "sendWindowStart": "09:00",
>   "sendWindowEnd": "20:00"
> }'
> ```

> [accordion] HTTP 4: follow-up 2 (+3 days)
>
> ```bash theme={null}
> curl -X POST 'https://app.tuco.ai/api/messages' -H 'Content-Type: application/json' -H 'Authorization: Bearer {{secrets.tuco_api_key}}' -d '{%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {%- assign discountCode = "COMEBACK10" -%}
> {%- assign summary = cartSummary | strip -%}
> {%- capture idemKey -%}shopify-ac-{{ customer.legacyResourceId }}-{{ "now" | date: "%Y%m%d" }}-fu2{%- endcapture -%}
> {%- capture message -%}
> Last note from {{ shopName }}, {{ fn }}: your cart{% if summary != blank %} with {{ summary }}{% endif %} is still open. {% if discountCode != blank %}Use code {{ discountCode }} at checkout. {% endif %}{% if checkoutUrl != blank %}Finish here: {{ checkoutUrl | strip }}{% else %}Reply here and I’ll send you the link.{% endif %} Reply STOP to opt out.
> {%- endcapture -%}
> {
>   "recipientPhone": {{ phone | json }},
>   "recipientName": {{ recipientName | json }},
>   "idempotencyKey": {{ idemKey | json }},
>   "message": {{ message | strip | json }},
>   "scheduledDate": {{ "now" | date_plus: "3 days" | date: "%Y-%m-%dT%H:%M:%S%z" | json }},
>   "skipIfReplied": "4d",
>   "timezone": {{ shop.ianaTimezone | json }},
>   "sendWindowStart": "09:00",
>   "sendWindowEnd": "20:00"
> }'
> ```

**Manual fallback.** If you cannot import, add each action by hand: method **POST**, headers `Authorization: Bearer {{secrets.tuco_api_key}}` and `Content-Type: application/json`, and the matching body below. Fill the Body before the URL.

> [accordion] Fallback body: intro
>
> ```liquid theme={null}
> {%- comment -%} W2 HTTP #1  POST https://app.tuco.ai/api/messages  -- intro, NO link (Tuco refuses links in the first-ever message) {%- endcomment -%}
> {%- comment -%}
> === Reads the W1 metafields from the customer’s metafield list (no variable-picker step). Pattern: community.shopify.com/t/add-custom-metafield-value-to-automated-internal-email-when-order-is-placed/182116 ===
> {%- endcomment -%}
> {%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {%- comment -%} dots are replaced by spaces in names/titles so "Acme.co" cannot look like a domain to Tuco’s link filter {%- endcomment -%}
> {%- assign summary = cartSummary | replace: ".", " " | strip | default: "something" -%}
> {%- capture idemKey -%}shopify-ac-{{ customer.legacyResourceId }}-{{ "now" | date: "%Y%m%d" }}-intro{%- endcapture -%}
> {%- capture message -%}
> Hi {{ fn }}, it’s {{ shopName }}. Saw you left {{ summary }} in your cart. Any questions I can help with before you check out? Reply STOP to opt out.
> {%- endcapture -%}
> {
>   "recipientPhone": {{ phone | json }},
>   "recipientName": {{ recipientName | json }},
>   "idempotencyKey": {{ idemKey | json }},
>   "message": {{ message | strip | json }},
>   "timezone": {{ shop.ianaTimezone | json }},
>   "sendWindowStart": "09:00",
>   "sendWindowEnd": "20:00"
> }
> ```

> [accordion] Fallback body: tags
>
> ```liquid theme={null}
> {%- comment -%} W2 HTTP #2  POST https://app.tuco.ai/api/leads/tags  (identifies the lead by phone ONLY) {%- endcomment -%}
> {%- comment -%}
> === Reads the W1 metafields from the customer’s metafield list (no variable-picker step). Pattern: community.shopify.com/t/add-custom-metafield-value-to-automated-internal-email-when-order-is-placed/182116 ===
> {%- endcomment -%}
> {%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {
>   "phone": {{ phone | json }},
>   "add": "shopify-abandoned-checkout",
>   "customFields": {
>     "shopify_checkout_url": {{ checkoutUrl | strip | truncate: 1900, "" | json }},
>     "shopify_cart_items": {{ cartItems | strip | truncate: 1900, "…" | json }},
>     "shopify_cart_total": {{ cartTotal | strip | truncate: 100, "" | json }},
>     "shopify_abandoned_at": {{ "now" | date: "%Y-%m-%dT%H:%M:%S%z" | json }}
>   }
> }
> ```

> [accordion] Fallback body: follow-up 1
>
> ```liquid theme={null}
> {%- comment -%} W2 HTTP #3  POST https://app.tuco.ai/api/messages  -- follow-up 1 (+1 day), link allowed (not first-ever message) {%- endcomment -%}
> {%- comment -%}
> === Reads the W1 metafields from the customer’s metafield list (no variable-picker step). Pattern: community.shopify.com/t/add-custom-metafield-value-to-automated-internal-email-when-order-is-placed/182116 ===
> {%- endcomment -%}
> {%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {%- assign summary = cartSummary | strip -%}
> {%- capture idemKey -%}shopify-ac-{{ customer.legacyResourceId }}-{{ "now" | date: "%Y%m%d" }}-fu1{%- endcapture -%}
> {%- capture message -%}
> Hi {{ fn }}, your cart{% if summary != blank %} with {{ summary }}{% endif %} is still saved at {{ shopName }}. {% if checkoutUrl != blank %}You can finish checking out here: {{ checkoutUrl | strip }} Happy to answer any questions.{% else %}Reply here and I’ll send you the link.{% endif %} Reply STOP to opt out.
> {%- endcapture -%}
> {
>   "recipientPhone": {{ phone | json }},
>   "recipientName": {{ recipientName | json }},
>   "idempotencyKey": {{ idemKey | json }},
>   "message": {{ message | strip | json }},
>   "scheduledDate": {{ "now" | date_plus: "1 day" | date: "%Y-%m-%dT%H:%M:%S%z" | json }},
>   "skipIfReplied": "2d",
>   "timezone": {{ shop.ianaTimezone | json }},
>   "sendWindowStart": "09:00",
>   "sendWindowEnd": "20:00"
> }
> ```

> [accordion] Fallback body: follow-up 2
>
> ```liquid theme={null}
> {%- comment -%} W2 HTTP #4  POST https://app.tuco.ai/api/messages  -- follow-up 2 (+3 days), last message, optional discount {%- endcomment -%}
> {%- comment -%}
> === Reads the W1 metafields from the customer’s metafield list (no variable-picker step). Pattern: community.shopify.com/t/add-custom-metafield-value-to-automated-internal-email-when-order-is-placed/182116 ===
> {%- endcomment -%}
> {%- assign checkoutUrl = "" -%}{%- assign cartSummary = "" -%}{%- assign cartItems = "" -%}{%- assign cartTotal = "" -%}
> {%- for mf in customer.metafields -%}
>   {%- if mf.namespace == "tuco" -%}
>     {%- if mf.key == "checkout_url" -%}{%- assign checkoutUrl = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_summary" -%}{%- assign cartSummary = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_items" -%}{%- assign cartItems = mf.value -%}{%- endif -%}
>     {%- if mf.key == "cart_total" -%}{%- assign cartTotal = mf.value -%}{%- endif -%}
>   {%- endif -%}
> {%- endfor -%}
> {%- assign fn = customer.firstName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 30, "" | default: "there" -%}
> {%- assign shopName = shop.name | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | replace: ".", " " | strip | truncate: 40, "" | default: "our store" -%}
> {%- capture fullName -%}{{ customer.firstName }} {{ customer.lastName }}{%- endcapture -%}
> {%- assign recipientName = fullName | newline_to_br | strip_newlines | replace: "<br />", " " | remove: "{" | remove: "}" | strip | truncate: 80, "" | default: "Shopify customer" -%}
> {%- assign phone = customer.defaultPhoneNumber.phoneNumber | default: customer.phone | strip -%}
> {%- comment -%}
> === MERCHANT: discount code. Must be a REAL code you created in Shopify. Set to "" (empty) to send no discount line. ===
> {%- endcomment -%}
> {%- assign discountCode = "COMEBACK10" -%}
> {%- comment -%} === end discount block === {%- endcomment -%}
> {%- assign summary = cartSummary | strip -%}
> {%- capture idemKey -%}shopify-ac-{{ customer.legacyResourceId }}-{{ "now" | date: "%Y%m%d" }}-fu2{%- endcapture -%}
> {%- capture message -%}
> Last note from {{ shopName }}, {{ fn }}: your cart{% if summary != blank %} with {{ summary }}{% endif %} is still open. {% if discountCode != blank %}Use code {{ discountCode }} at checkout. {% endif %}{% if checkoutUrl != blank %}Finish here: {{ checkoutUrl | strip }}{% else %}Reply here and I’ll send you the link.{% endif %} Reply STOP to opt out.
> {%- endcapture -%}
> {
>   "recipientPhone": {{ phone | json }},
>   "recipientName": {{ recipientName | json }},
>   "idempotencyKey": {{ idemKey | json }},
>   "message": {{ message | strip | json }},
>   "scheduledDate": {{ "now" | date_plus: "3 days" | date: "%Y-%m-%dT%H:%M:%S%z" | json }},
>   "skipIfReplied": "4d",
>   "timezone": {{ shop.ianaTimezone | json }},
>   "sendWindowStart": "09:00",
>   "sendWindowEnd": "20:00"
> }
> ```

The follow-up bodies compute `scheduledDate` when the workflow runs (`"now" | date_plus: "1 day"`, and 3 days for follow-up 2). `skipIfReplied` of `"2d"` and `"4d"` makes Tuco cancel the queued send if the shopper replied in that window. In follow-up 2, edit the marked `discountCode` line: it must be a real Shopify discount code, or set it to `""` to drop the sentence.

The `/api/leads/tags` call uses `customFields`, which merges the cart data into the lead (matched by phone). See [Add or remove lead tags](https://docs.tuco.ai/api-reference/endpoint/lead-tags.md). The goal bot reads these fields.

## Step 5. Build Workflow 3, "Tuco · 3 · Stop when they buy"

1. Trigger: **Order created**. Condition: `order.customer.defaultPhoneNumber.phoneNumber` is not empty.
2. **Send HTTP request**, then **Import from cURL**, with the first command below (POST `https://app.tuco.ai/api/messages/cancel-for-lead`). 4XX **Ignore**, 5XX **Retry**. Cancelling is idempotent: Tuco returns 200 even with nothing pending. See [cancel for lead](https://docs.tuco.ai/api-reference/endpoint/cancel-for-lead.md).
3. **Send HTTP request**, then **Import from cURL**, with the second command below (POST `https://app.tuco.ai/api/leads/tags`). 4XX **Ignore** (most buyers were never texted, so `404 lead_not_found` is normal), 5XX **Retry**.

> [accordion] Cancel command
>
> ```bash theme={null}
> curl -X POST 'https://app.tuco.ai/api/messages/cancel-for-lead' -H 'Content-Type: application/json' -H 'Authorization: Bearer {{secrets.tuco_api_key}}' -d '{%- assign phone = order.customer.defaultPhoneNumber.phoneNumber | default: order.customer.phone | default: order.phone | strip -%}
> {
>   "recipientPhone": {{ phone | json }}
> }'
> ```

> [accordion] Tag command
>
> ```bash theme={null}
> curl -X POST 'https://app.tuco.ai/api/leads/tags' -H 'Content-Type: application/json' -H 'Authorization: Bearer {{secrets.tuco_api_key}}' -d '{%- assign phone = order.customer.defaultPhoneNumber.phoneNumber | default: order.customer.phone | default: order.phone | strip -%}
> {
>   "phone": {{ phone | json }},
>   "remove": "shopify-abandoned-checkout",
>   "add": "shopify-customer"
> }'
> ```

> [accordion] Manual fallback body: cancel
>
> ```liquid theme={null}
> {%- comment -%} W3 HTTP #1  POST https://app.tuco.ai/api/messages/cancel-for-lead  (trigger: Order created) {%- endcomment -%}
> {%- assign phone = order.customer.defaultPhoneNumber.phoneNumber | default: order.customer.phone | default: order.phone | strip -%}
> {
>   "recipientPhone": {{ phone | json }}
> }
> ```

> [accordion] Manual fallback body: tags
>
> ```liquid theme={null}
> {%- comment -%} W3 HTTP #2  POST https://app.tuco.ai/api/leads/tags  (404 lead_not_found is normal: set 4XX = Ignore) {%- endcomment -%}
> {%- assign phone = order.customer.defaultPhoneNumber.phoneNumber | default: order.customer.phone | default: order.phone | strip -%}
> {
>   "phone": {{ phone | json }},
>   "remove": "shopify-abandoned-checkout",
>   "add": "shopify-customer"
> }
> ```

Buying does not reset the cooldown on purpose. W3 never removes `tuco-cooldown`.

## Step 6. Build Workflow 4, "Tuco · 4 · Cooldown release"

1. Trigger: **Customer tags added**. Condition: `tags` contains `tuco-cooldown`.
2. Action: **Wait** 7 days. This is the cooldown length.
3. Action: **Remove customer tags** with `tuco-cooldown`. Do not add tags here.

## Step 7. Create the goal bot

The bot answers when a lead that carries the tag `shopify-abandoned-checkout` replies. It reads the stored cart and checkout link from the lead's custom fields.

Create it paused, so it cannot answer anyone until you have tested it. See [Create goal bot](https://docs.tuco.ai/api-reference/endpoint/create-goal-bot.md).

```bash theme={null}
export TUCO_API_KEY='tuco_xxxxxxxxxxxxx'   # never commit it

curl -X POST "https://app.tuco.ai/api/goal-bots" \
  -H "Authorization: Bearer $TUCO_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @abandoned-checkout.json
```

`abandoned-checkout.json`:

```json theme={null}
{
  "name": "Shopify · Abandoned checkout",
  "status": "paused",
  "mode": "autopilot",
  "maxTurns": 8,
  "includeTags": ["shopify-abandoned-checkout"],
  "excludeTags": ["shopify-customer"],
  "collectFields": ["purchase_blocker"],
  "kbRetrieval": "always",
  "prompt": "You are the texting assistant for an online store. This person left items in their cart and has just replied to us.\n\nFIRST: call get_lead_details. Use firstName, customFields.shopify_cart_items and customFields.shopify_cart_total to know what they left behind.\n\nRULES\n- Reply in at most 2 short sentences. Friendly, plain, no pressure, no hype, no emoji spam.\n- If they want to buy, finish, or ask for the link: send customFields.shopify_checkout_url exactly as stored. If that field is missing, say a teammate will send it shortly.\n- Answer product, shipping and returns questions ONLY from the knowledge base. If the knowledge base does not say, say you are not sure and that a teammate will follow up. Never invent policies, prices, stock, delivery dates or discounts.\n- Discounts: you may only offer a discount if one is written here: [MERCHANT: write one code and its terms here, or leave this line as 'No discount authorised'.]. Otherwise never mention discounts.\n- If they say why they have not bought (price, shipping cost, sizing, unsure, just browsing), save it in purchase_blocker in a few words, then help with that one thing.\n- Order problems, refunds, damaged items, cancellations, anything account-specific: say \"a teammate will follow up\" and stop.\n- If they say STOP, unsubscribe, remove me, or ask to stop in any way: send nothing at all.\n- Never claim to be human. If asked, say you are the store's AI assistant."
}
```

1. Replace the `[MERCHANT: ...]` text in the prompt with one discount code and its terms, or write `No discount authorised`.
2. Load your shipping, returns and FAQ pages into the Tuco knowledge base. The bot answers product and policy questions only from it. If the knowledge base does not say, the bot says it is not sure and that a teammate will follow up.
3. Run a [simulation](https://docs.tuco.ai/api-reference/endpoint/simulate-goal-bot.md). Nothing is sent.
4. Set the bot to active in **Goal bots**, or with `PATCH /api/goal-bots/{id}` and `{ "status": "active" }` ([update](https://docs.tuco.ai/api-reference/endpoint/update-goal-bot.md)).

In a live test, a shopper replied and the bot answered: "Here's the link to finish your checkout: [https://shop.example/recover?key=xyz](https://shop.example/recover?key=xyz). I'm not sure about shipping to Austin, but a teammate can follow up with you on that." The link was a placeholder here. The bot sends the real stored link.

## Step 8. Test with your own phone

1. Add a customer for yourself in Shopify with your E.164 phone and SMS marketing set to subscribed.
2. Start a checkout as that customer (email and phone) and leave. Wait for Shopify to fire "Customer abandons checkout". The Flow run log shows when. To skip the wait, set the four metafields by hand and add the tag `tuco-abandoned-checkout` to your customer.
3. In Flow, open the W2 run log. All four HTTP steps should be green. You get text 1 (no link) inside the send window. Reply "yes". The bot sends your link.
4. In Tuco, check that the lead has the tag `shopify-abandoned-checkout` and the custom fields `shopify_checkout_url`, `shopify_cart_items`, `shopify_cart_total` and `shopify_abandoned_at`. Two scheduled messages should be pending.
5. Complete the order. W3 runs, the pending messages are cancelled, and the tag becomes `shopify-customer`.

When this works, turn on W1 to W4 for everyone and watch the first day's run logs and replies.

## Troubleshooting

| Flow run log shows | Meaning and fix |
| - | - |
| 400 `LINK_IN_FIRST_MESSAGE` | The first message to a new contact had a link or domain-like text. The intro strips dots from shop and product names. Check the rendered message and remove any URL you added. |
| 400 `MALFORMED_NAME_TEMPLATE` | A literal `{{firstName}}`-style token reached `message`. A Liquid variable did not resolve. Check the metafield lines in Step 4. |
| 401 | Wrong or missing key. The secret handle must be exactly `tuco_api_key` and the header `Bearer {{secrets.tuco_api_key}}`. |
| 404 `lead_not_found` | `/api/leads/tags` for a phone Tuco has never contacted. Normal in W3 (Ignore). In W2 it means the intro step failed. |
| 400 phone invalid | The phone is not E.164. Fix the customer's phone or add a "starts with +" condition. |
| 429 | Rate limited. The Retry setting handles it. |
| Empty summary or URL in a body | The metafield path is wrong or W1 never ran. |
| Nothing happens after abandonment | A W1 condition failed: not SUBSCRIBED, phone empty, customer already ordered, or `tuco-cooldown` still on the customer. Open W1's run log. |

## Verified in a real store

Tested on 2026-12-04 in a Shopify dev store on a Grow plan.

* W2 activated and fired. Shopify sent valid JSON: `recipientPhone` `+13054952897` (E.164), a name with quotes escaped correctly, an `idempotencyKey` such as `shopify-ac-31632508289058-20261204-intro`, and `scheduledDate` from `"now" | date_plus: "1 day" | date: "%Y-%m-%dT%H:%M:%S%z"`, for example `2026-12-05T14:49:18+0000`. The `timezone` came from `shop.ianaTimezone`. The `for mf in customer.metafields` loop was accepted by the editor and by activation.
* W1 activated. `abandonment.abandonedCheckoutPayload.abandonedCheckoutUrl`, `lineItems` (`title`, `quantity`, `variantTitle`), `subtotalPriceSet.presentmentMoney.amount` and `.currencyCode`, and `abandonment.customerHasNoOrderSinceAbandonment` were all accepted.
* **Update customer metafield**, then the Metafield dropdown, then **Use an unstructured metafield**, then Namespace, Key, Type **Single line text** and Value. No definition is needed.
* The Customer abandons checkout trigger fired about 16 minutes after the shopper left checkout in this test. Shopify documents no timing. W1 stored the real recover URL and the items, and the W1 to W2 chain worked end to end.
* Found and fixed along the way: braces inside `{ }` break Flow's tokenizer, metafield values must not end in a newline, `customer.phone` and `customer.smsMarketingConsent` are deprecated, and an empty cart used to render "your your cart" with a dangling link colon. The follow-ups now handle empty metafields.

## Limits and caveats

* **Shopify plan.** You need Grow, Advanced or Plus for the HTTP action.
* **Volume.** Each Tuco line can start conversations with 50 first-time contacts per day. Add lines for more volume. See [pricing](https://docs.tuco.ai/pricing.md).
* **SMS fallback.** If a number is not on iMessage, Tuco can fall back to SMS. SMS fallback only reaches numbers in the line's country, so a US line reaches US numbers. See [failures and fallbacks](https://docs.tuco.ai/features/failures-and-fallbacks.md).
* **You write the AI rules.** The goal bot prompt is yours to edit and test. Tools that write the rules for you save time, but you give up control over what is said.
* **Consent is yours.** Tuco does not decide whether you may text a customer. See [iMessage compliance](https://docs.tuco.ai/guides/imessage-compliance-tcpa.md).

> [card] [Goal bots](https://docs.tuco.ai/features/goal-bots.md)
>
> How goal bots read lead fields, use the knowledge base and hand off to a person.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.