This tutorial will cover setting up a publicly accessible webhook and the functionality to receive SMS delivery receipts. 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 sends an SMS from your account, it checks whether you have configured a webhook to forward any delivery receipts to. This configuration is an account-wide setting.
If you have configured a webhook, Vonage will send a
POST request to this webhook, so it's time to create the code to handle this webhook request.
Create a file called
delivery-receipt.go and enter the following code:
This code will listen for any request sent to the
/webhooks/delivery-receipt endpoint and outputs the
status part of the body of the request.
Expose the Project To the Internet
Your webhook endpoint needs to be accessible publicly over the internet for Vonage APIs to make requests to it.
Ngrok is our suggested tool used to run examples in development and is used in this tutorial to expose the webhook endpoint. If you haven’t got ngrok installed, you can find a great introduction to this service and how to install it in this tutorial.
Launch ngrok with the command below:
Make a note of the public URLs that ngrok creates for you; these will be similar to the example below:
Note: 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
delivery-receipt URL in the dashboard each time you run the command.
Configure Your Vonage Account
Your Vonage account needs configuring to know where to make the delivery receipt requests. You can add the delivery receipt webhook URL in your Vonage settings page, under the Delivery receipts label. The image below shows an example of this:
Time to Test
Ngrok already running, in a new Terminal window, make sure you've navigated to the project directory containing your
delivery-receipt.go file. Run the following command:
Now, within another Terminal window, run the following command, replacing
YOUR_NUMBER with your phone number to receive the test SMS message.
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 involving Go:
- Go Explore the Vonage APIs with Vonage Go SDK
- Using JWT for Authentication in a Golang Application
- Receive an SMS with Go
Don’t forget that if you have any questions, advice, or ideas you’d like to share with the community, please feel free to jump on our Community Slack workspace. I’d love to hear back from anyone that has implemented this tutorial and how your project works.