Kirim OTP WhatsApp di Node.js

Nambahin verifikasi WhatsApp ke backend Node.js cuma butuh dua panggilan REST: POST /v1/send buat kirim OTP dan POST /v1/verify buat cek kodenya. Contoh ini pakai fetch bawaan Node 18+, jadi nggak perlu install package apa pun.

Node.js 18+ · built-in fetch, 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.js
// Node.js 18+ ships a global fetch — no dependencies needed.
// The key comes from https://app.otpspace.com/dashboard/api-keys.
// Keep it on the server; never expose it in a browser or mobile client.
const API_KEY = process.env.OTPSPACE_API_KEY;
const BASE = "https://api.otpspace.com/v1";

// Send a WhatsApp OTP to an Indonesian number in E.164 form,
// e.g. "+6281234567890". Resolves once OTP Space returns 202 Accepted.
export async function sendOtp(phone) {
  const res = await fetch(BASE + "/send", {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ phone: phone }),
  });

  if (res.status !== 202) {
    const err = await res.json().catch(() => ({}));
    throw new Error(err.error || ("otp send failed: " + res.status));
  }
  return res.json(); // { success, data: { otp_id, ... }, credits_remaining }
}

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.js
// Verify the code the user typed. A 200 status means the code is correct;
// 422 (wrong code), 410 (expired) and 429 (too many attempts) mean it is not.
export async function verifyOtp(phone, code) {
  const res = await fetch(BASE + "/verify", {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ phone: phone, code: code }),
  });

  return res.status === 200; // 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

Go 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.