ReOTP.
SDK cho nhà phát triển/Thư viện ReOTP cho Node.js
1 min read

Thư viện ReOTP cho Node.js

Thư viện sẵn sàng để dễ dàng sử dụng ReOTP trong ứng dụng Node.js/Next.js.

Thay vì viết HTTP request thủ công, dùng SDK đơn giản để tích hợp ReOTP trong giây.

Đây là Wrapper sẵn sàng copy vào file reotp.ts hoặc reotp.js dùng ngay.

Tính năng

  • Gửi Init tự động và xử lý lỗi.
  • Hàm Polling (kiểm tra trạng thái liên tục) built-in.
  • TypeScript types mạnh.
Integration Code (SDKs)
export class ReOTPClient {
  private apiKey: string;
  private baseUrl: string;

  constructor(apiKey: string, baseUrl: string = "https://your-domain.com") {
    this.apiKey = apiKey;
    this.baseUrl = baseUrl;
  }

  async verify(phoneNumber: string, method: "WHATSAPP" | "TELEGRAM" = "WHATSAPP") {
    const res = await fetch(`${this.baseUrl}/api/verify/init`, {
      method: "POST",
      headers: { 
        "Content-Type": "application/json",
        "Authorization": `Bearer ${this.apiKey}`
      },
      body: JSON.stringify({ phoneNumber, method })
    });
    
    if (!res.ok) throw new Error(await res.text());
    return await res.json(); // { requestId, waLink, tgLink, expectedText }
  }

  async waitForVerification(requestId: string, timeoutMs: number = 120000): Promise<boolean> {
    const startTime = Date.now();
    
    return new Promise((resolve, reject) => {
      const interval = setInterval(async () => {
        if (Date.now() - startTime > timeoutMs) {
          clearInterval(interval);
          resolve(false);
        }

        try {
          const res = await fetch(`${this.baseUrl}/api/verify/status?id=${requestId}`);
          const data = await res.json();
          
          if (data.status === "VERIFIED") {
            clearInterval(interval);
            resolve(true);
          } else if (data.status === "EXPIRED") {
            clearInterval(interval);
            reject(new Error("Session expired"));
          } else if (data.status === "REJECTED") {
            clearInterval(interval);
            reject(new Error(data.failureReason || "Session rejected"));
          }
        } catch (e) {
          // Keep polling
        }
      }, 3000);
    });
  }
}