Skip to content

Instantly share code, notes, and snippets.

@leogtzr
Created October 2, 2020 04:14
Show Gist options
  • Save leogtzr/cb8e1dd1c826d9b507e9f560220b4cb5 to your computer and use it in GitHub Desktop.
Save leogtzr/cb8e1dd1c826d9b507e9f560220b4cb5 to your computer and use it in GitHub Desktop.
How to send SMS in Golang
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
// SMSRequestBody ...
type SMSRequestBody struct {
From string `json:"from"`
Text string `json:"text"`
To string `json:"to"`
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
}
func main() {
body := SMSRequestBody{
APIKey: os.Getenv("NEXMO_API_KEY"),
APISecret: os.Getenv("NEXMP_API_SECRET"),
To: "PHONE_NUMBER",
From: "...",
Text: "Hello! how are you?",
}
smsBody, err := json.Marshal(body)
if err != nil {
panic(err)
}
resp, err := http.Post("https://rest.nexmo.com/sms/json", "application/json", bytes.NewBuffer(smsBody))
if err != nil {
panic(err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(respBody))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment