Skip to main content
This guide walks you through setting up the E-Commerce API for local development.

Prerequisites

Before you begin, ensure you have the following installed:
  • Node.js (v14 or higher)
  • npm or yarn
  • MySQL (v8.0 or higher)
  • ngrok (optional, for webhook testing)

Installation

1

Clone the repository

Clone the project repository to your local machine:
git clone <repository-url>
cd e-comm-api
2

Install dependencies

Install all required npm packages:
npm install
This will install the following key dependencies:
  • express - Web framework
  • mysql2 - MySQL database driver
  • jsonwebtoken - JWT authentication
  • bcrypt - Password hashing
  • multer - File upload handling
  • express-rate-limit - Rate limiting
  • dotenv - Environment variable management
  • cors - Cross-origin resource sharing
  • swagger-ui-express - API documentation
3

Set up the database

Create a new MySQL database for the application:
CREATE DATABASE ecommerce_db;
You’ll need to create the required tables for users, products, categories, orders, cart, reviews, and addresses. Refer to your database schema documentation for the complete table structure.
4

Configure environment variables

Create a .env file in the root directory with your configuration:
cp .env.example .env
See the Configuration guide for detailed information about all available environment variables.
5

Start the development server

Run the application in development mode with auto-reload:
npm run dev
The server will start on the configured port (default: 5000):
Server running on http://localhost:5000
Connected to MySQL database.
API Docs available at http://localhost:5000/docs

Development scripts

The following npm scripts are available for development:
npm run dev

Script descriptions

  • npm run dev - Starts the server with nodemon for automatic reloading during development
  • npm start - Starts the server in production mode
  • npm run tunnel - Creates an ngrok tunnel to expose your local server to the internet

Setting up ngrok tunnel

For testing webhooks or allowing external access to your local server, you can use the ngrok tunnel:
npm run tunnel
This command runs:
ngrok http --url=loving-liger-previously.ngrok-free.app 5000
The ngrok URL in package.json is pre-configured. You may need to update it with your own ngrok domain if you have a paid ngrok account, or remove the --url parameter to use a random ngrok URL.

Verify installation

Once the server is running, you can verify the installation:
1

Check the health endpoint

Visit http://localhost:5000/docs to see the Swagger API documentation.
2

Test API authentication

All API requests require an API key. Include it in your request headers:
curl -H "x-api-key: your-api-key" http://localhost:5000/api/products

Project structure

After setup, your project structure should look like this:
e-comm-api/
├── config/          # Database and configuration files
├── controllers/     # Request handlers
├── middlewares/     # Custom middleware (auth, file upload, logging)
├── routes/          # API route definitions
├── docs/            # Swagger documentation
├── uploads/         # File upload directory
├── logs/            # Application logs
├── app.mjs          # Main application file
├── package.json     # Dependencies and scripts
└── .env             # Environment variables

Next steps

Configuration

Learn about environment variables and configuration options

Authentication

Understand how to authenticate API requests
The API includes built-in Swagger documentation. Once your server is running, visit /docs to explore all available endpoints interactively.

Build docs developers (and LLMs) love