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
- Sign up at DaisyChain
- Complete your profile setup
- 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
- Enable test mode in your dashboard
- Create a test campaign
- Submit test referrals
- 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
- Configure your referral program settings in the dashboard
- Set up webhook endpoints for real-time updates
- Implement referral tracking in your application
- Test thoroughly in test mode
- Switch to production mode
7. Best Practices
- 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
}
}
- 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
});
- Rate Limiting
const client = new DaisyChain({
apiKey: 'your_api_key',
retryConfig: {
maxRetries: 3,
backoff: 'exponential'
}
});
Need Help?
- Check our API Reference
- Join our Discord Community
- Email support at support@daisychain.la