Skip to content

Repository files navigation

ForexKing Backend API (Serverless)

πŸš€ Production-ready serverless API for Vercel deployment

Features

βœ… Serverless Architecture - Optimized for Vercel deployment
βœ… User Registration with OTP verification
βœ… Email Service via Gmail SMTP
βœ… Secure Authentication with bcrypt & JWT
βœ… Contact Form with email notifications
βœ… Rate Limiting & security features
βœ… MongoDB Atlas cloud database
βœ… Auto-scaling & high availability


πŸš€ Deploy to Vercel

GitHub Auto-Deploy

  1. Push code to GitHub
  2. Connect repository to Vercel
  3. Configure environment variables
  4. Deploy automatically

πŸ’» Local Development

# 1. Install dependencies
npm install

# 2. Configure environment variables
# Create .env file with required variables

# 3. Start development server (Vercel Dev)
npm run dev

# Server will run on: http://localhost:3000

πŸ“Š API Endpoints

Base URL: https://apis.forexking.info

Health Check

Get API Status

GET /health

Response:

{
  "success": true,
  "message": "ForexKing Backend API is running",
  "timestamp": "2025-10-27T12:00:00.000Z",
  "environment": "production"
}

Authentication

1. Register New User

POST /api/register
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "Strong@Pass123",
  "phone_number": "+201234567890"
}

Response:

{
  "success": true,
  "message": "Registration successful. Please check your email for OTP verification code.",
  "email": "john@example.com",
  "name": "John Doe"
}

2. Verify OTP

POST /api/verify-otp
Content-Type: application/json

{
  "email": "john@example.com",
  "otp": "123456"
}

Response:

{
  "id": "507f1f7***********",
  "name": "John Doe",
  "email": "john@example.com",
  "accessToken": "eyJhbGciOiJ*******..."
}

3. Login

POST /api/login
Content-Type: application/json

{
  "email": "john@example.com",
  "password": "Strong@Pass123"
}

Response:

{
  "id": "507f1f77b********",
  "name": "John Doe",
  "email": "john@example.com",
  "accessToken": "eyJhbGciOiJIUzI************..."
}

4. Resend OTP

POST /api/send-otp
Content-Type: application/json

{
  "email": "john@example.com"
}

Response:

{
  "success": true,
  "message": "OTP resent successfully. Please check your email."
}

Contact Form

Submit Contact Message

POST /api/contact
Content-Type: application/json

{
  "name": "Jane Smith",
  "email": "jane@example.com",
  "message": "I would like to know more about your services."
}

Response:

{
  "success": true,
  "message": "Message sent successfully. We will get back to you soon."
}

Validation Rules

Password Requirements

  • Minimum 8 characters
  • At least one uppercase letter (A-Z)
  • At least one lowercase letter (a-z)
  • At least one number (0-9)
  • At least one special character (!@#$%^&*)

Email

  • Valid email format
  • Unique (not already registered)

Phone Number

  • E.164 international format
  • Must start with +
  • Unique (not already registered)

Name

  • 2-100 characters
  • Required

πŸ“§ Email Templates

1. OTP Verification Email

Sent immediately after registration with 6-digit OTP code.
Expiry: 15 minutes

2. Welcome Email

Sent after successful account verification.

3. Contact Form Notification

Sent to company email when someone submits contact form.


βš™οΈ Environment Variables

Required environment variables must be configured in Vercel Dashboard:

Variable Description
MONGODB_URI MongoDB Atlas connection string
JWT_SECRET Secret for JWT signing
JWT_EXPIRE Token expiry duration
EMAIL_USER Gmail email address
EMAIL_PASS Gmail App Password
COMPANY_EMAIL Email to receive contact forms
NODE_ENV Environment mode

πŸ”’ Security Features

  • πŸ” bcrypt password hashing (10 rounds)
  • 🎫 JWT token authentication
  • πŸ›‘οΈ Security headers (CORS, Content-Type)
  • 🚦 Rate limiting per IP address
    • Register: 10 req / 15 min
    • Login: 10 req / 15 min
    • Contact: 5 req / 15 min
    • OTP Resend: 5 req / 15 min
  • βœ… Input validation with custom validators
  • ⏰ OTP expiration (15 minutes)
  • 🌐 CORS enabled for all origins

πŸ“ Project Structure (Serverless)

backend/
β”œβ”€β”€ api/                      # Serverless functions
β”‚   β”œβ”€β”€ _lib/                # Shared utilities
β”‚   β”‚   β”œβ”€β”€ serverless-helpers.js  # DB, CORS, validation
β”‚   β”‚   └── email-service.js       # Email functionality
β”‚   β”œβ”€β”€ register.js          # POST /api/register
β”‚   β”œβ”€β”€ login.js             # POST /api/login
β”‚   β”œβ”€β”€ verify-otp.js        # POST /api/verify-otp
β”‚   β”œβ”€β”€ send-otp.js          # POST /api/send-otp (resend)
β”‚   β”œβ”€β”€ contact.js           # POST /api/contact
β”‚   β”œβ”€β”€ health.js            # GET /health
β”‚   └── index.js             # GET / (welcome)
β”œβ”€β”€ models/
β”‚   └── User.js              # User schema & methods
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ generateToken.js     # JWT utilities
β”‚   └── generateOTP.js       # OTP generation
β”œβ”€β”€ templates/
β”‚   β”œβ”€β”€ otpEmail.html        # OTP email template
β”‚   β”œβ”€β”€ welcomeEmail.html    # Welcome email template
β”‚   └── contactNotification.html # Contact notification
β”œβ”€β”€ vercel.json              # Vercel configuration
β”œβ”€β”€ package.json             # Dependencies
β”œβ”€β”€ env.example              # Environment variables template
β”œβ”€β”€ VERCEL_DEPLOYMENT.md     # Deployment guide
└── README.md                # This file

Legacy Files (kept for backward compatibility):

  • server.js, routes/, controllers/, config/ - Traditional Express setup

Dependencies

{
  "express": "^4.18.2",
  "mongoose": "^8.0.0",
  "bcryptjs": "^2.4.3",
  "jsonwebtoken": "^9.0.2",
  "nodemailer": "^6.9.7",
  "dotenv": "^16.3.1",
  "cors": "^2.8.5",
  "express-validator": "^7.0.1",
  "helmet": "^7.1.0",
  "express-rate-limit": "^7.1.5"
}

πŸ“– Documentation

For detailed deployment instructions and setup guides, contact the development team.


Error Handling

All errors return consistent JSON format:

{
  "success": false,
  "message": "Error description",
  "errors": ["Detailed errors if validation"]
}

Common HTTP Status Codes:

  • 200 - Success
  • 201 - Created (registration)
  • 400 - Bad Request (validation error)
  • 401 - Unauthorized (invalid credentials)
  • 403 - Forbidden (account not verified)
  • 404 - Not Found
  • 429 - Too Many Requests (rate limit)
  • 500 - Server Error

Testing

Use tools like:

  • Postman - API testing GUI
  • cURL - Command line
  • Thunder Client - VS Code extension

Example test flow:

  1. Register user β†’ Receive OTP email
  2. Verify OTP β†’ Get access token
  3. Login with credentials β†’ Get access token
  4. Use token for protected routes

πŸ› οΈ Development

# Install dependencies
npm install

# Run with Vercel dev server (recommended)
npm run dev

# Deploy to preview environment
npm run deploy:preview

# Deploy to production
npm run deploy

🌐 Production Deployment

Manual Deployment:

npm run deploy

License

Mustafagaberr Β© 2025

About

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages