Quick Start Guide

Get started with DaisyChain’s referral API in minutes. This guide will walk you through the basic setup and implementation.

1. Create an Account

  1. Sign up at DaisyChain
  2. Complete your profile setup
  3. Get your API key from the dashboard

2. Install the SDK

# Using npm
npm install @daisychain/sdk

# Using yarn
yarn add @daisychain/sdk

# Using pnpm
pnpm add @daisychain/sdk

3. Basic Implementation

Initialize the Client

import { DaisyChain } from '@daisychain/sdk';

const client = new DaisyChain({
  apiKey: 'your_api_key',
  // Optional: Enable test mode
  testMode: true
});

Track Referrals

// When a purchase is made
await client.submitReferral({
  referrerIdentifier: 'referrer@example.com',
  customerId: 'customer123',
  orderId: 'order456',
  orderAmount: 99.99
});

Add Referral Field to Forms

// Add a referral field to your checkout form
client.injectReferralField('#checkout-form', {
  label: 'Referral Code',
  placeholder: 'Enter email or code',
  position: 'top',
  validateOnInput: true
});

Handle Webhook Events

// Set up your webhook endpoint
app.post('/webhook', async (req, res) => {
  const event = req.body;
  
  switch (event.type) {
    case 'referral.created':
      // Handle new referral
      break;
    case 'referral.converted':
      // Handle successful conversion
      break;
    case 'payout.completed':
      // Handle completed payout
      break;
  }
  
  res.sendStatus(200);
});

4. Test Your Integration

  1. Enable test mode in your dashboard
  2. Create a test campaign
  3. Submit test referrals
  4. Verify webhook delivery

5. Common Integration Patterns

E-commerce Integration

// Track referral after successful checkout
checkout.on('success', async (order) => {
  await client.submitReferral({
    referrerIdentifier: order.referralCode,
    customerId: order.customerId,
    orderId: order.id,
    orderAmount: order.total,
    metadata: {
      products: order.items.map(item => item.id)
    }
  });
});

SaaS Integration

// Track referral after subscription signup
subscription.on('created', async (sub) => {
  await client.submitReferral({
    referrerIdentifier: sub.referralSource,
    customerId: sub.customerId,
    orderId: sub.id,
    orderAmount: sub.amount,
    metadata: {
      plan: sub.planId,
      interval: sub.interval
    }
  });
});

6. Next Steps

  1. Configure your referral program settings in the dashboard
  2. Set up webhook endpoints for real-time updates
  3. Implement referral tracking in your application
  4. Test thoroughly in test mode
  5. Switch to production mode

7. Best Practices

  1. Error Handling
try {
  await client.submitReferral({
    // ... referral data
  });
} catch (error) {
  if (error.code === 'INVALID_REFERRER') {
    // Handle invalid referrer
  } else if (error.code === 'DUPLICATE_ORDER') {
    // Handle duplicate order
  }
}
  1. Webhook Verification
import { verifyWebhookSignature } from '@daisychain/sdk';

app.post('/webhook', (req, res) => {
  const signature = req.headers['x-daisychain-signature'];
  const isValid = verifyWebhookSignature(
    signature,
    req.body,
    process.env.WEBHOOK_SECRET
  );
  
  if (!isValid) {
    return res.status(400).send('Invalid signature');
  }
  // Process webhook
});
  1. Rate Limiting
const client = new DaisyChain({
  apiKey: 'your_api_key',
  retryConfig: {
    maxRetries: 3,
    backoff: 'exponential'
  }
});

Need Help?