Examples
How to implement Webhooks for your GrowSurf campaign.

Example 1: Webhooks

Below is a Node.js + Express example of what the code for your webhook endpoint could look like:
1
//Your webhooks payload endpoint
2
app.post("/your/webhook/payload-url", function(req, res) {
3
const body = req.body;
4
5
try {
6
if (body.event === 'PARTICIPANT_REACHED_A_GOAL') {
7
// Write code here to do something when a participant wins a reward
8
console.log(`${body.data.participant.email} just won this reward: ${body.data.reward.description}`);
9
​
10
} else if (body.event === 'NEW_PARTICIPANT_ADDED') {
11
// Write code here to do something when a new participant is added
12
console.log(`${body.data.email} just joined via source: ${body.data.referralSource}.`);
13
14
} else if (body.event === 'CAMPAIGN_ENDED') {
15
// Write code here to do something when a campaign ends
16
console.log(`${body.data.name} just ended with ${body.data.referralCount} total referrals!`);
17
18
}
19
20
} catch (err) {
21
res.status(400).end();
22
}
23
res.json({received: true});
24
});
Copied!
To see the different sample data from webhook request payloads, see Events​

Example 2: Webhooks (with secret)

Below is a Node.js + Express example (using the crypto-js library) of what the code for your webhook endpoint could look like:
1
//Your webhooks payload endpoint
2
app.post("/your/webhook/payload-url", function(req, res) {
3
const body = req.body;
4
const signature = req.get("GrowSurf-Signature");
5
6
try {
7
// Validate the signature
8
validateSignature(body, signature);
9
10
// Do work!.....
11
// Write your code in here...
12
13
} catch (err) {
14
res.status(400).end();
15
}
16
res.json({received: true});
17
});
18
​
19
/**
20
* Compares the GrowSurf header provided signature against the expected
21
* signature.
22
*
23
* @param {Object} body the request body provided by GrowSurf
24
* @param {String} signature the signature hash value provided within the header of the request
25
* @returns {Boolean} valid true if the expected matches the given
26
* @throws {Exception} thrown if the expected signature value does not match the given
27
*/
28
const validateSignature = function(body, signature) {
29
// Extract
30
let parts = signature.split(",");
31
// t value
32
let timestamp = parts[0].split("=")[1];
33
// v value
34
let hash = parts[1].split("=")[1];
35
// Generate hash
36
let message = (timestamp + "." + JSON.stringify(body));
37
let expected = CryptoJS.HmacSHA256(message, "YOUR-SECRET-TOKEN").toString();
38
​
39
// Validate/Compare
40
if(expected === hash) {
41
return true;
42
} else {
43
throw new Error("Invalid Signature");
44
}
45
}
Copied!
Last modified 2yr ago