LoyaltySurf Docs
Help CenterSystem StatusContact SupportYour Dashboard →
  • Welcome
  • Tutorials
  • Developer Tools
    • REST API
      • Tutorials
      • Objects
      • API Reference
      • API Guidelines
      • API Response Codes
    • Webhooks
      • Securing Your Webhooks (optional)
      • Examples
      • Events Reference
    • Metadata
  • Integrations
    • Chargebee
    • HubSpot
    • PayPal
    • Recurly
    • Slack
    • Stripe
    • Tango Card
    • Zapier
      • Tutorials
      • Troubleshooting
Powered by GitBook
On this page
  • Example 1: Webhooks
  • Example 2: Webhooks (with secret)

Was this helpful?

  1. Developer Tools
  2. Webhooks

Examples

How to implement Webhooks for your LoyaltySurf campaign.

Example 1: Webhooks

Below is a Node.js + Express example of what the code for your webhook endpoint could look like:

//Your webhooks payload endpoint
app.post("/your/webhook/payload-url", function(req, res) {
  const body = req.body;
  
  try {
    if (body.event === 'PARTICIPANT_SUBMITTED_REWARD_FORM') {
      // Write code here to do something when a participant submits a reward form
      console.log(`${body.data.participant.email} just submitted a reward form: ${body.data.reward.description} - ${body.data.reward.cta}`);
      
    } else if (body.event === 'PARTICIPANT_REACHED_A_GOAL') {
      // Write code here to do something when a participant wins a reward
      console.log(`${body.data.participant.email} just won this reward: ${body.data.reward.description}`);

      // If the reward is approved
      if (body.data && body.data.reward && body.data.reward.approved) {
        // Do something
        
        // Optional: If you set metadata on the CampaignReward object, you can reference it:
        if (body.data.reward.metadata && body.data.reward.metadata["proRewardValue"]) {
            console.log(`${body.data.participant.email} earned an amount of ${body.data.reward.metadata["proRewardValue"]}.`);
        }        
      }
    } else if (body.event === 'NEW_PARTICIPANT_ADDED') {
      // Write code here to do something when a new participant is added    
      console.log(`${body.data.email} just joined via source: ${body.data.loyaltyActionSource}.`)
            
    } else if (body.event === 'CAMPAIGN_ENDED') {
      // Write code here to do something when a campaign ends
      console.log(`${body.data.name} just ended with ${body.data.loyaltyActionCount} total loyalty actions!`);
            
    }
  
  } catch (err) {
    res.status(400).end();
  }
  res.json({received: true});
});

Helpful tips:

Example 2: Webhooks (with secret)

//Your webhooks payload endpoint
app.post("/your/webhook/payload-url", function(req, res) {
  const body = req.body;
  const signature = req.get("LoyaltySurf-Signature");
  
  try {
    // Validate the signature
    validateSignature(body, signature);
  
    // Do work!.....
    // Write your code in here...  
  
  } catch (err) {
    res.status(400).end();
  }
  res.json({received: true});
});

/**
 * Compares the LoyaltySurf header provided signature against the expected
 * signature.
 *
 * @param {Object} body the request body provided by Surf
 * @param {String} signature the signature hash value provided within the header of the request
 * @returns {Boolean} valid true if the expected matches the given
 * @throws {Exception} thrown if the expected signature value does not match the given
 */
const validateSignature = function(body, signature) {
  // Extract
  let parts = signature.split(",");
  // t value
  let timestamp = parts[0].split("=")[1];
  // v value
  let hash = parts[1].split("=")[1];
  // Generate hash
  let message = (timestamp + "." + JSON.stringify(body));
  let expected = CryptoJS.HmacSHA256(message, "YOUR-SECRET-TOKEN").toString();

  // Validate/Compare
  if(expected === hash) {
    return true;
  } else {
    throw new Error("Invalid Signature");
  }
}
PreviousSecuring Your Webhooks (optional)NextEvents Reference

Last updated 4 months ago

Was this helpful?

To see the different sample data from webhook request payloads, see

If you use in your New Participant Reward events, your marketing team can update these values anytime in the future from the Campaign Editor without getting developers involved.

Below is a Node.js + Express example (using the library) of what the code for your webhook endpoint could look like:

Events
reward metadata
crypto-js