Code-Based Referral Sharing with Shopify Discount API

Samantha Brown Updated by Samantha Brown

Overview

Enable referral attribution using referral codes provided by Friendbuy and apply incentives using Shopify’s native Discounts API. This allows referred friends to enter a referral code at checkout and automatically receive a discount. Attribution will be handled via Friendbuy’s API.

Workflow Summary

  1. Advocate Receives Referral Code
  2. Code generated via Friendbuy API (postpersonalreferrallink)

    Example: manish123

    Code is stored in the merchant’s database or passed directly to the friend
  3. Friend Enters Code in Shopify Checkout

    The code is entered as a Shopify discount code
  4. Shopify Applies Discount
    • Shopify validates and applies the discount automatically
    • Shopify sends order completion webhook
  5. Friendbuy Receives Referral Code + Conversion Event

Merchant includes the referral code in the conversion tracking call to Friendbuy

  1. Friendbuy attributes the referral to the advocate

Implementation Steps

Step 1: Generate Referral Code in Friendbuy

Use Friendbuy’s existing API endpoint:

POST /v1/referral/postpersonalreferrallink

Response will include:

{

  "referral_code": "manish123",

  "advocate": {

    "email": "manish@example.com",

    ...

  }

}

Step 2: Create Matching Discount in Shopify

Using the Shopify's GraphQL API , create a discount code that matches the referral code received from Friendbuy.

Endpoint:

POST /admin/api/2024-01/price_rules.json

Sample Price Rule:

{

  "price_rule": {

    "title": "Friendbuy Referral - manish123",

    "target_type": "line_item",

    "target_selection": "all",

    "allocation_method": "across",

    "value_type": "percentage",

    "value": "-15.0",

    "customer_selection": "all",

    "starts_at": "2025-06-10T00:00:00-04:00"

  }

}

Then create a discount code for the price rule:

POST /admin/api/2024-01/price_rules/{price_rule_id}/discount_codes.json

Body:

{

  "discount_code": {

    "code": "manish123"

  }

}

Step 3: Apply Referral Code at Checkout

When the referred friend enters manish123 at checkout:

  • Shopify applies the discount automatically
  • Code-based sharing is handled natively
Step 4: Track the Conversion in Friendbuy

At purchase or signup, send a track event to Friendbuy with the referral code.

Example API call to Friendbuy:

fbq('track', 'purchase', {

  email: 'friend@example.com',

  referral_code: 'manish123',

  order_id: '123456789',

  value: 85.00

});

Important: referral_code must be included in every track event where attribution is expected.

How did we do?

Contact