Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?


Today we're going to get your server to automatically sound the (textual) alarm when something goes wrong. Using Node.js and the Express framework, we'll light up the phones of all of your server administrators when there's an exception. Read on to send automatic server notifications through SMS and MMS when your code throws an exception.

Start by cloning the sample application from Github, here. Then head to the application's README.md to see how to run it locally.

EMCがいかにしてTwilio SMSを使用して68,000人の従業員にITアラートを送信しているか見てみましょう


Onward and Upward

Configure a Twilio REST client

In order to send messages we'll need to create a Twilio REST client, which requires first reading a TWILIO_ACCOUNT_SID, TWILIO_NUMBER, and TWILIO_AUTH_TOKEN from environmental variables.

We also make the variables available through the config module.

The values for your account SID and Auth Token come from the Twilio console:


Click the eyeball icon to expose your Auth Token to copy/paste.

You will have to use a purchased number for the TWILIO_NUMBER variable. Phone numbers can be found and added in the Twilio console.

The link above will explain how to set environmental variables on Mac OSX, Windows, and *NIX (although may vary based on choice of shell). On other platforms it might be in a console or set in some other way. You probably have to read platform specific documentation to see where to set these. Read it, though - best practice dictates you keep them out of the code itself.


        You found what you needed from the console - next, let's get your server administrators in a list.

        Who Ya Gonna SMS?

        List Your Server Admins and Well-Wishers

        Here we create a JSON formatted list of administrators who should be notified if a server error occurs. The only essential piece of data we'll need is a phoneNumber for each administrator.




              Next, let's have a look at how we're going to capture application exceptions.

              All Your Exceptions Are Belong to Us

              Handle All The Unexpected Application Exceptions

              エラー処理およびメッセージ配信を 1 つの Express.js ミドルウェアとして実装します。

              We'll make all our Twilio API calls from inside our custom middleware.


                    Now you've seen the main server application - let's look in detail at how we'll send SMS notifications to the administrators in case of emergency.

                    Urgent, Urgent... Emergency

                    Trigger Notifications for Everyone on the Administrator List

                    In our Express middleware module, we read all the soon-to-be-awoken administrators from our JSON file and send alert messages to each of them. We do that using the sendSms method in twilioClient. We also call next with the error object, which will allow other configured middleware to execute after us.


                          And that's how our notifications will be prepared - let's take a look now at how the server will send the message(s).

                          Send SMS or MMS Messages from the Server

                          There are three parameters needed to send an SMS using the Twilio REST API: from, to, and body. US and Canadian phone numbers can also send an image with the message (other countries will have an automatic shortened URL). Just uncomment mediaUrl and insert your choice of image, serious or funny.




                                And with that, you've seen how easy it is to add powerful administrative features with Twilio! Let's explore where you might want to look next...

                                Nice Feature! What Else Can I Build?


                                At Twilio, we love Node.js - and we have plenty of Node content for you to keep those browser tabs full.

                                Two-Factor Authentication with Authy and Node.js

                                Increase the security of your login system by verifying a user's identity using Twilio's Authy.

                                SMS・MMS マーケティング通知

                                SMS and MMS messages are a personal way to engage with users. There are many benefits of marketing directly to phones, but perhaps most of all - SMSes and MMSes are opened at a rate far greater than email.


                                What are you going to build next? Hit up the Twilio account on Twitter and let us know!

                                Kevin Whinnery Jose Oliveros Agustin Camino David Prothero Kat King Samuel Mendes Paul Kamp Andrew Baker
                                Rate this page:


                                誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。