ReOTP.
Developer Packages/ReOTP Library for Node.js
1 min read

ReOTP Library for Node.js

A ready-made library to facilitate using ReOTP in Node.js/Next.js applications.

Instead of writing HTTP requests manually, you can use our simple SDK code to integrate ReOTP in seconds.

This library is a ready-to-use Wrapper that you can copy directly into your project as a reotp.ts or reotp.js file and use it.

Features

  • Automatically send Init and handle errors.
  • Built-in Polling (continuous status check) function.
  • Strong Typescript types provided.
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) => {
      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);
            resolve(false);
          }
        } catch (e) {
          // Keep polling
        }
      }, 3000);
    });
  }
}