What's the correct way to do a health check on a Twilio connection in TypeScript?

carlspring

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?

jassent

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the correct way to do inheritance in TypeScript for React components?

What's the correct way to check if a collection exists?

What is the correct way to check for String equality in Typescript (for Angular 4)

What is the correct way to do this?

What's the correct way to check if an object is a typing.Generic?

What is the correct way to check an object's type in a conditional statement - python

What is the correct way to define a React component's contextTypes in TypeScript?

what is the correct way to check for False?

Is their a way to reset a Twilio device's handler function for what happens on connection Disconnect

What's the Correct Way to Do a Read with While Loop

What's the correct way to do a rotating log with python twisted?

What's the correct way to do IN (date-range) in Postgres?

What is the best way to scan a hard drive/check health in Ubuntu?

What's the difference between elb health check and ec2 health check?

Using HikariCP's connection pool the correct way

What is the correct way to check url type value?

What is the correct way to check for a '?' in a case statement

What is the correct way to check if a global variable exists?

What is the correct way to check if a cell in a database is null

What is the correct way to check if CancellationToken was invoked for backgroundservice

What is the correct way to check for "possibly null" references?

What is the correct way to add a property to an Object [Typescript]

what is the correct way to import a module in Typescript?

What is the correct way to "do nothing" in a blocking way?

What is the Best way to check internet connection in Ionic

What's the correct way to implement a user-defined do.call's what function?

How to check rabbitMQ connection(health check) up or not?

What is the correct way to check object's property if object not undefined in angular 2 template?

Correct way to check zookeeper's running mode