Sinch SMS’e Başlama
Sinch SMS API’sini Go (Golang) ile kullanmak için resmi Go SDK mevcut değil; doğrudan REST API’yi HTTP istekleri ile çağırın. Sinch Build Dashboard’a kaydolun, ücretsiz sanal numara ve API Token ile Service Plan ID alın. developers.sinch
Gerekli Adımlar
- Sinch Dashboard’da hesap oluşturun ve SMS sekmesinden API Token ile Service Plan ID’yi kopyalayın.
- Go projesinde
net/http,encoding/jsonvegithub.com/joho/godotenvgibi paketleri kullanın (ortam değişkenleri için). - Gönderici numaranızı (sanal numara) dashboard’dan alın.
Go ile SMS Gönderme Örneği
Aşağıdaki kod, SMS batch göndermek için temel bir HTTP POST isteği yapar. API Token’ı Bearer olarak ekleyin ve JSON payload ile https://eu.sms.api.sinch.com/xms/v1/{service_plan_id}/batches endpoint’ini hedefleyin (bölgeye göre eu veya us seçin). sinch
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type SMSRequest struct {
From string `json:"from"`
To []string `json:"to"`
Body string `json:"body"`
}
func main() {
servicePlanID := os.Getenv("SINCH_SERVICE_PLAN_ID")
apiToken := os.Getenv("SINCH_API_TOKEN")
from := os.Getenv("SINCH_FROM_NUMBER") // Örn: "+1234567890"
reqBody := SMSRequest{
From: from,
To: []string{"+905551234567"}, // Alıcı numaraları
Body: "Merhaba, Sinch SMS testi!",
}
jsonData, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST",
fmt.Sprintf("https://eu.sms.api.sinch.com/xms/v1/%s/batches", servicePlanID),
bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "Bearer "+apiToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println("Yanıt:", string(body)) // Batch ID içerir
}
.env dosyasına kimlik bilgilerini ekleyin ve godotenv.Load() ile yükleyin. Hata yönetimi ve retry logic ekleyin. Detaylı API referansı için Sinch docs’u inceleyin. sinch.redocly