This guide shows how to send SMS messages to your phone using Infobip API through the Go SDK.
For detailed information on the full capabilities of the Infobip SMS channel, check the API Reference.
Prerequisites
- Basic Go and API knowledge
- Go 13 (or newer) installation
- A running Go project
Steps overview
- Install the Infobip SDK.
- Import the required packages.
- Create an Infobip client.
- Setup your message.
- Send the message.
- Check response (optional).
A bonus section shows how to send multiple messages.
Install the Infobip SDK
The SDK can be retrieved as a normal Go library, using the go get
command as follows:
go get "github.com/infobip-community/infobip-api-go-sdk/v3"
Import the required packages
You’ll need the Infobip client and models imported into your source code:
import (
//..
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip"
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/models"
)
Create an Infobip client
To create the main client, you need to specify your API key, and custom base URL.
You can get them logging in to your Infobip account.
// Secrets configuration
baseURL := "your base URL here"
apiKey := "your api key here"
// Initialize Infobip client
client, _ := infobip.NewClient(baseURL, apiKey)
Setup your message
To send the message, you need to create a message and a request to send it.
msg := models.SMSMsg{
Destinations: []models.SMSDestination{
{To: "123456789012"},
},
From: "Cool Gopher",
Text: "Hello from Infobip Go SDK!",
}
req := models.SendSMSRequest{
Messages: []models.SMSMsg{msg},
}
Send the message
Send the message like so:
resp, respDetails, err := client.SMS.Send(context.Background(), req)
Check the response (optional)
You can print the response objects to verify whether the SMS has left the Infobip platform successfully. You should see a message ID and a 200 OK
response code:
fmt.Printf("Sent message: %+vn", resp.Messages)
fmt.Printf("HTTP response: %sn", respDetails.HTTPResponse.StatusCode)
You should see something like this:
Sent message: [{MessageID:35539968238603572226 Status:0xc000388640 To:523311800428}]
HTTP response: 200 OK
And that’s it!
Bonus: sending multiple messages
If you want to send multiple messages, you can replace the message and request setup to include several destinations and message instances.
As you may have noticed, the Destinations
field of a request, as well as the Messages
field of a message are slices.
Add as many elements as you want.
// Create SMS messages.
msg := models.SMSMsg{
Destinations: []models.SMSDestination{
{To: "123456789012"},
{To: "987654321098"},
},
From: "Cool Gopher",
Text: "Hello from Infobip Go SDK!",
}
msg2 := models.SMSMsg{
Destinations: []models.SMSDestination{
{To: "123456789012"},
{To: "987654321098"},
},
From: "Cool Gopher",
Text: "Hello 2 from Infobip Go SDK!",
}
// Create request to send multiple messages.
req := models.SendSMSRequest{
Messages: []models.SMSMsg{msg, msg2},
}
Other than that, everything else is the same as sending a single message.
Thanks for reading!