Log In

Sign Up

3 Ways To Send Text Messages (SMS) with Golang

Digital communication has become one of the critical factors in almost all the areas, such as banking, health, government, education, and social media, for many purposes such as user authorization, appointment booking, and many more. Among different approaches to communication, SMS is one of the well-known methods.

Developers use many APIs and related approaches to implement SMS functionality in those areas. In the modern world, many developers use Golang during their development process. As a result, SMS implementation using Golang is getting popular.

This post discusses three ways to send an SMS using Golang.

01 - Using Twilio API

Twilio APIs provide seamless communication with users via voice, SMS, video, or chat. Developers can send SMS easily using the Twilio Go Helper Library.

Twilio SMS pricings and services depend on the country you live in. For example, in the UK, only sending and receiving picture messages are free of charge, and alphanumeric sender id services have a different approach.

Twilio Golang SMS Pricing

For outbound services for UK users, the pricing depends on the country.

As prerequisites, you must have a Twilio account and a Twilio mobile number. Twilio offers a $15 credit for new users when upgrading to a paid version. When using Twilio, it is essential to know its features and limitations.

Pros of Twilio

  • Powerful communication strategies and insanely fast service.
  • Developer-friendly descriptive documentation.
  • Support for many programming languages.

Cons of Twilio

  • Complex user interface for non-developers.
  • Limitations of features when you have a trial account (limited caller time, can only send SMS to non-Twilio numbers which have verified with Twilio)
  • Twilio has different features based on the region.

Tutorial on Sending SMS with Twilio

01 - First, you must create a trial account by navigating to the Twilio signup page. After filling in all the essential details, you will be prompted to the welcome page. You will have the option to select how you need to use Twilio. For example, you can use Twilio with no code at all.

However, here you need to select either with code or with minimal code and select the language as Golang.Twilio Golang Sign Up

02 - After starting the trial, you will be prompted to the dashboard, where you will have free credits of $15.

Twilio SMS Setup for Golang

Now, you can create your own main.go file and start the most exciting part.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package main import ( "fmt""strings""math/rand" "time""net/http" "net/url""encoding/json" ) func main() { // Set account keys & information accountSid := "ACXXXXXXXXX" authToken := "XXXXXXXXXX" urlStrting := "" + accountSid + "/Messages.json" message := [1]string{"Hello SMS"} rand.Seed(time.Now().Unix()) //Set up the data for the text message messageDate := url.Values{} messageDate.Set("To","numberTo") messageDate.Set("From","numberFrom") messageDate.Set("Body",quotes[rand.Intn(len(quotes))]) msgDataReader := *strings.NewReader(messageDate.Encode()) //Create HTTP request client client := &http.Client{} request, _ := http.NewRequest("POST", urlStrting, &msgDataReader) request.SetBasicAuth(accountSid, authToken) request.Header.Add("Accept", "application/json") request.Header.Add("Content-Type", "application/x-www-form-urlencoded") //Make HTTP POST request client.Do(req) }

03 - Then, you must import all the packages in the import block, and in the main package, initialize accountSid and AuthToken. You can find your accountSid and AuthToken in the account info section. urlString is the Twilio API that we use for SMS messages. The message is defined as the possible message bodies.

04 - Then, set up the messageDate by initializing the sender, the receiver number, and the message body.

05 - Next, create the HTTP request client by initializing the request and wrapping credentials using the SetBasicAuth() function.

06 - Then, make the POST request using HTTP

07 - Finally, run the following command to send the SMS.

1 go run main.go

Furthermore, the Twilio dashboard can send SMS using programming languages like Java, C#, Ruby, Python, Php, and NodeJS.

02 - Using TextMagic API

TextMagic API is a much simpler way of sending SMS using Golang. It provides a communication link between the application layer and TextMagic's API gateway. Furthermore, it allows you full access even during the trial period.

Pros of TextMagic API

  • A secure way to send and receive SMS.
  • It allows users to check the status of the message (sent or received).
  • It will enable you to attach images, PDFs, and word files in sizes less than 10MB.

Cons of TextMagic API

  • If you send a large amount of SMS and receive messages from your clients back, the service can be costly.
  • Currently, only possible to send personal text messages or use TextMagic virtual numbers for 2FA or other verification purposes because of local network carrier restrictions in some countries.

The pricing for TextMagic works is similar to Twilio APIs. For the UK, the price for an SMS, MMS, and carrier lookup is $0.05.

TextMagic SMS Pricing

Tutorial on Sending SMS with TextMagic

As a prerequisite, what TextMagic API need is to have the Go distribution on your computer.

01 - First, you must create an account in TextMagic. Here, I will sign up with Google.

Create TextMagic Account02 - Next, go to the API Settings page and create the API keys. Here click on the Add new API key button, enter a name, and click on the Generate new key button.

TextMagic Create API Key03 - You must install the TextMagic in Golang helper by running the following command.

1 go get

04 - You can use the following code example to send an SMS.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package main import (   "fmt"   textmagic "" ) func main() {   client := textmagic.NewClient("userName", "APIKey")   params := map[string]string{     "phones": "+44XXXXXXXXX",     "text" : "Hello SMS",   }   message, err := client.CreateMessage(params)   if err != nil {     fmt.Println(err)   } else {     fmt.Println(message.Id)   } }

05 - Finally, run the main.go file to send SMS using the following command.

1 go run main.go

03 - Using Multi-Channel Notification API - Courier

When considering requirements in the modern world, there are many scenarios where organizations need different approaches for communication, such as Email, Slack, WhatsApp, and even Twilio-like SMS services.

Courier is a multi-channel platform that allows seamless communication with various channels. With Courier, you can handle all such channels using only a single Golang API.

Pros of Courier

  • Provide excellent documentation, which allows you to get straight into your tasks.
  • Easy to understand the process, even for a person coming from a non-technical background.
  • The analytic window allows the user to track the activities during the messaging process (check if the message is received/opened).

Cons of Courier

  • It can be costly when operating with multiple notification channels.


01 - First, you need to sign-up for Courier and create a free account by following the descriptive guide and selecting SMS as the required service.

02 - Then connect with an SMS provider. Here, I will click on Twilio since I have already created an account with Twilo. Other than Twilio, there are many SMS providers which you can select.

SMS Notification Providers Golang

03 - Enter your account SID and the token available on Twilio Dashboard.

Twilio SID

04 - On the next screen, set the receiver's phone number.

Twilio SMS Test

05 - In the next screen, you can select the method or language needed in the service. You can also run the cURL command in the terminal to check your SMS gateway. I will choose Golang and continue to the next step.

Golang Send SMS Test06 - You must download the Golang package (SDK) using the following command.

1 go get -u

07 - Next, you can create the main.go file and paste the following code snippet generated in the Courier user interface.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package main import ( "context""log""" ) func main() { client := courier.CreateClient("<YOUR_AUTH_TOKEN>", nil) requestID, err := client.SendMessage( context.Background(), courier.SendMessageRequestBody { Message: map[string]interface{}{ "to": map[string]string{ "phone_number": "447548418181", }, "content": map[string]string{ "title": "Welcome to Courier!", "body": "Want to hear a joke? {{joke}}", }, "data": map[string]string{ "joke": "What did C++ say to C? You have no class.", }, }, }) if err != nil { log.Fatalln(err) } log.Println(requestID) }

08 - Finally, run the following command and check if you can receive the message.

1 go run main.go


This article proposed three ways to send SMS notifications via Golang and guided you in selecting the best SMS notification service for your Golang application. Also, we have discussed the pros and cons of using each method. We hope now you can select the most suitable approach for your future projects.

Thank you for reading.

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification









© 2023 Courier. All rights reserved.