How to setup a webhook addon for jobs status change using Simple function nodeJS

Originally posted by Motaz Mohamed

I am trying to create my own addon webhook here but it doesnt seem to trigger and its always telling me invalid function

any pointers would be helpful to tell me what i am missing or not doing correctly

here is my manifest

{
    "name": "xxxxxx",
    "version": "6.0",
    "iconURL": "https:\/\/www.servicem8.com\/images\/addon-sdk-sample-icon.png",
    "supportURL": "https:\/\/support.exampleaddon.com",
    "supportEmail": "support@exampleaddon.com",
    "oauth": {
        "scope": "manage_jobs read_jobs"
    },
    "actions": [],
    "menuItems": [],
    "webhooks": [{
        "object": "job",
        "fields": [
            "status"
        ]}
    ]
}

and here is my simple function


var request = require('request');

exports.handler = (event, context, callback) => {

    var strJobUUID = event.eventArgs.entry[0].uuid;
    var strAccessToken = event.auth.accessToken;
     
    
    var jobData = {
        status: 'Completed',
        uuid: strJobUUID
    };
    
                request.post({
                    url: 'third_party_endpoint',
                    headers: {
                               accept: 'application/json',
                               'User-Agent': 'Mozilla/5.0',
                                //   'Authorization': authorizationHeader
                            },
                    body: JSON.stringify(jobData)
                }, function (err, httpResponse, body) {

                   callback(null, {result: "job added to third party"});
                });
callback(null, {result: "job added to third party"});

the addon keeps telling me invalid function

Not Ready (Function Not Valid)

Hello

i was able to resolve this

I have found out the following as i was troubleshooting

1- simple function nodeJS currently only accepts and async function

your function handler needs to be async therefore any requests need to have an await keyword

2- the request library seems to be depereceated and no longer works here to make https post or get calls
I have found a good alternative that works is to use

a fetch method instead
there is other librarys as well you can use like axios

Example

//example to fetch job Data on a job webhook trigger
//based on the manifest posted this function will listen to any create , update or delete triggers on the job record in servicem8 and fetch its data and send it to another third_part_application
exports.handler = async (event, context, callback) => {

    var strJobUUID = event.eventArgs.entry[0].uuid;
    var strAccessToken = event.auth.accessToken;
     
    
    var jobData = {};

await fetch('https://api.servicem8.com/api_1.0/job.json?' + encodeURIComponent('$filter') + '=' + encodeURIComponent('uuid eq \'' + strJobUUID + '\''), {
        method: "GET",
        headers: {
            'Authorization': 'Bearer ' + strAccessToken
        },
    })
        .then(res => res.json())
        .then(res => jobData = res[0]);
    
   await fetch('third_party_endpoint', {
        method: 'POST',
        headers: {
            accept: 'application/json',
            'User-Agent': 'Mozilla/5.0',
            //'Authorization': authorizationHeader
        },
        body: JSON.stringify(jobData)
    });
};

Hi Motaz,

I can see a few issues with your webhook function that are likely causing the “Function Not Valid” error.

  1. Deprecated Library: You’re using the request library which is deprecated. Simple Functions only support Node.js (JavaScript), and you should use modern HTTP libraries like axios or Node’s built-in https module.

  2. Callback Issues: You have two callback() calls in your function - one inside the request callback and another immediately after. This can cause function execution problems as the second callback will be called immediately without waiting for the HTTP request to complete.

  3. Missing Content-Type Header: When posting JSON data, you need to include the Content-Type: application/json header.

  4. Simple Function Limitations: Remember that Simple Functions have a maximum execution duration of 15 seconds and 128 MB memory limit.

Thanks,
Cody