I would like to implement an end-point in TypeScript that checks if the Twilio connection is established properly. What's the correct way to check this?
I have something like the following code:
private twilioClient: Twilio;
...
async checkTwilio() {
this.twilioClient.messages.list({ limit: 10 })
.then(function(messages) {
console.log("Twilio connection works!");
return HttpStatus.OK;
})
.catch(function(err) {
console.error("Failure to establish Twilio connection!", err)
return HttpStatus.BAD_REQUEST;
})
}
Is there a better way to do this?
I use the Account endpoint to do a health check. This confirms that you can reach the Twilio REST Api, that the Twilio REST Api is responding, and that the account is active. For example, if your credit card expired your account could become suspended. Checking the Account endpoint will confirm that your account is working from a technical and billing perspective.
Here is some node.js code from the Twilio documentation page to "Fetch Account" which is similar enough to Typescript:
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
client.api.v2010.accounts('ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
.fetch()
.then(account => console.log(account.friendlyName));
The above returns:
{
"auth_token": "auth_token",
"date_created": "Thu, 30 Jul 2015 20:00:00 +0000",
"date_updated": "Thu, 30 Jul 2015 20:00:00 +0000",
"friendly_name": "friendly_name",
"owner_account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"status": "active"
}
Notice the "status": "active" property. The status can be can be closed, suspended or active.
I bring this up because I once had my account suspended because a credit card charge declined and I didn't have a backup on file. The phones rang disconnected for a few hours until the situation was corrected.