Skip to main content

Installation

Install the EasyOTP SDK using npm or your preferred package manager:

Quick Start

Initialization

Create a new EasyOTP client instance with your API key:

Configuration Options

string
required
Your EasyOTP API key. Get it from your dashboard.
string
Base URL for the API. Defaults to https://app.easyotp.dev/api/v1.
Never expose your API key in client-side code. Always use the SDK from your backend server.

Methods

send()

Send a verification code via SMS, Email, or Voice.

Parameters

string
required
Communication channel. Must be one of: sms, email, or voice
string
required
Recipient address:
  • For SMS/Voice: E.164 formatted phone number (e.g., +1234567890)
  • For Email: Valid email address (e.g., [email protected])
string
Custom message template. Use {code} as a placeholder for the verification code.Default: "Your verification code is: {code}"
string
Email subject line (only used when channel is email)Default: "Your Verification Code"
number
Code expiration time in seconds. Must be between 60 and 3600.Default: 300 (5 minutes)
string
Custom verification code. Must be a numeric string between 4-10 digits.If not provided, a code will be automatically generated.

Returns

boolean
Always true for successful requests
string
Unique identifier for this verification. Use this when calling verify().
string
ISO 8601 timestamp when the code expires
string
Unique request identifier for debugging

verify()

Verify a code that was previously sent.

Parameters

string
required
The verification ID returned from the send() method
string
required
The verification code to check. Must be a numeric string between 4-10 digits.

Returns

boolean
Always true for successful requests (even if the code is invalid)
boolean
true if the code was correct and not expired, false otherwise
string
Human-readable result message. Possible values:
  • "Code verified successfully"
  • "Invalid code"
  • "Code expired"
  • "Code already used"
string
Unique request identifier for debugging

Examples

Complete Verification Flow

Sending via Different Channels

Error Handling

The SDK throws errors for failed requests. Always wrap API calls in try-catch blocks:

Express.js Integration Example

Error Handling

The SDK throws errors for failed API requests. Error objects include:
  • message: Human-readable error message
  • status: HTTP status code
  • request_id: Unique request identifier for debugging
  • retry_after: Seconds to wait before retrying (for rate limit errors)

Common Error Codes

TypeScript Support

The SDK includes full TypeScript definitions. Import types as needed:

Best Practices

Store API keys securely: Use environment variables and never commit them to version control.
Handle errors gracefully: Always wrap SDK calls in try-catch blocks and provide meaningful error messages to users.
Use appropriate expiration times:
  • SMS/Voice: 2-5 minutes
  • Email: 10-15 minutes
Keep verification IDs server-side: Never expose verification IDs in URLs or client-side code. Store them server-side associated with user sessions.
Rate limiting: The SDK automatically handles rate limit responses. Check for retry_after in error responses to inform users when they can retry.

Additional Resources

API Reference

Complete REST API documentation

Quickstart Guide

Get started in 5 minutes

NPM Package

View on npm

Support

Get help from our team