ReOTP.
SDKs para Desenvolvedores/Biblioteca ReOTP para Node.js
1 min read

Biblioteca ReOTP para Node.js

Biblioteca pronta para facilitar o uso do ReOTP em aplicações Node.js/Next.js.

Em vez de escrever requisições HTTP manualmente, você pode usar nosso simples código SDK para integrar o ReOTP em segundos.

Esta biblioteca é um Wrapper pronto que você pode copiar diretamente para seu projeto como um arquivo reotp.ts ou reotp.js e usá-lo.

Recursos

  • Envio automático do Init e tratamento de erros.
  • Função de Polling (verificação contínua de status) integrada.
  • Tipos Typescript robustos.
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);
    });
  }
}