Codementor Events

How to Send SMS in Go Using Plivo's SMS API

Published Dec 16, 2021Last updated Dec 22, 2021
How to Send SMS in Go Using Plivo's SMS API

Your company has settled on Plivo to handle its voice and messaging communications, and now it’s your job to start integrating Plivo into your company’s applications. Don’t worry — Plivo has an SDK to help you out. Let’s see how to send and receive messages through Plivo in a Go application.

Install the Plivo SDK

We’ll presume you already have Go installed. To install the SDK, run go get github.com/plivo/plivo-go, or cloneour repository into your GOPATH.

Find your Auth ID and Auth Token

You have to have proper credentials before you can use the Plivo API. We provide an Auth ID and Auth Token in the Account section at the top of your Plivo Console.

Find Your Auth Credentials on Plivo Console

Choose a phone number

You need an SMS-enabled Plivo phone number to send messages to the US and Canada. Check the Numbers screen of your Plivo console to see what numbers you have available and which of them support SMS capabilities. You can also buy numbers from this screen.

Buy a New Plivo Number

SMS regulations followed by carriers vary from country to country. For messages to countries other than the US and Canada, you might want to register an alphanumeric sender ID for your messages. You can learn more about the use of alphanumeric sender ID and register one from your Plivo Console.

Send an SMS message

Now you’re ready to start. Create a file called SendSMS.go and paste in this code:

package main
import "github.com/plivo/plivo-go"
func main() {
    client, err: = plivo.NewClient("<auth_id>", "<auth_token>", & plivo.ClientOptions {}) if err != nil {
        panic(err)
    }
    client.Messages.Create(plivo.MessageCreateParams {
        Src: "plivo_src_number",
        Dst: "the_destination_number",
        Text: "Hello, world!",
    })
}

Note: If you’re using a Plivo trial account, you can send messages only to phone numbers that have been verified with Plivo. You can verify a phone number using the Sandbox Numbers page of the Console.

Save the file and use the below command to run it.

Receive an SMS message

Of course sending messages is only half of the equation. Plivo supports receiving SMS text messages in many countries (see our SMS API coverage page and click on the countries you’re interested in). When someone sends an SMS message to a Plivo phone number, you can receive it on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL.

Create another file — let’s call it receive_sms.go — and paste in this code.

package main
import ("net/http") func handler(w http.ResponseWriter, r * http.Request) 
{
    fromnumber: = r.FormValue("From") 
    tonumber: = r.FormValue("To") 
    text: = r.FormValue("Text") 
    print("Message Received - ", fromnumber, " ", tonumber, " ", text)
}
func main() {
    http.HandleFunc("/receive_sms/", handler) http.ListenAndServe(":8080", nil)
}

To run it, run the below command.

That’s fine for testing, but it’s not much good if you can’t connect to the internet to receive incoming messages and handle callbacks. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive messages:

Ngrok will display a forwarding link that you can use as a webhook to access your local server using the public network.

Sample ngrok CLI

Now you can create an application to receive SMS messages (follow our Quickstart guide for details).

Conclusion

And that’s all there is to sending and receiving SMS messages using Plivo’s Go SDK. Don’t use Go? Don’t worry — we have SDKs for Java, Python, PHP, Node.js, Ruby, .NET Core, and .NET Framework.

Haven’t tried Plivo yet? Getting started is easy and only takes 5 minutes! Sign up today.

Discover and read more posts from Plivo-Community
get started
post commentsBe the first to share your opinion
Show more replies