Using SMS and Voice Services via Twilio





Twilio is a pay-as-you-go service. There are no set-up fees and you can close your account at any time. You can find more details at Twilio Pricing.


The Twilio platform consists of the Twilio Markup Language (TwiML), a RESTful API and VoIP SDKs for web browsers, Android and iOS. Helper libraries are available in multiple languages. You can find the full list at Twilio Helper Libraries.


TwiML is a set of instructions that you can use to tell Twilio what to do when you receive an incoming call or SMS. When someone makes a call or sends an SMS to one of your Twilio numbers, Twilio will look up the URL associated with that phone number and make a request to that URL. Twilio will read TwiML instructions at that URL to determine what to do:

  • <Say> - text to speech
  • <Record> - record the call
  • <Play> - play a message for the caller
  • <Gather> - prompt the caller to press digits on their keypad
  • <Message> - send a message

Learn about the other verbs and capabilities from the Twilio Markup Language documentation.


The Twilio REST API allows you to query metadata about your account, phone numbers, calls, text messages, and recordings. You can also do some fancy things like initiate outbound calls and send text messages.

Because the API is based on REST principles, it's very easy to write and test applications. You can use your browser to access URLs, and you can use pretty much any HTTP client in any programming language to interact with the API. For more information, see the Twilio REST API.

Copying the sample code

A complete code sample showing integration with Twilio is available at GitHub. If you want to use this sample code, copy it to your local machine:

go get -u -d -v
cd $GOPATH/src/


To use Twilio to receive an incoming call, send an SMS, or receive an SMS:

  1. Create a project. For further instructions, see "Hello, World!" for Go on App Engine.

  2. Create a Twilio account.

  3. In Twilio, create a number. Configure the URLs for your Twilio number as needed for your app. For example, for the sample code:

    • Configure the voice request URL to be https://[your-app-id]
    • Configure the SMS request URL to be https://[your-app-id]
  4. Configure your Twilio settings in the environment variables section in app.yaml. For example:

      TWILIO_ACCOUNT_SID: your-account-sid
      TWILIO_AUTH_TOKEN: your-auth-token
      TWILIO_NUMBER: your-twilio-number

  5. Download the Twilio packages to your local machine using the command line. For example:

    go get -u
    go get -u
  6. Include the Twilio package in your application's import statement. For example:

    import (

  7. To prepare for local testing of your app, set the environment variables on your local machine using the command line. For example, for the sample code you can use:

    export TWILIO_ACCOUNT_SID=[your-twilio-account-sid]
    export TWILIO_AUTH_TOKEN=[your-twilio-auth-token]
    export TWILIO_NUMBER=[your-twilio-number]
  8. You can run the application locally to test the callbacks and SMS sending. For example:

    go run twilio.go

Receiving an incoming call

After you complete Setting up, create your first application:

The receiveCallHandler function in twilio.go shows how to respond to an incoming call and return TwiML instructions:
func receiveCallHandler(w http.ResponseWriter, r *http.Request) {
	resp := twiml.NewResponse()
	resp.Action(twiml.Say{Text: "Hello from App Engine!"})
The /call/receive handler in app.rb shows how to respond to an incoming call and return TwiML instructions:

# Answers a call and replies with a simple greeting.
post "/call/receive" do
  content_type :xml

  response = do |r|
    r.say(message: "Hello from Twilio!")

  1. Deploy your project to App Engine. After you deploy your project, you can send an HTTP POST request to, which returns the following text:

        <Say>Hello from Twilio!</Say>
  2. Copy and paste the URL into the Voice URL box on the Numbers page of your Twilio Account.

  3. Now call your Twilio number! You should hear a voice say "Hello from Twilio!" in response. When you call, Twilio will fetch your URL, and execute the XML instructions listed above. Then Twilio will hang up, because there are no more instructions.

Sending an SMS

The sendSMSHandler function in twilio.go shows how to how to send an SMS:
func sendSMSHandler(w http.ResponseWriter, r *http.Request) {
	to := r.FormValue("to")
	if to == "" {
		http.Error(w, "Missing 'to' parameter.", http.StatusBadRequest)

	msg := twirest.SendMessage{
		Text: "Hello from App Engine!",
		From: twilioNumber,
		To:   to,

	resp, err := twilioClient.Request(msg)
	if err != nil {
		http.Error(w, fmt.Sprintf("Could not send SMS: %v", err), 500)

	fmt.Fprintf(w, "SMS sent successfully. Response:\n%#v", resp.Message)
  1. Change the to parameter to use a real phone number. The to number can be any outgoing number, such as your mobile phone number.

  2. Deploy the updated code, and then send the SMS by putting this URL into a web browser:

Receiving an SMS

To receive an incoming SMS using Twilio, you can use the Twilio Go package.

The receiveSMSHandler function in twilio.go shows how to respond to an incoming SMS and return TwiML instructions:

func receiveSMSHandler(w http.ResponseWriter, r *http.Request) {
	sender := r.FormValue("From")
	body := r.FormValue("Body")

	resp := twiml.NewResponse()
		Body: fmt.Sprintf("Hello, %s, you said: %s", sender, body),
		From: twilioNumber,
		To:   sender,

Deploy your updated code to App Engine, and then send an SMS to your Twilio number. You should receive a greeting in response.

Learning More about Twilio

Now that you've used some basic features, learn about more features and some best practices for building secure and scalable applications:

¿Te ha resultado útil esta página? Enviar comentarios:

Enviar comentarios sobre...

App Engine flexible environment for Go