Skip to main content

Installation

Install the EasyOTP Python SDK using pip:

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"
int
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

bool
Always True for successful requests
str
Unique identifier for this verification. Use this when calling verify().
str
ISO 8601 timestamp when the code expires
str
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

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

Examples

Complete Verification Flow

Sending via Different Channels

Error Handling

The SDK raises exceptions for failed requests. Always wrap API calls in try-except blocks:

Flask Integration Example

Django Integration Example

Error Handling

The SDK raises custom exceptions for different error types:

Exception Classes

  • EasyOTPError: Base exception for all SDK errors
  • AuthenticationError: Raised for 401 errors (invalid API key)
  • InsufficientCreditsError: Raised for 402 errors (insufficient credits)
  • ForbiddenError: Raised for 403 errors (API key disabled)
  • NotFoundError: Raised for 404 errors (verification not found)
  • RateLimitError: Raised for 429 errors (rate limit exceeded)
  • ServerError: Raised for 500 errors (internal server error)

Error Object Properties

All exceptions include:
  • message: Human-readable error message
  • status_code: HTTP status code
  • request_id: Unique request identifier for debugging
  • retry_after: Seconds to wait before retrying (for rate limit errors)

Common Error Codes

Type Hints

The SDK includes type hints for better IDE support:

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-except 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 exception objects to inform users when they can retry.
Use environment variables: Store your API key in environment variables for better security:

Additional Resources

PyPI Package

View on PyPI

API Reference

Complete REST API documentation

Quickstart Guide

Get started in 5 minutes

Support

Get help from our team