Sending SMS in Go (Golang): Overview of API Libraries and Implementation Examples
Sending SMS messages is one of the key functions of modern web applications: from two-factor authentication to order status notifications. In the Go (Golang) ecosystem, there are several mature libraries that allow you to integrate SMS sending through popular gateways (Twilio, Vonage, Clickatell, Russian providers). In this article, we will thoroughly examine how to set up SMS sending in Go using best practices and official APIs.
Why Go for Sending SMS?
Go is a compiled language with excellent support for concurrency, which is ideal for asynchronous sending of large numbers of messages. Thanks to built-in goroutines and channels, you can process thousands of SMS requests without blocking the main thread. Additionally, the standard net/http library makes it easy to work with the REST APIs of any SMS provider.
Main Libraries for Working with SMS in Go
- twilio-go — official client for Twilio (the most popular international service). Supports SMS, MMS, voice calls.
- vonage-go (formerly Nexmo) — client for the Vonage API.
- go-sms — a universal library with support for multiple providers (SMS.ru, Twilio, Clickatell).
- gosms — a simple wrapper for HTTP requests to any SMS gateway.
Installing the Library
For the example, we will use the official Twilio client. Install the package via go get:
go get github.com/twilio/twilio-goFor the Russian provider SMS.ru, there is also a convenient library:
go get github.com/smsru/smsru-goBasic Example: Sending SMS via Twilio
Before starting, obtain your Account SID and Auth Token from the Twilio console. Below is minimal working code:
package main
import ( "fmt" "os"
"github.com/twilio/twilio-go" openapi "github.com/twilio/twilio-go/rest/api/v2010")
func main() { // Initialize the client client := twilio.NewRestClientWithParams(twilio.ClientParams{ Username: os.Getenv("TWILIO_ACCOUNT_SID"), Password: os.Getenv("TWILIO_AUTH_TOKEN"), })
// Message parameters params := &openapi.CreateMessageParams{} params.SetTo("+1234567890") // recipient number params.SetFrom("+0987654321") // your Twilio number params.SetBody("Hello! This is a test SMS from Go!")
// Sending resp, err := client.Api.CreateMessage(params) if err != nil { fmt.Println("Error sending:", err) return }
fmt.Println("SMS sent! SID:", *resp.Sid)}Sending SMS via a Russian Gateway (SMS.ru)
For integration with SMS.ru, use the smsru-go library. The code will look like this:
package main
import ( "fmt" "os"
"github.com/smsru/smsru-go")
func main() { // Create a client with an API key client := smsru.NewClient(os.Getenv("SMSRU_API_KEY"))
// Send a single message result, err := client.SendSMS("+79991234567", "Hello! This is a test from Go!") if err != nil { fmt.Println("Error:", err) return }
if result.Status == "OK" { fmt.Println("SMS sent! ID:", result.SmsID) } else { fmt.Println("Error:", result.StatusText) }}Asynchronous Sending Using goroutines
One of the main advantages of Go is the ability to easily send SMS in parallel. Example of mass sending:
package main
import ( "fmt" "sync")
func sendSMSAsync(client *smsru.Client, phone, message string, wg *sync.WaitGroup) { defer wg.Done() resul