How to Send and Receive SMS Messages Using Go and Plivo’s Messaging API

How to Send and Receive SMS Messages Using Go and Plivo’s Messaging 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 a Go SDK to help you out. You can use it write Go applications that send and receive SMS messages.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Go development environment.

Send an SMS message

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import "github.com/plivo/plivo-go/v7"

func main()  {
  client, err := plivo.NewClient("<auth_id>","<auth_token>", &plivo.ClientOptions{})
  if err != nil {
    panic(err)
  }
  client.Messages.Create(plivo.MessageCreateParams{
    Src: "<sender_id>",
    Dst: "<destination_number>",
    Text: "Hello, world!",
  })
}

Replace the auth placeholders with actual values from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234). In countries other than the US and Canada you can use a sender ID for the message source. You must have a Plivo phone number to send messages to the US or Canada; you can rent a Plivo number from Phone Numbers >Buy Numbers on the Plivo console or via the Numbers API. Save the file and run it.

$ go run SendSMS.go

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 (sandbox) a number by going to the console’s Phone Numbers > Sandbox Numbers page.

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 application. Plivo will send the message along with other parameters to your Message URL.

Create a file called receive_sms.go (or whatever name you like) and paste into it this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)
}

Save the file and run it.

$ go run receive_sms.go

You should then be able to see your basic server app in action on http://localhost:8080/receive_sms/.

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 http [portnum]

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 five minutes. Sign up today.

Get Volume Pricing

Thousands of businesses in more than 220 countries trust Plivo’s cloud communications platform

The best communications platform forthe world’s leading entertainment service

Frequently asked questions

No items found.
footer bg

Subscribe to Our Newsletter

Get monthly product and feature updates, the latest industry news, and more!

Thank you icon
Thank you!
Thank you for subscribing
Oops! Something went wrong while submitting the form.

POSTS YOU MIGHT LIKE