# Webhook to get device updates

**URL:** <https://forum.genieacs.com/t/webhook-to-get-device-updates/2160>\
**Category:** Uncategorized\
**Created:** [November 8, 2021, 10:22pm UTC](https://forum.genieacs.com/t/webhook-to-get-device-updates/2160 "2021-11-08T22:22:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@jacob](https://forum.genieacs.com/u/jacob)\
**Post date:** [November 8, 2021, 10:22pm UTC](https://forum.genieacs.com/t/webhook-to-get-device-updates/2160/1 "2021-11-08T22:22:49Z")

</div>

Hi,  
Is there a way to register a webhook in order to get callbacks when devices are updated/registered or is the only way to periodically check for changes via api ?

Thanks

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [November 9, 2021, 9:18pm UTC](https://forum.genieacs.com/t/webhook-to-get-device-updates/2160/2 "2021-11-09T21:18:43Z")

</div>

Use a preset that calls [provision](https://docs.genieacs.com/en/latest/provisions.html) which calls an [extension script](https://docs.genieacs.com/en/latest/extensions.html).

Here is what I use to log bootstraps (another variation of this logs 1 BOOT as well):

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/1X/9d395c02beca5ba8d0bfd3a067ea8691eeb5e5d8.png)

```auto
//Send_Events provision script

let deviceId = null;
let serial = declare('DeviceID.SerialNumber', {value: 1}).value[0];
let productClass = declare('DeviceID.ProductClass', {value: 1}).value[0];
let model = declare('VirtualParameters.Model', {value: 1}).value[0];
let softwareVersion = declare('VirtualParameters.SoftwareVersion', {value: 1}).value[0];
let oui = declare('DeviceID.OUI', {value: 1}).value[0];
let events = args;
let did = declare('DeviceID.ID', {value: 1});

//Brand new devices won't have the Device.ID set yet
if (did.size) {
    deviceId = did.value[0];
}

const params = {deviceId, serial, productClass, oui, model, softwareVersion, events};

ext('events', 'send', JSON.stringify(params));

```

You will need to edit your genieacs.env file and add an entry for API\_URL (not GENIEACS\_API\_URL, just API\_URL) that points to where ever you want the data posted to. Remember to restart the CWMP process after editing the .env file

```auto
//ext/events.js
const url = require('url');
const process = require('process');
const logger = require('../../lib/logger');
const API_URL = process.env.API_URL;
const https = require('./https').create();

function send(args, callback) {
    let params = JSON.parse(args[0]);

    const uri = API_URL + 'Event?serial=' + params.serial + '&productClass=' + params.productClass + '&oui=' + params.oui;
    const logParams = { deviceId: params.deviceId, remoteAddress: 'events:send', events: params.events };
    logger.info(Object.assign({ message: uri }, logParams, params));

    let postData = JSON.stringify(params);

    let options = url.parse(uri);
    options.headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Content-Length': postData.length
    };

    options.method = 'POST';

    let request = https.request(options, (response) => {
        if (response.statusCode >= 400) {
            logger.error(Object.assign({ message: 'Unexpected error sending event', statusCode: response.statusCode, statusMessage: response.statusMessage }, logParams));
            return callback(new Error('Unexpected error sending event. Response Code: ' + response.statusCode + '. Status Message: ' + response.statusMessage));
        }

        let data = '';
        response.on('data', function (d) {
            data = data + d.toString();
        });

        response.on('end', function () {
            return callback(null, data);
        });
    });

    request.on('error', function (err) {
        console.log('args');
        console.log(arguments);
        callback(err);
    });

    request.write(postData);
    request.end();
}

exports.send = send;

```
