ReOTP.
ডেভেলপার প্যাকেজ/ReOTP SDK জাভা স্ক্রিপ্টের জন্য
1 মিনিট পড়ার সময়

ReOTP SDK জাভা স্ক্রিপ্টের জন্য

Node.js/Next.js অ্যাপ্লিকেশনে ReOTP ব্যবহার করতে সহজ SDK।

HTTP অনুরোধ ম্যানুয়ালি লিখতে না হয়ে আপনি আমাদের সহজ SDK কোড ব্যবহার করে ReOTP এক সেকেন্ডে একীকরণ করতে পারেন।

এই SDK একটি প্রস্তুত Wrapper যা আপনি সরাসরি আপনার প্রকল্পে reotp.ts বা reotp.js ফাইল হিসেবে কপি করতে পারেন এবং ব্যবহার করতে পারেন।

বৈশিষ্ট্য

  • স্বয়ংক্রিয়ভাবে Init পাঠানো ও ত্রুটি প্রকাশ।
  • SSE (Server-Sent Events) ফাংশন (স্টিম কন্টিনিউয়াস স্টেট চেক) একীভূত।
  • শক্তিশালী TypeScript টাইপ।
ইন্টিগ্রেশন কোড (SDKs)
export class ReOTPClient {
  private apiKey: string;
  private baseUrl: string;

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

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

  async waitForVerification(requestId: string, timeoutMs: number = 120000): Promise<{ verified: boolean; phoneNumber?: string }> {
    return new Promise((resolve, reject) => {
      const EventSource = require('eventsource');
      const evtSource = new EventSource(`${this.baseUrl}/api/verify/stream?id=${requestId}`);

      const timeout = setTimeout(() => {
        evtSource.close();
        resolve({ verified: false });
      }, timeoutMs);

      evtSource.onmessage = (event: any) => {
        const data = JSON.parse(event.data);
        if (data.status === "VERIFIED") {
          clearTimeout(timeout);
          evtSource.close();
          resolve({ verified: true, phoneNumber: data.phoneNumber });
        } else if (data.status === "EXPIRED") {
          clearTimeout(timeout);
          evtSource.close();
          reject(new Error("Session expired"));
        }
      };

      evtSource.onerror = () => {
        // Fallback or retry
      };
    });
  }
}