Skip to contentSkip to navigationSkip to topbar

Send & receive SMS messages

Created by: Twilio
  • SMS
  • PHP
  • Java
  • Python
  • Ruby
  • .NET
  • JavaScript
  • Go
  • Popular
(information)

Info

Regulations require that you first register your number to use these resources to send SMS traffic to US and Canadian mobile destinations. If you are sending to Canada or the US and have not already done so, please see our documentation to register your Twilio phone number(link takes you to an external page) or Toll-Free number(link takes you to an external page) before you use this template.

A flow diagram showing how send and receive SMS functionality works

Learn how to send and receive SMS with Twilio with this code sample using a combination of Twilio's Programmable Messaging API(link takes you to an external page) and TwiML (Twilio Markup Language) for Programmable Messaging(link takes you to an external page). In a few lines of code, you'll see your phone light up sending and receiving SMS with Twilio.

How it works

For replying to SMS:

  • User sends an SMS to your Twilio phone number.
  • If the message body was "never gonna", Twilio will send an SMS back to the user with a follow-on line from Rick Astley's hit "Never Gonna Give You Up"(link takes you to an external page), e.g., "run around and desert you" or "say goodbye".
  • If the message body was anything else, then "I just wanna tell you how I'm feeling - Gotta make you understand" will be sent as the body of the reply message.

For sending SMS

  • An SMS will be sent to your mobile/cell phone with the message "This is the ship that made the Kessel Run in fourteen parsecs?".
Report this template(link takes you to an external page)
  1. Choose a language

    This project is available in multiple programming languages.

    Select language
  2. Get the code for this project

    The code for this sample is available on GitHub to view.

    View on GitHub(link takes you to an external page)
  3. Get Twilio credentials

    You will need an Account SID and Auth Token in order to run this code.

    Checking for existing account...

  4. Set up the code sample locally

Need some help?

We all do sometimes; code is hard. Get help now from our support team(link takes you to an external page), or lean on the wisdom of the crowd by browsing the Twilio tag(link takes you to an external page)on Stack Overflow.

Terms of servicePrivacy Policy

Copyright © 2026 Twilio Inc.