I'm using node-twilio and I keep getting a "Error: Unable to reach host: "api.twilio.com"
for every request. We've checked the packets via mtr
and they are reaching api.twilio.com. Running on debian on GCE.

- 428
- 4
- 17
-
Are you configured your accountSid & authToken ? – Şivā SankĂr May 08 '16 at 07:41
-
1Ref out this documentation https://www.twilio.com/docs/node/install. If possible post your code here – Şivā SankĂr May 08 '16 at 07:42
2 Answers
After days of digging around, found out that the node-twilio module shows many errors incorrectly as:
"Error: Unable to reach host: "api.twilio.com".
The following lines:
var error = null;
if (err || (response && (response.statusCode < 200 || response.statusCode > 206))) {
error = {};
// response is null if server is unreachable
if (response) {
error.status = response.statusCode;
error.message = data ? data.message : 'Unable to complete HTTP request';
error.code = data && data.code;
error.moreInfo = data && data.more_info;
} else {
error.status = err.code;
error.message = 'Unable to reach host: "'+client.host+'"';
}
}
This happens because you have a self signed certificate in your chain and the underlying module twilio depends on is request, which is throwing the following error:
Error: SELF_SIGNED_CERT_IN_CHAIN
but this is not the error being thrown by node-twilio (bad error propagation on their part)
There are 2 fixes:
1.Tell nodejs to ignore self signed certificates in chain by setting:
export NODE_TLS_REJECT_UNAUTHORIZED=0
- Find the self signed certificate and remove it from the chain. Here is an example using openssl: https://serverfault.com/questions/590870/how-to-view-all-ssl-certificates-in-a-bundle
References:
https://github.com/request/request
https://github.com/twilio/twilio-node/blob/45858420688854494c2ed476a1997773c33a32a0/lib/Client.js
Ignore invalid self-signed ssl certificate in node.js with https.request?
-
I had a similar problem with getting the useless `"Error: Unable to reach host: "api.twilio.com".` error. The trick for me was to set a breakpoint in `/path/to/root/dir/node_modules/twilio/lib/Client.js` in the line where `request` is called, and then inspect the `err` object – almel Jun 22 '17 at 18:14
It may be because of your internet connection.
After couple of minutes, if you have internet, try again and it should work.

- 1,992
- 1
- 21
- 34