Kirim OTP WhatsApp di PHP

Verifikasi WhatsApp di PHP — polos atau Laravel — cuma butuh dua panggilan REST: POST /v1/send dan POST /v1/verify. Contoh cURL di bawah jalan tanpa dependency; ada juga versi HTTP client Laravel di bawahnya.

PHP 7.4+ · cURL extension (bundled) · Laravel snippet included

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.php
<?php
// Plain cURL — no Composer packages required (PHP 7.4+).
// The key comes from https://app.otpspace.com/dashboard/api-keys.
// Keep it server-side; never expose it in front-end code.
const OTPSPACE_KEY  = 'sk_live_xxx';
const OTPSPACE_BASE = 'https://api.otpspace.com/v1';

function otpspace_post(string $path, array $payload): array {
    $ch = curl_init(OTPSPACE_BASE . $path);
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . OTPSPACE_KEY,
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS     => json_encode($payload),
    ]);
    $body   = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return ['status' => $status, 'body' => json_decode($body, true)];
}

// Send a WhatsApp OTP to an Indonesian number (E.164, e.g. "+6281234567890").
// OTP Space returns 202 Accepted once the message is queued.
function send_otp(string $phone): bool {
    $res = otpspace_post('/send', ['phone' => $phone]);
    return $res['status'] === 202;
}

// Verify the code the user typed — 200 means it is correct.
function verify_otp(string $phone, string $code): bool {
    $res = otpspace_post('/verify', ['phone' => $phone, 'code' => $code]);
    return $res['status'] === 200;
}

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.

OtpService.php (Laravel)
<?php
// Laravel: use the built-in HTTP client (Illuminate\Support\Facades\Http).
// Put OTPSPACE_KEY in your .env and read it via config/services.php.
namespace App\Services;

use Illuminate\Support\Facades\Http;

class OtpService
{
    private string $base = 'https://api.otpspace.com/v1';

    private function client()
    {
        return Http::withToken(config('services.otpspace.key'))
            ->acceptJson();
    }

    // Send a WhatsApp OTP; true once OTP Space returns 202 Accepted.
    public function send(string $phone): bool
    {
        return $this->client()
            ->post($this->base . '/send', ['phone' => $phone])
            ->status() === 202;
    }

    // Verify the user's code; true on 200 OK.
    public function verify(string $phone, string $code): bool
    {
        return $this->client()
            ->post($this->base . '/verify', ['phone' => $phone, 'code' => $code])
            ->status() === 200;
    }
}

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

Go Node.js

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.