Integrate Flock referral and rewards platform into your Node server with the official Node.js SDK. This guide covers setup, configuration, and usage for server-side applications.
The Node.js SDK enables seamless integration of Flock referral and rewards into your backend services. Effortlessly build powerful referral programs, track customer engagement, and manage rewards programmatically.
Identify and manage customers
Ingest checkpoints to trigger rewards at key milestones
import { FlockSDK } from '@wflock/node-flock';// Initialize with your service access keyconst sdk = new FlockSDK({ accessKey: 'your-service-access-key', // Get this from the Flock dashboard});
Your service access key should be kept secure and never exposed in client-side code. This SDK is designed for server-side use only.
Trigger checkpoints to record key milestones in your user journey and/or trigger rewards. See Checkpoints for more details.
Copy
// Ingest a checkpointawait sdk.checkpoints.ingest('purchase_completed', { externalUserId: 'user_123', // The user ID in your system environment: 'production', // or 'test'});
// Identify a customerconst customer = await sdk.customers.identify({ externalUserId: 'external_user_123', // The user ID in your system email: 'user@example.com', name: 'Jane Doe', customProperties: { // Optional custom properties plan: 'premium', signupDate: '2025-01-15' }});
Checkpoints are the recommended way to trigger rewards. Configure which checkpoints trigger rewards in the Flock dashboard.
// Get live campaign for an environmentconst campaign = await sdk.campaigns.getLive({ environment: 'production', // or 'test' customerId: 'customer_123' // This is Flock internal ID that you get after calling the identify method});
Sometimes, you may want to validate and create a referral from your backend.
Copy
// Validate a referral codeconst validation = await sdk.referrals.validate('JANE123');if (validation.isValid) { console.log('Valid referral code from:', validation.referredBy); // Create a referral const referral = await sdk.referrals.create({ campaignId: 'campaign_123', referralCode: 'JANE123', refereeId: 'customer_123' // This is Flock internal ID that you get after calling the identify method });}
Direct reward triggering should only be used when checkpoint-based rewards don’t meet your needs. For most use cases, ingesting checkpoints is the recommended approach.
Trigger rewards programmatically:
Copy
// Trigger a rewardawait sdk.rewards.trigger({ externalUserId: 'user_123', environment: 'production', // or 'test' rewardFor: ['self', 'referral_partner'] // Who should receive the reward});