Add SMS to your Web App (with 4 lines of code)


In this post I’d like to quickly show you how easy it is to add SMS to your web app then give you a more in-depth look at how Twilio works as well as some more robust use-cases for SMS.

This post was inspired by a conversation I had with a friend shortly after joining Twilio. We were talking about API’s when he nonchalantly explained that he himself used Twilio to send SMS notifications from his web app. As proof he then turned his laptop around to face me and pointed to the “four lines of code [he] added” to enable SMS.

Certainly it is not always that simple. Some of our biggest customers have built entire businesses around the Twilio Platform, but everyone has to start somewhere and in my opinion these four lines of code are the place to start.

So without further adieu, I present the four lines (okay, throw in a couple extra for proper formatting) needed to send an SMS from your web app:

Or how about Ruby:

To see more awesome examples including node.js, python, C# or java visit the Twilio docs.

So now that we’ve gotten that out of the way, let’s quickly discuss how Twilio accomplishes this task.

How Twilio Works

Outbound Messages
When you send an SMS/MMS from your web app using Twilio it works like this:

As you can see sending an SMS is pretty straight forward, in fact all of this interaction is triggered with just those four lines I showed you above. But what if you want that user to be able to respond to your outgoing message? Well then your Twilio Phone Number needs some extra instructions, specifically what to do when it receives an incoming SMS.

Inbound Messages
When an SMS is received by your Twilio Number it looks something like this:


Now that we’ve looked at how Twilio interacts with your web application at a high level, it’s time to put our gloves on and dig in. We’ll start by doing some basic setup.

Setup: Get your free Twilio number and install a helper library

Sign up at Twilio to get your free Twilio number. Once you sign up click on Numbers to see your number.

If you plan on receiving SMS messages with your web app you will also need somewhere to host your TwiML instructions. There are lots of web hosting services that will host your application for free or cheaply including Heroku and Googles AppEngine.

Installing the Helper Library

Twilio helper libraries give you the ability to interact with the Twilio REST API in the language of your choice. Installing the libraries is usually quite straightforward. For example, to install the Ruby helper library you could just install the gem like so:

Here are some of officially supported Twilio helper libraries:
Click to get installation instructions and further documentation

To view all of the available helper libraries visit the Twilio docs.

Once you’ve installed the helper library of your choice it’s time to use the REST API to send and receive SMS Messages.

Next Step: Read the docs

If you’ve worked with APIs in the past you might just want to dive in to the API docs. The Twilio REST API documentation is fully stocked with examples to help you dive in. However if you’re like me and you prefer a more tutorial approach check out the Twilio Quickstart, which will walk you through sending and receiving your first SMS.

API Reference:

Quickstart: Sending and Receiving an SMS (uses PHP)

Congratulations, now you’re able to add SMS capabilities to any of your web apps in minutes.

Once you’ve grasped these basic concepts consider upping the ante and adding even more sweetness to your application. Here are some popular ideas:

Well I hope this taste of the Twilio API has left you excited to learn even more. Many of my colleagues have written awesome tutorials on using SMS for even cooler purposes. Here are a few of my favorites. Happy hacking.

  • Nilima

    Newly constructed building houses for rent in HSR Layout with cover parking.

    rent: 23000, Deposit: 10 months negotiable.

    Note: Broker charges One month rent.

    for more contact below details

    Thanks & Regards

    BG Enterprises

    Birendra B

    Mob: 7259679626

    What’sapp: 9206534065


    Type For rent
    Num. Rooms 2
    Num. Bathrooms 1
    Status New construction
    Square Meters 1200
    Square Meters (total) 12001
    Num. Floors 0
    Floor Number 2
    Other characteristics


    For buy property,flats in Bangalore, click here

  • Nilima is a free local classifieds website & online classifieds platform where you can post free advertisements related a business, product, items and service offered in India!!!., launched in 2015, a place where people can connect with each other to buy/sell/find goods and services. We have 9 Categories & Approx 150 sub-categories including Electronics, Real estate, vehicles, jobs, community, personals,Entertainment etc provides many new features for sellers to grow their business fast & promote their service in all over works for both sellers as well as buyers. so Everyone can use our free as well as premium service. Sellers can sell anything, Buyers can buy anything from
    New features :
    SEND PM Option : Buyers can send message privately to sellers related to their products or purchase. This is only best features for sellers & buyers both where they can meet to each other & discuss about products, service.

    Add Money to wallet : This option is only provide by classified site, No Other company provide this features for sellers. Through this features, sellers can add some money in their Rackons account wallet, & they can use that money to promote their business by premium ad or premium banner or many ways. And they will get Extra money also i.e. If they pay 500/- then they will get 20% extra mean 600/- will be credit in their wallet. click here to get extra money :

    Promo Code : This also best option for small & medium business sellers, If they dont want to invest money to purchase premium ad then they can use promo code then they will get some amount in their wallet & can use that money in premium ads Its also best & free service for sellers. They can use free premium ads service without pay any fees.

    Live chat Support : company also provide live chat support for both sellers as well as buyers, if they want any type of service & they are unable to find or post any type of problems then our live chat support team will help you both. Its very easy & fast service.

    Social Sharing : also provide social sharing option for sellers, will share all premium ads on all social media like facebook, twiiter, Google+, linkedin. pinterest, etc Through this sharing, buyers can see their ads & can contact them through this sharing. So this is very useful for both sellers as well as buyers.

    Mobile recharge : This is premium service. When sellers will buy premium ad then they can get Free Mobile recharge, they can see pricing of premium ad & mobile recharge system while publish an ad on site

    Many More features for sellers & buyers…So is India’s as well as all over world best classified company, No Other company will provide your thats all features.. Its Totally Free service..

    If you want more business with then you need to pay..You can also join with & can earn money..Note: Investors can also invest money in Its very big brand company..

  • Asif Alvi

    Hello Twilio
    I want to recieve message form my twilio number at my dashboard which are live and also want to send the messages from my dashboard. I made my dashboard for the purpose of receiving and sending messages from my twilio number. Please give some suggestion for that i can do these things in my dashboard. I want to use Twilio library in PHP language.
    Thanking you

    • dprothero

      Definitely check out our PHP quick start here:

      • Asif Alvi

        Thanks a lot sir,
        one more thing i wanna do. I have two twilio number , if i select the one number of them, then the messages related to that number should be appear and when i select another number then their messages should be appear. Please tell me how to do it and how can i implement in my dashboard.
        Thanks once again,

        • dprothero

          You can retrieve messages using the Messages list resource:

          You can pass a From=[your number] for one request to get all the messages from a particular phone number and then make a second request with To=[your number] to get the inbound messages.

          The other option would be to make a single request without a filter and then filter them in your own PHP code.

          • Asif Alvi

            sir can u make codes for this ?

  • Sarika

    Useful information.
    Thanks, Sarika

  • Rajat sadana

    What’s the pricing model

    • Twilio

      Hey Rajat,
      We have more info on our pricing right here
      Let us know if you have any questions! We’re happy to help.

  • Shopping Cart

    Its great blog..!!online sms service

  • ifeanyi emmanual

    must i credit my twilio account before i can send any sms with php on my site?

    • philnash

      You can always send messages to a number that you’ve verified in your account, to test the service before paying for it. If you want to send to any number then you will need to upgrade your account and add some credit.

      • ifeanyi emmanual

        Just replying for over how many months, very bad support center

        • philnash

          Apologies, I only just came across this question so I thought I’d answer it. This is our blog though, it’s not the support centre. You can find that here: If you need more urgent help, I recommend you get in touch there.


    i want to more details about the Twilio. How it works in the web application.Can u please help me out

  • andrew

    I can definitely use this on my new site instead of an email opt in