In a previous post, we showed you how to send an SMS with Go. This post will show you how to receive SMS messages with your Vonage virtual number.
A publically accessible webhook is required and configured with your Vonage account to receive an inbound SMS. This tutorial will cover the process of setting up a publically accessible webhook, and the functionality to receive inbound SMS messages. You can find the code used in this tutorial on our Go Code Snippets Repository.
Vonage API Account
To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.
This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs. If you’ve just signed up, the initial cost of a number will be easily covered by your available credit.
Set up the Code
When Vonage receives an SMS to your virtual number, it checks whether you have configured a webhook to forward this SMS. This configuration could be either account-wide or specific to one virtual phone number.
If you have configured a webhook, Vonage will send a
GET request. So it's time to create the code that will handle this webhook request.
Create a file called
inbound-sms.go and enter the following code:
This code will listen for any request sent to the
/webhooks/inbound-sms endpoint and outputs the
text part of the body of the request.
Expose the Project To the Internet
For Vonage APIs to make requests to your webhook endpoint, it must be accessible publicly over the internet.
Ngrok is our go-to tool for running examples in development. As a result, ngrok is the service of choice in this tutorial. If ngrok isn’t installed, a great introduction to ngrok and how to install it can be found in this tutorial.
Launch Ngrok with the following command:
Make a note of the public URLs that ngrok creates for you. These will be similar to (but different from) the following:
This URL will be different every time you run the command if you’re using the free plan. So you will have to update the
inbound-sms URL in the dashboard each time you run the command.
Purchase a Vonage Number
If you haven’t already purchased a Vonage virtual phone number to follow this tutorial, please go ahead and buy one now. You can do this in the developer dashboard; however, there is another option, which is the Nexmo CLI. The CLI can assist in performing the account management tasks without having to leave your Terminal.
The command below searches for phone numbers capable of sending and receiving SMS, as well as making sure the number belongs to the country of your choosing. The two-character country code chooses the country; for example, for the USA, it would be
Choose one of the phone numbers from the list of phone numbers that are listed when you make command. Replace
VONAGE_VIRTUAL_NUMBER in the command below with the chosen phone number, and run the command.
Configure Your Vonage Account
Your new Vonage virtual number and your Webhook URL need to be linked together so that Vonage knows where to send the inbound SMS messages.
As with the previous step, you could add your webhook URL to your Vonage virtual number, but this tutorial will show the example on how to make this change via the CLI.
Before you run the command below, replace the following placeholders:
VONAGE_VIRTUAL_NUMBERwith your recently purchased Vonage number
WEBHOOK_URLwith your Ngrok URL, followed by
/webhooks/inbound-sms, so it should look something like:
Time to Test
Ngrok already running, in a new Terminal window, make sure you've navigated to the project directory containing your
receive-sms.go file. Run the following command:
Now, within another Terminal window, run the following command, replacing
VONAGE_VIRTUAL_NUMBER with the Vonage number you purchased.
If you check the Terminal window where you ran
go run ./, you should see a line like what you see below appear:
The webhook receives more fields than are displayed above. You can see a full list of these fields with some examples below. The example below is from the API docs under
You can find the code shown in this tutorial on the Go code snippets repository.
Below are a few other tutorials we’ve written either involving Go or receiving SMS messages:
- Go Explore the Vonage APIs with Vonage Go SDK
- Using JWT for Authentication in a Golang Application
- Receive an SMS with Python
Don’t forget, if you have any questions, advice or ideas you’d like to share with the community, then please feel free to jump on our Community Slack workspace, or pop a reply below 👇. I’d love to hear back from anyone that has implemented this tutorial and how your project works.