SDKs

SDKs

Client libraries and code examples for integrating SecureOTP across different languages and frameworks.

SDKs & Code Examples

SecureOTP exposes a simple REST API. No SDK is required — any HTTP client works. The examples below show a complete send-and-verify flow in each language.

JavaScript / Node.js

javascript
const SECUREOTP_KEY = process.env.SECUREOTP_KEY
const BASE_URL = 'https://otp.applicanity.com'

async function sendOtp(phone, channel = 'sms') {
  const res = await fetch(`${BASE_URL}/api/v1/otp/${channel}/send`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${SECUREOTP_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ phone }),
  })
  if (!res.ok) throw new Error((await res.json()).error)
  return res.json() // { message, expiresAt }
}

async function verifyOtp(phone, code, channel = 'sms') {
  const res = await fetch(`${BASE_URL}/api/v1/otp/${channel}/verify`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${SECUREOTP_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ phone, code }),
  })
  const data = await res.json()
  return data.success // boolean
}

// Usage
await sendOtp('+628123456789', 'sms')
const ok = await verifyOtp('+628123456789', userInput, 'sms')

Python

python
import os
import requests

SECUREOTP_KEY = os.environ['SECUREOTP_KEY']
BASE_URL = 'https://otp.applicanity.com'

HEADERS = {
    'Authorization': f'Bearer {SECUREOTP_KEY}',
    'Content-Type': 'application/json',
}

def send_otp(phone: str, channel: str = 'sms') -> dict:
    res = requests.post(
        f'{BASE_URL}/api/v1/otp/{channel}/send',
        json={'phone': phone},
        headers=HEADERS,
    )
    res.raise_for_status()
    return res.json()  # { 'message': ..., 'expiresAt': ... }

def verify_otp(phone: str, code: str, channel: str = 'sms') -> bool:
    res = requests.post(
        f'{BASE_URL}/api/v1/otp/{channel}/verify',
        json={'phone': phone, 'code': code},
        headers=HEADERS,
    )
    return res.json().get('success', False)

# Usage
send_otp('+628123456789', 'whatsapp')
ok = verify_otp('+628123456789', user_input, 'whatsapp')

PHP

php
<?php

$SECUREOTP_KEY = $_ENV['SECUREOTP_KEY'];
$BASE_URL = 'https://otp.applicanity.com';

function secureotp_request(string $path, array $body): array {
    global $SECUREOTP_KEY, $BASE_URL;
    $ch = curl_init($BASE_URL . $path);
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . $SECUREOTP_KEY,
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => json_encode($body),
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}

function send_otp(string $phone, string $channel = 'sms'): array {
    return secureotp_request("/api/v1/otp/{$channel}/send", ['phone' => $phone]);
}

function verify_otp(string $phone, string $code, string $channel = 'sms'): bool {
    $data = secureotp_request("/api/v1/otp/{$channel}/verify", [
        'phone' => $phone,
        'code'  => $code,
    ]);
    return $data['success'] ?? false;
}

// Usage
send_otp('+628123456789');
$ok = verify_otp('+628123456789', $_POST['code']);

Go

go
package secureotp

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
    "os"
)

var (
    apiKey  = os.Getenv("SECUREOTP_KEY")
    baseURL = "https://otp.applicanity.com"
)

func post(path string, body map[string]string) (map[string]interface{}, error) {
    b, _ := json.Marshal(body)
    req, _ := http.NewRequest("POST", baseURL+path, bytes.NewBuffer(b))
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    var result map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&result)
    return result, nil
}

func SendOTP(phone, channel string) error {
    _, err := post(fmt.Sprintf("/api/v1/otp/%s/send", channel), map[string]string{"phone": phone})
    return err
}

func VerifyOTP(phone, code, channel string) (bool, error) {
    data, err := post(fmt.Sprintf("/api/v1/otp/%s/verify", channel), map[string]string{
        "phone": phone,
        "code":  code,
    })
    if err != nil {
        return false, err
    }
    success, _ := data["success"].(bool)
    return success, nil
}

cURL (Shell)

bash
# Send OTP via SMS
curl -X POST https://otp.applicanity.com/api/v1/otp/sms/send \
  -H "Authorization: Bearer $SECUREOTP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"phone":"+628123456789"}'

# Verify OTP
curl -X POST https://otp.applicanity.com/api/v1/otp/sms/verify \
  -H "Authorization: Bearer $SECUREOTP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"phone":"+628123456789","code":"A3K9PZ"}'

Framework Integrations

Next.js API Route

javascript
// app/api/auth/send-otp/route.js
export async function POST(request) {
  const { phone } = await request.json()

  const res = await fetch(`${process.env.SECUREOTP_BASE}/api/v1/otp/sms/send`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.SECUREOTP_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ phone }),
  })

  const data = await res.json()
  return Response.json(data, { status: res.status })
}

Express.js Middleware

javascript
import express from 'express'

const router = express.Router()

router.post('/send', async (req, res) => {
  const { phone, channel = 'sms' } = req.body

  const upstream = await fetch(
    `${process.env.SECUREOTP_BASE}/api/v1/otp/${channel}/send`,
    {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${process.env.SECUREOTP_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ phone }),
    }
  )

  const data = await upstream.json()
  res.status(upstream.status).json(data)
})