Kirim OTP WhatsApp di Go

Butuh verifikasi nomor WhatsApp di aplikasi Go? Dua panggilan REST — POST /v1/send untuk kirim OTP, POST /v1/verify untuk cek kodenya. Contoh di bawah cuma pakai net/http dari standard library, jadi tinggal tempel dan jalan.

Go 1.21+ · standard library only, no dependencies

Gratis 10 OTP saat daftar — tanpa kartu kredit.

1. Kirim OTP

Bikin API key, lalu POST nomor tujuan ke /v1/send. OTP Space balas 202 Accepted begitu pesan masuk antrean pengiriman WhatsApp.

otp.go
package otp

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

// apiKey comes from https://app.otpspace.com/dashboard/api-keys.
// Keep it server-side — never ship it in a mobile or browser client.
const apiKey = "sk_live_xxx"

const baseURL = "https://api.otpspace.com/v1"

// SendOTP sends a WhatsApp OTP to an Indonesian number (E.164, e.g.
// "+6281234567890"). OTP Space replies 202 Accepted once it is queued.
func SendOTP(phone string) error {
	payload, _ := json.Marshal(map[string]string{"phone": phone})

	req, _ := http.NewRequest(http.MethodPost, baseURL+"/send", bytes.NewReader(payload))
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	if res.StatusCode != http.StatusAccepted { // 202
		return fmt.Errorf("otp send failed: %s", res.Status)
	}
	return nil
}

2. Verifikasi kode

POST nomor yang sama plus kode yang diinput user ke /v1/verify. Status 200 OK berarti kode benar; tiap nomor dapat maksimal 5 percobaan sebelum OTP dikunci.

verify.go
// VerifyOTP checks the code the user typed against the OTP that was sent to
// their number. A 200 OK means the code is correct; anything else (422 wrong
// code, 410 expired, 429 too many attempts) means it is not.
func VerifyOTP(phone, code string) (bool, error) {
	payload, _ := json.Marshal(map[string]string{"phone": phone, "code": code})

	req, _ := http.NewRequest(http.MethodPost, baseURL+"/verify", bytes.NewReader(payload))
	req.Header.Set("Authorization", "Bearer "+apiKey)
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return false, err
	}
	defer res.Body.Close()

	return res.StatusCode == http.StatusOK, nil // 200 = verified
}

Respons dari API

Kirim yang sukses langsung membalas dengan tujuan yang dimask, otp_id untuk tracking, dan sisa kredit kamu.

202 Accepted
{
  "success": true,
  "data": {
    "otp_id": "otp_abc123def456",
    "destination": "6281234***90",
    "channel": "whatsapp",
    "status": "queued",
    "expires_at": "2026-07-17T10:05:00Z"
  },
  "credits_remaining": 994
}

Dibangun untuk aplikasi Indonesia

OTP Space kirim OTP via WhatsApp ke semua operator Indonesia lewat dua panggilan REST — tanpa setup WhatsApp Business API, tanpa approval template, tanpa SDK. Cara pakainya sama dari bahasa apa pun: daftar, bikin key, kirim. Contoh di sini cuma pakai standard library, jadi bisa langsung ditempel ke service yang sudah ada.

Bahasa lain

Node.js PHP

Kirim OTP WhatsApp pertamamu hari ini

Daftar gratis, bikin API key, dan integrasi cuma dua panggilan REST — tanpa kartu kredit, tanpa sales call, tanpa verifikasi WhatsApp Business.