How do I code a custom webhook in brainCloud?



  • Hi,

    As the subject implies - I am integrating my app with an external service that can call my app via webhooks.

    How would I hook this up using brainCloud?

    Ima.


  • brainCloud

    Hi,

    Here are the steps to setting up a webhook to call a custom cloud code script in brainCloud:

    Step 1 - Write your script

    • Go to Design | Cloud Code | Scripts and create your script
    • You might want to just start with a simple script that logs the incoming parameters (see sample below)
    • Be sure to enable the S2S callable field on the Details page.

    Step 2 - Declare the webhook and link it to your script

    • Go to Design | Cloud Code | WebHooks and create your webhook
    • Link it to the script that you just wrote. If you don't see your script in the list, double-check the state of the S2S callable field
    • You can optionally restrict the webhook to be callable via a range of ip ranges (good for additional security)
    • Click [Save] and copy the webhook URL that gets generated

    Step 3 - Go to the external service, and give it your webhook URL

    • This step varies by service of course.

    Step 4 - Trigger the webhook to test

    • This also varies by service. You could use Postman to do it as well.

    Step 5 - Confirm the structure of the incoming parameters

    • It is a bit difficult to figure out what a webhook will send you ahead-of-time
    • The simplest is to invoke the hook, and then examine the results
    • See the sample logging script below for an example
    • Then view the logged input data via Monitoring | Global Monitoring | Recent Errors. Be sure to enable Info -level messages, and click [Refresh]

    Step 6 - Complete the writing of your script


    Sample script - just dumps parameters:

    var response = {};
    
    bridge.logInfoJson("Script invoked from webhook. Contents of data...", data);
    
    // Return the webhook response
    response.statusOverride = 200;
    response.jsonResponse = {};
    
    response;
    

    Note - you can also view the request sent and response received from your webhook via the Server Logs. For more information on brainCloud logs - see this knowledge base article.

    Hope that helps!

    Paul.


  • brainCloud

    Hi,

    Here are the steps to setting up a webhook to call a custom cloud code script in brainCloud:

    Step 1 - Write your script

    • Go to Design | Cloud Code | Scripts and create your script
    • You might want to just start with a simple script that logs the incoming parameters (see sample below)
    • Be sure to enable the S2S callable field on the Details page.

    Step 2 - Declare the webhook and link it to your script

    • Go to Design | Cloud Code | WebHooks and create your webhook
    • Link it to the script that you just wrote. If you don't see your script in the list, double-check the state of the S2S callable field
    • You can optionally restrict the webhook to be callable via a range of ip ranges (good for additional security)
    • Click [Save] and copy the webhook URL that gets generated

    Step 3 - Go to the external service, and give it your webhook URL

    • This step varies by service of course.

    Step 4 - Trigger the webhook to test

    • This also varies by service. You could use Postman to do it as well.

    Step 5 - Confirm the structure of the incoming parameters

    • It is a bit difficult to figure out what a webhook will send you ahead-of-time
    • The simplest is to invoke the hook, and then examine the results
    • See the sample logging script below for an example
    • Then view the logged input data via Monitoring | Global Monitoring | Recent Errors. Be sure to enable Info -level messages, and click [Refresh]

    Step 6 - Complete the writing of your script


    Sample script - just dumps parameters:

    var response = {};
    
    bridge.logInfoJson("Script invoked from webhook. Contents of data...", data);
    
    // Return the webhook response
    response.statusOverride = 200;
    response.jsonResponse = {};
    
    response;
    

    Note - you can also view the request sent and response received from your webhook via the Server Logs. For more information on brainCloud logs - see this knowledge base article.

    Hope that helps!

    Paul.