Python SDK for EasyOTP - Send and verify OTP codes via SMS, Email, and Voice
Project description
EasyOTP Python SDK
Python SDK for EasyOTP - Send and verify OTP codes via SMS, Email, and Voice.
Installation
pip install easyotp
Or install from source:
pip install .
Quick Start
from easyotp import EasyOTP
client = EasyOTP(api_key="your_api_key_here")
response = client.send(
channel="sms",
recipient="+1234567890",
message="Your verification code is: {code}",
expires_in=300
)
print(f"Verification ID: {response.verification_id}")
print(f"Expires at: {response.expires_at}")
verify_response = client.verify(
verification_id=response.verification_id,
code="123456"
)
if verify_response.verified:
print("Code verified successfully!")
else:
print(f"Verification failed: {verify_response.message}")
Usage
Initialization
from easyotp import EasyOTP
client = EasyOTP(
api_key="your_api_key_here",
base_url="https://app.easyotp.dev/api/v1", # Optional, defaults to production
timeout=30 # Optional, request timeout in seconds
)
Sending Verification Codes
SMS
response = client.send(
channel="sms",
recipient="+1234567890",
message="Your verification code is: {code}",
expires_in=300
)
response = client.send(
channel="email",
recipient="user@example.com",
subject="Your Verification Code",
message="Your verification code is: {code}",
expires_in=600
)
Voice
response = client.send(
channel="voice",
recipient="+1234567890",
message="Your verification code is: {code}",
expires_in=300
)
Verifying Codes
verify_response = client.verify(
verification_id="11f951d5-32d1-4b49-bdda-7da248e2615c",
code="123456"
)
if verify_response.verified:
print("Success!")
else:
print(f"Failed: {verify_response.message}")
Response Objects
SendResponse
response = client.send(channel="sms", recipient="+1234567890")
print(response.success) # True
print(response.verification_id) # "11f951d5-32d1-4b49-bdda-7da248e2615c"
print(response.expires_at) # "2024-01-01T12:05:00.000Z"
print(response.expires_at_datetime) # datetime object
print(response.request_id) # "7b4d6022-7260-4568-b6b7-29c366c47bbc"
VerifyResponse
verify_response = client.verify(verification_id="...", code="123456")
print(verify_response.success) # True
print(verify_response.verified) # True or False
print(verify_response.message) # "Code verified successfully" or error message
print(verify_response.request_id) # "7b4d6022-7260-4568-b6b7-29c366c47bbc"
Error Handling
The SDK provides specific exception classes for different error scenarios:
from easyotp import (
EasyOTP,
AuthenticationError,
InsufficientCreditsError,
APIKeyDisabledError,
RateLimitError,
ValidationError,
NotFoundError,
ServerError,
)
client = EasyOTP(api_key="your_api_key")
try:
response = client.send(channel="sms", recipient="+1234567890")
except AuthenticationError as e:
print(f"Auth failed: {e.message} (request_id: {e.request_id})")
except InsufficientCreditsError as e:
print(f"No credits: {e.message}")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds")
except ValidationError as e:
print(f"Invalid request: {e.message}")
except NotFoundError as e:
print(f"Not found: {e.message}")
except ServerError as e:
print(f"Server error: {e.message}")
except EasyOTPError as e:
print(f"Error: {e.message}")
Complete Example
from easyotp import EasyOTP, EasyOTPError
client = EasyOTP(api_key="your_api_key")
try:
send_response = client.send(
channel="sms",
recipient="+1234567890",
message="Your Acme Corp verification code is: {code}",
expires_in=300
)
print(f"Code sent! Verification ID: {send_response.verification_id}")
user_code = input("Enter verification code: ")
verify_response = client.verify(
verification_id=send_response.verification_id,
code=user_code
)
if verify_response.verified:
print("Verification successful!")
else:
print(f"Verification failed: {verify_response.message}")
except EasyOTPError as e:
print(f"Error: {e}")
API Reference
EasyOTP Class
__init__(api_key: str, base_url: str = None, timeout: int = 30)
Initialize the EasyOTP client.
api_key: Your EasyOTP API keybase_url: Base URL for the API (defaults to production)timeout: Request timeout in seconds (default: 30)
send(channel: str, recipient: str, message: str = None, subject: str = None, expires_in: int = None, code: str = None) -> SendResponse
Send a verification code.
channel: Communication channel ("sms","email", or"voice")recipient: Recipient address (phone number for SMS/voice, email for email)message: Custom message template with{code}placeholder (optional)subject: Email subject line (email channel only, optional)expires_in: Code expiration time in seconds, 60-3600 (optional, default: 300)code: Custom verification code, 4-10 digits (optional, auto-generated if not provided)
Returns: SendResponse object
verify(verification_id: str, code: str) -> VerifyResponse
Verify a verification code.
verification_id: Verification ID from the send responsecode: Verification code to check
Returns: VerifyResponse object
License
MIT
Support
For support, email support@easyotp.dev or visit https://docs.easyotp.dev
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file easyotp-1.0.0.tar.gz.
File metadata
- Download URL: easyotp-1.0.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daaed93cd26726687b620a6ec97a404396d916f00690eef0c11bf365591909ad
|
|
| MD5 |
9adb173a6b038f11cb23072358f48a14
|
|
| BLAKE2b-256 |
3a026e3ecbdd936eee239290a9af2aabff3e6b543c3b5077b4f76eb039618663
|
File details
Details for the file easyotp-1.0.0-py3-none-any.whl.
File metadata
- Download URL: easyotp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a1184fa5e4b3b644cc80b8f347ac1574ed964bf995101a8530a7359488b6141
|
|
| MD5 |
0fc58effe23b05978eccf2495636aaec
|
|
| BLAKE2b-256 |
171118eeecf2684f01d804eacd89be6a2b912b4db89679b832c0ac904d977532
|