API v1Node.js · Python

DiOTP SDKs for Node.js and Python

Official client libraries for the DiOTP OTP API: npm install @diorbit/diotp for Node.js and pip install diotp for Python. Send a code and verify it in a few lines, with typed errors and safe retries.

Prefer no dependency? Every SDK is a thin layer over the REST API. The REST quickstart shows the same send and verify calls with any HTTP client.

Why use an SDK

The DiOTP API is small on purpose. To add phone verification you send a code with POST /otp/send and check what the user typed with POST /otp/verify. You can do both with any HTTP client, and the API documentation shows how in cURL, Node.js, Python and PHP. The SDKs wrap those calls so you write less code and handle errors the same way everywhere.

Each library does the same four things, with names that follow the habits of its language:

  • Send an OTP by SMS, or by WhatsApp once that channel is switched on for your account. The country code defaults to 91 and the channel to sms.
  • Verify the code the user typed. A successful call means the number is verified; anything else raises an error you can branch on.
  • Read the account and key a request uses (/me), which is a quick way to check a key before going live.
  • Read a month of usage (/usage): attempts, accepted messages, billable units and the amount.

Every send adds an Idempotency-Key header automatically, a random UUID unless you pass your own. If your server times out and retries with the same key, DiOTP returns the first response and does not text the user a second time. Every error response becomes one typed error carrying the stable error code, the HTTP status, a readable message and the request id to quote to support.

Both keep your dependency tree short. The Node.js client has no runtime dependencies and uses the fetch built into Node.js 18; the Python client has one, httpx. The source is small and easy to read or copy. Both are released under the MIT licence by Diorbit Technologies LLP.

The two libraries

LanguagePackageInstall
Node.jsNode.js 18 or later. Zero runtime dependencies; uses the built-in fetch. ESM, CommonJS and TypeScript types included.diotpnpm · GitHubnpm install @diorbit/diotp
PythonPython 3.9 or later. One dependency: httpx. Sync DiOTP and async AsyncDiOTP clients, fully typed.diotpPyPI · GitHubpip install diotp

Send and verify in each language

The examples below do the whole flow: send a code to a 10-digit Indian number, keep the request_id, then verify the code the user typed. Read the API key from an environment variable such as DIOTP_API_KEY. Never put it in a browser bundle or a mobile app.

Install from npm
npm install @diorbit/diotp

Node.js 18 or later. Source on GitHub, package on npm.

Node.js: send and verify
import { DiOTP } from '@diorbit/diotp'

const diotp = new DiOTP() // reads DIOTP_API_KEY

const { requestId } = await diotp.send({ to: '9876543210' }) // country code 91 by default

// Later, with the code your user typed:
const result = await diotp.verify({ requestId, code: '482913' })
console.log(result.verified) // true
Node.js: handling errors and rate limits
import { DiOTP, InvalidCodeError, ExpiredError, RateLimitError, DiOTPError } from '@diorbit/diotp'

const diotp = new DiOTP({ timeout: 10000, maxRetries: 2 })

export async function sendCode(phone) {
  try {
    const { requestId, expiresAt } = await diotp.send({ to: phone, purpose: 'login' })
    return { requestId, expiresAt }
  } catch (err) {
    if (err instanceof RateLimitError) return { error: err.code } // e.g. resend_cooldown
    throw err
  }
}

export async function checkCode(requestId, code) {
  try {
    await diotp.verify({ requestId, code })
    return { verified: true }
  } catch (err) {
    if (err instanceof InvalidCodeError) return { verified: false, error: 'wrong_code' }
    if (err instanceof ExpiredError) return { verified: false, error: 'expired' }
    if (err instanceof DiOTPError) return { verified: false, error: err.code }
    throw err
  }
}

Handling errors

Branch on the error code, not on the message. The codes you will meet most often are invalid_code (wrong code, the user can try again), expired (more than five minutes passed), too_many_attempts (five wrong tries) and the send limits such as resend_cooldown. A send stopped by a limit is neither sent nor charged. In test mode, sending to any number other than the one you verified at sign-up returns trial_restricted. Both SDKs have a typed error for every code on API error codes, which lists each one with its cause and fix. The codes are also in the OpenAPI reference, and the verify OTP page explains what to show the user for each.

Framework guides

Using another language or a framework? These guides build the same send and verify flow step by step, with the REST API or the SDKs above:

Start with the REST API

You do not need a library at all. Each SDK is a thin layer over the same REST API, so you can call the API directly using the samples in the docs, and the method names match the API fields. If you prefer a visual tool first, the Postman collection runs the same four calls with no code at all.

Ready to try it? Create an account to get an API key and free test messages to your own number, then check OTP pricing before you go live.

SDKs: FAQ

Install the package for your language: npm install @diorbit/diotp for Node.js, or pip install diotp for Python. Both are open source under the MIT licence, with their code on GitHub. For PHP, Java, Go, Ruby and other stacks, call the REST API directly; the framework guides on this page show how.