Skip to content

RJPearson94/twilio-sdk-go

Repository files navigation

Twilio Go SDK

Twilio SDK PkgGoDev Release Go Report Card License

This SDK is designed to allow you to interact with Twilio API's using Golang.

This project is compatible with the following versions of Golang:

Version Supported
1.16.x Yes
1.17.x Yes
1.18.x Yes

⚠️ Disclaimer: This project is not an official Twilio project and is not supported or endorsed by Twilio in any way. It is maintained in my free time.

Getting Started

NOTE: The default branch for this project is called main

Documentation

The code uses go doc style documentation with links to the relevant Twilio API documentation/ guides where appropriate.

Examples

Example code snippets for all of the supported services & resources can be found here

Initialising the Twilio Client

There are many ways to initialise and configure the Twilio Client. See below for examples:

With Credentials

creds, err := credentials.New(credentials.Account{
    Sid:       os.Getenv("TWILIO_ACCOUNT_SID"),
    AuthToken: os.Getenv("TWILIO_AUTH_TOKEN"),
})
if err != nil {
    log.Panicf("%s", err.Error())
}

twilioClient := twilio.NewWithCredentials(creds)

With Session

creds, err := credentials.New(credentials.Account{
    Sid:       os.Getenv("TWILIO_ACCOUNT_SID"),
    AuthToken: os.Getenv("TWILIO_AUTH_TOKEN"),
})
if err != nil {
    log.Panicf("%s", err.Error())
}

twilioClient := twilio.New(session.New(creds))

With Session & Config

The Twilio Client allows the user to supply configuration to alter the default behaviour of the SDK.

The SDK supports the following configuration:

  • BackoffInterval - The time taken in milliseconds between retries
  • DebugEnabled - This logs out the request and response details for each call to the Twilio API
  • Edge - Specify a public edge or private interconnect to connect to Twilio via. See Global Infrastructure - Edge Locations for more information
  • Region - Specify a public region or private interconnect region to connect to Twilio via. See Global Infrastructure - Legacy Regions for more information
  • RetryAttempts - The number of retry attempts before an error is returned

Enabling debug mode

creds, err := credentials.New(credentials.Account{
    Sid:       os.Getenv("TWILIO_ACCOUNT_SID"),
    AuthToken: os.Getenv("TWILIO_AUTH_TOKEN"),
})
if err != nil {
    log.Panicf("%s", err.Error())
}

twilioClient := twilio.New(session.New(creds), &client.Config{
   DebugEnabled: true,
})

Specifying Edge & Region configuration

creds, err := credentials.New(credentials.Account{
    Sid:       os.Getenv("TWILIO_ACCOUNT_SID"),
    AuthToken: os.Getenv("TWILIO_AUTH_TOKEN"),
})
if err != nil {
    log.Panicf("%s", err.Error())
}

twilioClient := twilio.New(session.New(creds), &client.Config{
    Edge:       utils.String("dublin"),
    Region:     utils.String("ie1"),
})

Used By

With Thanks

This project is very heavily inspired and influenced by other open-source projects including: