twilio-go

Project Status
This project is currently in PILOT and in active development. If you've identified an issue, please open an issue. If you would like to particulate in the pilot, please sign up for Twilio Insiders twil.io/insider.
All the code here was generated by twilio-oai-generator by leveraging openapi-generator and twilio-oai. If you find an issue with the generation or the openapi specs, please go ahead and open an issue or a PR against the relevant repositories.
Documentation
The documentation for the Twilio API can be found here.
Supported Go Versions
This library supports the following Go implementations:
Installation
To use twilio-go in your project initialize go modules then run:
go get github.com/twilio/twilio-go/twilio
Getting Started
Getting started with the Twilio API couldn't be easier. Create a
Client and you're ready to go.
API Credentials
The Twilio Client needs your Twilio credentials. You should pass these
directly to the constructor (see the code below).
package main
import "github.com/twilio/twilio-go/twilio"
func main(){
accountSID := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
client := twilio.NewClient(accountSID, authToken)
}
We suggest storing your credentials as environment variables and then use it in your code. Why? You'll never have to worry about committing your credentials and accidentally posting them somewhere public.
package main
import (
"github.com/twilio/twilio-go/twilio"
"os"
)
func main(){
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
client := twilio.NewClient(accountSid, authToken)
}
Specify a Region and/or Edge
package main
import (
"github.com/twilio/twilio-go/twilio"
"os"
)
func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
client := twilio.NewClient(accountSid, authToken)
client.SetRegion("au1")
client.SetEdge("sydney")
}
This will result in the hostname transforming from api.twilio.com to api.sydney.au1.twilio.com.
A Twilio client constructed without these parameters will also look for TWILIO_REGION and TWILIO_EDGE variables inside the current environment.
Buy a phone number
package main
import (
"fmt"
twilio "github.com/twilio/twilio-go/twilio"
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
"os"
)
func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
phoneNumber := "AVAILABLE_TWILIO_PHONE_NUMBER"
client := twilio.NewClient(accountSid, authToken)
params := &openapi.CreateIncomingPhoneNumberParams{}
params.PhoneNumber = &phoneNumber
resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
if err != nil {
fmt.Println(err.Error())
err = nil
} else {
fmt.Println("Phone Number Status: " + *resp.Status)
}
}
Send a text message
package main
import (
"fmt"
twilio "github.com/twilio/twilio-go/twilio"
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
"os"
)
func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
client := twilio.NewClient(accountSid, authToken)
text := "Hello there"
params := &openapi.CreateMessageParams{}
params.To = &to
params.From = &from
params.Body = &text
resp, err := client.ApiV2010.CreateMessage(accountSid, params)
if err != nil {
fmt.Println(err.Error())
err = nil
} else {
fmt.Println( "Message Status: " + *resp.Status)
fmt.Println( "Message Sid: " + *resp.Sid)
}
}
Make a call
package main
import (
"fmt"
twilio "github.com/twilio/twilio-go/twilio"
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
"os"
)
func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
client := twilio.NewClient(accountSid, authToken)
callurl := "http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient"
params := &openapi.CreateCallParams{}
params.To = &to
params.From = &from
params.Url = &callurl
resp, err := client.ApiV2010.CreateCall(accountSid, params)
if err != nil {
fmt.Println(err.Error())
err = nil
} else {
fmt.Println("Call Status: " + *resp.Status)
fmt.Println("Call Sid: " + *resp.Sid)
fmt.Println("Call Direction: " + *resp.Direction)
}
}
Handling Exceptions
package main
import (
"fmt"
twilio "github.com/twilio/twilio-go/twilio"
"github.com/twilio/twilio-go/framework/error"
openapi "github.com/twilio/twilio-go/twilio/rest/api/v2010"
"os"
)
func main() {
accountSid := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
phoneNumber := os.Getenv("TWILIO_PHONE_NUMBER")
client := twilio.NewClient(accountSid, authToken)
params := &openapi.CreateIncomingPhoneNumberParams{}
params.PhoneNumber = &phoneNumber
resp, err := client.ApiV2010.CreateIncomingPhoneNumber(accountSid, params)
if err != nil {
twilioError := err.(*error.TwilioRestError)
fmt.Println(twilioError.Error())
}
}
For more descriptive exception types, please see the Twilio documentation.
Building
To build twilio-go run:
go build ./...
Testing
To execute the test suite run:
go test ./...