How to Send SMS With Go
Published on April 19, 2021

In this blog post, we'll show you how to use the Vonage Go SDK to send an SMS using Go in less than 20 lines of code.

Prerequisites

To follow along with this post you'll need to have Golang installed on your development machine. Installation instructions can be found on the official Golang website.

Alternatively, if you're new to Go, or you don't want to go through the installation process, you can work directly in the Golang Playground instead.

Using the Vonage Go SDK

Fire up your editor and create a new file called main.go. Then scaffold the basics of a Go application by typing (or copying) the following code:

package main

import (
	"fmt"
	"github.com/vonage/vonage-go-sdk"
)

func main() {

}

Note: If you save main.go and the files in the import statement disappear, don't worry, they'll come back once you use them inside the main() function.

Now it's time to put some meat on those bones and instantiate the Vonage Go SDK so you can actually make it do things.

Inside the main() function add the following:

auth := vonage.CreateAuthFromKeySecret(API_KEY, API_SECRET)
smsClient := vonage.NewSMSClient(auth)
response, _ := smsClient.Send("44777000000", "44777000777", "Hi from golang", vonage.SMSOpts{})

There are two things happening here.

First, you create an auth object that combines your API key and secret together using a helper function that will ensure everything is formatted correctly.

Note: Your API key and secret can be found by logging into your Vonage Dashboard. If you don't have an account yet, you can sign up here and get a free starter credit to run this code!

Second, you instantiate a new smsClient that will hold all the functionality the Vonage Go SDK provides. Your auth object is passed into this.

With this in place, you can now perform actions on the Vonage API, such as sending an SMS.

Send SMS Messages With Go

With the Vonage API client ready to go, your code will now look like this:

package main

import (
	"fmt"
	"github.com/vonage/vonage-go-sdk"
)

func main() {
    auth := vonage.CreateAuthFromKeySecret(API_KEY, API_SECRET)
    smsClient := vonage.NewSMSClient(auth)
    response, _ := smsClient.Send("44777000000", "44777000777", "Hi from golang", vonage.SMSOpts{})
}

In order to send an SMS with Go you need to pass smsClient all of the information the SMS needs to make it to its destination.

As a minimum you should include the number the SMS should be sent to, the number it is sent from and the text to be displayed.

The To number can be your own number but the From number must be an SMS capable number purchased via your Vonage Dashboard.

Now the only thing left to do is to tell our app to send the SMS. This is done using the Send method provided by the API client.

The heavy lifting of sending the SMS is all done in a single line:

response, _ := smsClient.Send("44777000000", "44777000777", "Hi from golang", vonage.SMSOpts{})

Finally, add a quick bit of error checking and response output:

if response.Messages[0].Status == "0" {
    fmt.Println("Message sent")
}

Your final `main.go file should look like this:

package main

import (
	"fmt"
	"github.com/vonage/vonage-go-sdk"
)

func main() {
	auth := vonage.CreateAuthFromKeySecret(API_KEY, API_SECRET)
	smsClient := vonage.NewSMSClient(auth)
	response, _ := smsClient.Send("44777000000", "44777000777", "Hi from golang", vonage.SMSOpts{})

	if response.Messages[0].Status == "0" {
		fmt.Println("Message sent")
	}
}

Now the stage is set to send that SMS! Head to your terminal and from inside the folder you're working in run:

go run main.go

If everything worked you'll see Message Sent returned to the screen just before the familiar sound of your SMS notification rings out signaling your success.

Where to Go From Here?

The next change you can make to the code above is to make it a little more secure by removing the hardcoded API key, API secret, and the phone numbers.

A good way to do this is to move them to environment variables that are stored in a .env file.

Try implementing this using the godotenv package and quickly shore up your security.

Further Reading

If sending an SMS with Go has go you excited about what other communication elements you could be adding to your application then take a look at the examples on the Vonage Go SDK GitHub repository.

There you'll find code for using many other aspects of the Vonage APIs such as making phone calls, receiving SMS messages, and verifying phone numbers.

As ever, we're keen to hear from you. If you have questions about using Go with the Vonage APIs consider joining our Vonage Community on Slack and asking your questions there.

Contributions to the SDK are also welcome, so if you're keen on helping us expand the scope of it further, feel free to raise a pull request on GitHub.

Martyn DaviesVonage Alumni

Former Director of Developer Education at Vonage. With a background as a creative developer, product manager, and hack day organizer, Martyn has been working as a technology advocate since 2012 having previously spent time in broadcasting and major record companies. Educating and empowering developers all over the world.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.