Skip to content

Repository files navigation

E-Commerce API

An E-Commerce API built using Node JS, Express JS, Firebase & Stripe.

Back-End : Node JS, Express JS

Database : Firebase (Firebase Emulator)

API Testing : Postman

Payment Gateway : Stripe

How To Use

Terminal Commands :-

Express JS Server (Nodemon) :

npm run dev

Firebase Emulator :

npx firebase emulators:start --project demo-e-commerce-api

Stripe CLI :

npx stripe listen --forward-to localhost:3000/api/webhooks/stripe --events checkout.session.completed

Ports :-

Firebase Emulator :

http://127.0.0.1:4000

Express JS Server :

http://localhost:3000

API Testing :- Use Postman For API Testing.

Payment Gateway Workflow :-

This API uses Stripe Checkout to securely process payments. It uses a two-step flow :

  1. Order Creation : When a POST request is made to /api/orders, the backend securely calculates the total price from the database and creates a Stripe Checkout Session. It returns a checkout_url in API response in Postman and saves the order in Firestore as Pending.
  2. Webhook Automation : When the user opens that checkout_url in the browser and completes the payment, Stripe sends a Webhook event (checkout.session.completed) to the backend. The server verifies the signature and automatically updates the order's payment_status to Paid.

Testing Payments : Use the Stripe Card 4242 4242 4242 4242 (Dummy) with a future expiration date 12/26 (Dummy) and a 3-digit CVC 123 (Dummy) to simulate a successful transaction.

API Endpoints

Method Endpoint Description Auth Required
GET / Home Page No
GET / POST /api/users Fetch All Users / Create User No
GET / PUT / DELETE /api/users/:id Manage Specific User Admin (PUT / DELETE)
GET / POST /api/categories Fetch All / Create Category Admin (POST Only)
GET / PUT / DELETE /api/categories/:id Manage Specific Category Admin (PUT / DELETE)
GET / POST /api/products Fetch All / Create Product Admin (POST Only)
GET / PUT / DELETE /api/products/:id Manage specific Product Admin (PUT/DELETE)
GET / POST /api/orders Fetch All / Create Order & Stripe URL No
GET /api/orders/:id Fetch specific Order No
POST /api/webhooks/stripe Stripe Webhook Listener Stripe Signature

Testing With Dummy Data

To test protected routes (POST, PUT, DELETE), you must include the custom header :

  • Key : x-user-id
  • Value : admin_1 (For Admin Access) or user_1 (For Standard User Access)

1. Create Users

Endpoint : POST /api/users

Headers : Not Required

Note : Do not include stripe_id in the body. The backend automatically generates it via Stripe.

Admin User :

{
    "id": "admin_1",
    "name": "John Doe",
    "address": "Bangalore",
    "phone": "9876543210",
    "role": "Admin"
}

Normal User :

{
    "id": "user_1",
    "name": "Jane Doe",
    "address": "Mumbai",
    "phone": "9873216540",
    "role": "User"
}

2. Create Category

Endpoint : POST /api/categories

Headers : x-user-id: admin_1

{
    "name": "Electronics",
    "description": "Gadgets"
}

(Copy the id from the response to use in the Product payload).

3. Create Product

Endpoint : POST /api/products

Headers : x-user-id: admin_1

{
    "name": "Laptop",
    "description": "MacBook Pro",
    "price": 1999.99,
    "category_ids": ["PASTE_YOUR_CATEGORY_ID_HERE"]
}

(Copy the id from the response to use in the Order payload).

4. Create Order

Endpoint : POST /api/orders

Headers : Not Required

{
    "buyer_id": "user_1",
    "order_items": [
        {
            "product_id": "PASTE_YOUR_PRODUCT_ID_HERE",
            "quantity": 2
        }
    ]
}

(Copy the checkout_url from the response, paste it into your browser, and use the test card below to pay).

5. Stripe Test Card Details

Use these dummy details on the Stripe Checkout page to simulate a successful transaction :

  • E-Mail : testuser@gmail.com (Or Any E-Mail)
  • Card Number : 4242 4242 4242 4242 (Or Any 16 Digits)
  • Expiration Date : 12/26 (Or Any Future Date)
  • CVC : 123 (Or Any 3 Digits)
  • Name : Test User (Or Any Name)

Releases

Packages

Contributors

Languages