Shopify Loyalty Reward Automation with n8n, Klaviyo, and PostgreSQL

Hi there!
I recently built a Shopify loyalty reward workflow using n8n, and wanted to share how I approached it.

The idea is simple: when a customer places a paid order over $500, they get a personalized 10% discount for their next purchase.

Here’s how I structured the workflow:

  1. Shopify Trigger detects a paid order.

  2. Order validation checks the payment status, order amount, and customer details.

  3. Shopify GraphQL API checks whether a discount already exists for that order. If not, it creates a customer-specific, single-use 10% discount valid for 30 days.

  4. PostgreSQL stores the discount details, processing status, and Klaviyo event payload.

  5. Klaviyo API receives a custom event containing the discount code and expiration date.

  6. Klaviyo Flow uses these event properties to personalize the reward email.

One of the things I spent more time on was handling repeated executions.

For example, if the same Shopify order triggers the workflow again, I don’t want to create another discount or send the same event twice.

To handle this, I added an existing-discount check in Shopify, PostgreSQL state tracking, and a unique event ID for Klaviyo.

I also connected the workflow to a separate centralized error handler in n8n. It logs incidents in Google Sheets, checks for duplicate notifications, and sends Slack alerts when something fails.

I tested the full process in a Shopify development store, including creating the discount, sending the event to Klaviyo, and receiving the personalized email.

The MVP is working, although automated recovery and production deployment would need some additional work.

I’m curious how others handle similar workflows, especially when an API operation succeeds but the next step fails.

Do you usually rely on n8n retries and error workflows, or use a database to track processing state and handle recovery?

I’d be happy to share more details about the workflow if anyone is interested!