Skip to content

Pranav77722/otp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

title emoji colorFrom colorTo sdk pinned
OTP Backend
πŸ”
blue
purple
docker
false

Flask OTP Backend

A Flask-based REST API that sends OTPs (One-Time Passwords) via email. The API accepts a mobile number, looks up the associated email from a database, generates a 6-digit OTP, and sends it via Resend API.

πŸš€ Features

  • Mobile number to email lookup
  • 6-digit OTP generation
  • Email delivery via Resend API
  • CORS enabled for frontend integration
  • Production-ready with Gunicorn
  • Deployable to Render.com

πŸ“‹ Prerequisites

  • Python 3.10+
  • Resend API account (free tier available)
  • Git and GitHub account
  • Render.com account (for deployment)

πŸ›  Local Setup

1. Clone the Repository

git clone https://github.com/Pranav77722/otp.git
cd otp

2. Install Dependencies

pip install -r requirements.txt

3. Get Resend API Key

  1. Sign up at resend.com
  2. Go to API Keys
  3. Click "Create API Key"
  4. Name: OTP Backend
  5. Permission: Full Access
  6. Copy the API key (starts with re_...)

4. Configure Environment Variables

Create a .env file:

cp .env.example .env

Edit .env and add your Resend API key:

RESEND_API_KEY=re_your_actual_api_key_here
SENDER_EMAIL=onboarding@resend.dev

5. Run Locally

python app.py

Server will start at http://localhost:5000

πŸ§ͺ Testing

Test the API using PowerShell:

$body = @{
    mobile = "9657329173"
} | ConvertTo-Json

Invoke-RestMethod -Uri "http://localhost:5000/send-otp" -Method Post -Body $body -ContentType "application/json"

Or using cURL:

curl -X POST http://localhost:5000/send-otp \
  -H "Content-Type: application/json" \
  -d '{"mobile":"9657329173"}'

Expected response:

{
  "success": true,
  "email": "ap8548328@gmail.com",
  "mobile": "9657329173",
  "message": "OTP sent"
}

🌐 Deploy to Render

Step 1: Push Code to GitHub

git add .
git commit -m "Deploy OTP backend"
git push origin main

Step 2: Create Render Web Service

  1. Go to Render Dashboard
  2. Click "New +" β†’ "Web Service"
  3. Connect your GitHub repository
  4. Configure:
    • Name: otp-backend
    • Region: Choose closest to you
    • Branch: main
    • Runtime: Python 3
    • Build Command: pip install -r requirements.txt
    • Start Command: gunicorn app:app

Step 3: Add Environment Variables

In Render Settings β†’ Environment:

  1. Click "Add Environment Variable"
  2. Add:
    • Key: RESEND_API_KEY
    • Value: Your Resend API key (from step 3 above)
  3. Click "Save Changes"

Render will automatically deploy your app!

Step 4: Test Production API

Your API will be available at: https://your-service-name.onrender.com

Test it:

$body = @{
    mobile = "9657329173"
} | ConvertTo-Json

Invoke-RestMethod -Uri "https://your-service-name.onrender.com/send-otp" -Method Post -Body $body -ContentType "application/json"

πŸ“‘ API Reference

Endpoint: POST /send-otp

Request:

{
  "mobile": "9657329173"
}

Success Response (200):

{
  "success": true,
  "email": "ap8548328@gmail.com",
  "mobile": "9657329173",
  "message": "OTP sent"
}

Error Responses:

400 - Invalid Request

{
  "success": false,
  "message": "Mobile number is required"
}

404 - Mobile Not Found

{
  "success": false,
  "message": "Mobile number not registered"
}

500 - Server Error

{
  "success": false,
  "message": "Failed to send email: ..."
}

πŸ“š Database

Current users in db.py:

Mobile Number Email
9657329173 ap8548328@gmail.com
9699883753 adi.akolkar12@gmail.com

To add more users, edit db.py:

USERS = {
    "9657329173": "ap8548328@gmail.com",
    "9699883753": "adi.akolkar12@gmail.com",
    "1234567890": "newuser@example.com"  # Add new entry
}

πŸ”§ Configuration

Sender Email (Optional)

By default, emails are sent from onboarding@resend.dev.

To use a custom sender:

  1. Own a domain (e.g., yourdomain.com)
  2. Verify it in Resend dashboard
  3. Add environment variable:
    SENDER_EMAIL=noreply@yourdomain.com
    

⚠️ Important Notes

  • Do NOT commit .env to GitHub (it's in .gitignore)
  • Free Resend tier: 3,000 emails/month, 100/day
  • Render free tier: May spin down after inactivity (30 sec startup)
  • For production, consider upgrading to paid tiers

πŸ“ Project Structure

otp/
β”œβ”€β”€ app.py              # Main Flask application
β”œβ”€β”€ db.py               # User database (mobile β†’ email)
β”œβ”€β”€ email_service.py    # Resend email integration
β”œβ”€β”€ otp_service.py      # OTP generation logic
β”œβ”€β”€ firebase_init.py    # Firebase Admin SDK setup (optional)
β”œβ”€β”€ requirements.txt    # Python dependencies
β”œβ”€β”€ Procfile           # Render/Heroku configuration
β”œβ”€β”€ Dockerfile         # Docker configuration
β”œβ”€β”€ runtime.txt        # Python version
β”œβ”€β”€ .env.example       # Environment template
└── README.md          # This file

🀝 Contributing

Pull requests are welcome! For major changes, please open an issue first.

πŸ“„ License

MIT License

πŸ†˜ Support

For issues or questions:


Made with ❀️ using Flask and Resend

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published