# External Script calling external API - Timed out

**URL:** <https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609>\
**Category:** Uncategorized\
**Created:** [February 18, 2020, 10:06pm UTC](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609 "2020-02-18T22:06:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tripout](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/tripout/32/164_2.png) [@tripout](https://forum.genieacs.com/u/tripout)\
**Post date:** [February 18, 2020, 10:06pm UTC](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609/1 "2020-02-18T22:06:51Z")

</div>

Hello,

I have looked at a couple different example of how to call an external API using genieacs, I have even tried the one listed in the docs to get the lat/long of the ISS but all my checks keep coming back with time out.

For reference here is what the script should return when you give it an IP,

```
curl --location --request GET 'http://rancid.abccomm.com/tools/lte/api.php?ip=104.36.101.255'

```

Response:  
{“ip\_addr”:“104.36.foo.bar”,“IMSI”:“302425111112882”,“custom\_ICCID”:“474544”,“PlatID”:null,“code”:“OK”}

Here is my code:

```
const getIMSI = ext("ipam_imsi","getIMSI", CPE_IP);

```

/opt/genieacs/ext/ipam\_imsi.js

```
genieacs@acs:~$ cat /opt/genieacs/ext/ipam_imsi.js
var http = require("http");

function getIMSIfunc() {
  let IP = arguments[0];
  var options = {
    'method': 'GET',
    'hostname': 'rancid.abccomm.com',
    'path': '/tools/lte/api.php?ip=' + IP,
    'headers': {
    },
    'maxRedirects': 20
  };
var req = http.request(options, function (res) {
var chunks = [];

res.on("data", function (chunk) {
  chunks.push(chunk);
});

res.on("end", function (chunk) {
  var body = Buffer.concat(chunks);
  console.log(body.toString());
      var result = JSON.parse(body.toString());
      return result;
});
req.end();
res.on("error", function (error) {
  console.error(error);
 });
});
}
exports.getIMSI = getIMSIfunc;

```

I get this in my cwmp-access.log

```
Channel has faulted; channel="test" retries=0 faultCode="timeout" faultMessage="Extension timed out"
```

---

<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:** [February 18, 2020, 11:51pm UTC](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609/2 "2020-02-18T23:51:12Z")

</div>

You are missing the callback. Try this:

```javascript
var http = require("http");

function getIMSIfunc(args, callback) {
    let IP = args;
    var options = {
        'hostname': 'rancid.abccomm.com',
        'path': '/tools/lte/api.php?ip=' + IP,
        'headers': {
        },
        'maxRedirects': 20
    };
    var req = http.get(options, function (res) {
        var chunks = [];

        if (res.statusCode === 404) {
            return callback(null, null);
        }

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function (chunk) {
            var body = Buffer.concat(chunks);
            console.log(body.toString());
            var result = JSON.parse(body.toString());

            callback(null, result);
        });
    });

    req.on("error", function (err) {
        callback(err);
    });

}
exports.getIMSI = getIMSIfunc;

```

---

<div class="post-metadata">

**Author:** ![tripout](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/tripout/32/164_2.png) [@tripout](https://forum.genieacs.com/u/tripout)\
**Post date:** [February 19, 2020, 12:00am UTC](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609/3 "2020-02-19T00:00:35Z")

</div>

Thank you! I could’ve swore I tried the callback but maybe was using callback(result, null) instead. Either way it works now, thank you!

---

<div class="post-metadata">

**Author:** ![hrc91](https://avatars.discourse-cdn.com/v4/letter/h/9e8a1a/32.png) [@hrc91](https://forum.genieacs.com/u/hrc91)\
**Post date:** [January 7, 2023, 5:20am UTC](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609/4 "2023-01-07T05:20:58Z")

</div>

Hi @akcoder  
I am also having a similar problem with my POST request to an external system.

/opt/genieacs/ext/addnewdev.js

```auto
"use strict";
var http = require("http");
var result = null;

function addDevSync(oui,modelname,serial,callback) {
    
    var options = {
      'method': 'POST',
      'hostname': '10.72.0.189',
      'port': 9111,
      'path': '/api/device/sync-and-merge',
      'headers': {
        'Content-Type': 'application/json'
      },
      'maxRedirects': 20
    };

    var req = http.request(options, function (res) {
      var chunks = [];

     if (res.statusCode !== 200)
        return callback(
          new Error(`Request failed (status code: ${res.statusCode})`)
        );

      res.on("data", function (chunk) {
        chunks.push(chunk);
      });

      res.on("end", function (chunk) {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
        result = JSON.parse(body.toString());
        callback(null, result);
      });

      res.on("error", function (error) {
        callback(err);
      });
    });

    var postData = JSON.stringify({
      "serial": serial,
      "oui": oui,
      "modelName": modelname
    });

    req.write(postData);

    req.end();
}

exports.addDevSync = addDevSync;

```

Here is the provision script

```auto
let serial = declare("InternetGatewayDevice.DeviceInfo.SerialNumber", {value: 1});
let oui = declare("InternetGatewayDevice.DeviceInfo.ManufacturerOUI", {value: 1});
let modelname = declare("InternetGatewayDevice.DeviceInfo.ModelName", {value: 1});
log("Device ID is: "+oui.value[0] +"-"+modelname.value[0]+"-"+serial.value[0]);
ext("addnewdev", "addDevSync", oui.value[0], modelname.value[0], serial.value[0]);

```

When receiving an event from device, the provision script run, but it returned Timed out error

```auto
2023-01-07T04:52:24.339Z [INFO] ::ffff:10.72.203.192 xxxxxx-xxx-xxxxxx: Inform; cpeRequestId="16002" informEvent="0 BOOTSTRAP,1 BOOT" informRetryCount=0
2023-01-07T04:52:44.964Z [INFO] ::ffff:10.72.203.192 xxxxxx-xxx-xxxxxx: Script: Device ID is: xxxxxx-xxx-xxxxxx
2023-01-07T04:52:47.966Z [WARN] ::ffff:10.72.203.192 xxxxxx-xxx-xxxxxx: Channel has faulted; channel="bootstrap" retries=0 faultCode="timeout" faultMessage="Extension timed out"
2023-01-07T04:52:47.982Z [INFO] ::ffff:10.72.203.192 xxxxxx-xxx-xxxxxx: New device registered

```

Would you help to correct my extension script?  
Thank you so much!

---

<div class="post-metadata">

**Author:** ![usta11](https://avatars.discourse-cdn.com/v4/letter/u/ecb155/32.png) [@usta11](https://forum.genieacs.com/u/usta11)\
**Post date:** [February 17, 2023, 6:07pm UTC](https://forum.genieacs.com/t/external-script-calling-external-api-timed-out/609/5 "2023-02-17T18:07:15Z")

</div>

Did you find any solution? I’m having the same issue sometimes even external script ended successfully. I tried to increase EXT\_TIMEOUT but still I get timeout error.
