Skip to main content
Get your Crypto Shop Backend up and running quickly with this step-by-step guide. You’ll register a user, create an order, and process a TRX payment.

Prerequisites

Before you begin, ensure you have:
  • Node.js v16 or higher installed
  • MongoDB running (local or Atlas)
  • Access to TRON testnet (Nile)
  • A text editor or IDE

Setup

1

Clone and Install

Clone the repository and install dependencies:
git clone <your-repo-url>
cd crypto-shop-backend
npm install
2

Configure Environment

Create a .env file in the root directory:
.env
# MongoDB
MONGODB_URI=mongodb://localhost:27017/crypto-shop

# Server
NODE_ENV=development
PORT=3000

# TRON Network (Nile testnet)
TRON_NETWORK=https://nile.trongrid.io

# JWT Secrets (generate secure random strings)
ACCESS_TOKEN_SECRET=your_access_token_secret_min_32_chars_here
REFRESH_TOKEN_SECRET=your_refresh_token_secret_min_32_chars_here

# CORS
CLIENT_URL=http://localhost:3000
Generate strong, random secrets for ACCESS_TOKEN_SECRET and REFRESH_TOKEN_SECRET. Use at least 32 characters.
3

Start the Server

Run the development server:
npm run dev
You should see:
🚀 Server running on port 3000
The server will start on http://localhost:3000
4

Verify Installation

Test the health endpoint:
curl http://localhost:3000/api/health
Expected response:
{
  "status": "OK"
}

Make Your First API Calls

Now let’s walk through a complete user flow: registration, order creation, and payment.
1

Register a New User

Create a new user account. The system automatically generates a TRON wallet:
curl -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "username": "testuser",
    "password": "securepassword123",
    "passwordConfirm": "securepassword123"
  }'
Response:
{
  "message": "User registered successfully",
  "user": {
    "id": "65abc123def456789",
    "email": "[email protected]",
    "username": "testuser",
    "role": "user",
    "wallet": {
      "address": "TYxxx...xxxxx",
      "balance": 0
    }
  }
}
Authentication tokens are automatically set as HttpOnly cookies. Save the user ID and wallet address for the next steps.
2

Fund Your Wallet

Before making purchases, you need TRX in your wallet. Get testnet TRX from the TRON Nile faucet:
  1. Visit Nile Testnet Faucet
  2. Enter your wallet address from the registration response
  3. Request testnet TRX (usually 10,000 TRX)
This is testnet TRX with no real value. Never use testnet addresses for mainnet transactions.
3

Get Available Products

Fetch the product catalog:
curl http://localhost:3000/api/products
Response:
{
  "count": 2,
  "products": [
    {
      "_id": "65xyz789abc123",
      "name": "Premium Crypto Hoodie",
      "description": "Comfortable hoodie with crypto design",
      "price": 50,
      "stock": 100,
      "category": "clothing",
      "isActive": true
    }
  ]
}
4

Create an Order

Create an order with selected products:
curl -X POST http://localhost:3000/api/orders \
  -H "Content-Type: application/json" \
  -H "Cookie: accessToken=YOUR_ACCESS_TOKEN" \
  -d '{
    "products": [
      {
        "productId": "65xyz789abc123",
        "quantity": 1,
        "color": "black"
      }
    ]
  }'
Response:
{
  "success": true,
  "message": "Order created successfully",
  "order": {
    "_id": "65order123456",
    "orderId": "65order123456",
    "products": [
      {
        "productId": "65xyz789abc123",
        "name": "Premium Crypto Hoodie",
        "price": 50,
        "quantity": 1,
        "color": "black"
      }
    ],
    "subtotal": 50,
    "networkFee": -0.01,
    "total": 49.99,
    "status": "pending",
    "merchantAddress": "TMerchant...",
    "createdAt": "2026-03-04T10:30:00.000Z"
  }
}
Save the orderId for the payment step. You can only have one pending order at a time.
5

Process Payment

Pay for the order using TRX from your wallet:
curl -X POST http://localhost:3000/api/orders/65order123456/pay \
  -H "Content-Type: application/json" \
  -H "Cookie: accessToken=YOUR_ACCESS_TOKEN"
Response:
{
  "success": true,
  "message": "Payment sent. Waiting for blockchain confirmation.",
  "order": {
    "_id": "65order123456",
    "status": "pending",
    "transactionHash": "0xabc123..."
  },
  "transaction": {
    "hash": "0xabc123...",
    "from": "TYourWallet...",
    "to": "TMerchant...",
    "amount": 49.99,
    "status": "pending"
  }
}
The payment is sent to the TRON blockchain. The transaction listener will automatically confirm it and update the order status to completed within a few minutes.
6

Check Order Status

Monitor your order status:
curl http://localhost:3000/api/orders \
  -H "Cookie: accessToken=YOUR_ACCESS_TOKEN"
Once confirmed, the status will change from pending to completed.

Real-time Notifications

The backend uses Socket.io for real-time transaction confirmations. Connect to receive instant updates:
import { io } from 'socket.io-client';

const socket = io('http://localhost:3000');

// Join your user room
socket.emit('join-user', userId);

// Listen for transaction confirmations
socket.on('transaction:confirmed', (data) => {
  console.log('Payment confirmed!', {
    orderId: data.orderId,
    txHash: data.txHash,
    message: data.message,
    timestamp: data.timestamp
  });
});

Explore the API

Your backend includes interactive API documentation powered by Swagger:
http://localhost:3000/api/docs
The Swagger UI lets you:
  • Browse all available endpoints
  • Test API calls directly in your browser
  • View request/response schemas
  • Authenticate and try protected routes

Common Issues

If you receive an INSUFFICIENT_BALANCE error, ensure:
  1. You’ve requested testnet TRX from the Nile faucet
  2. The transaction has confirmed (wait 1-2 minutes)
  3. Your wallet has enough TRX to cover the order total plus network fees
You can only have one pending order at a time. Either:
  • Complete the payment for your existing order
  • Wait for it to expire (if implemented)
  • Contact an admin to cancel it
Ensure MongoDB is running:
# Check if MongoDB is running
mongosh --eval "db.version()"

Next Steps

API Reference

Explore all available endpoints and their parameters

Authentication

Learn about JWT tokens, cookies, and role-based access

Admin Dashboard

Manage products, orders, and view analytics

Deployment

Deploy your backend to production

Build docs developers (and LLMs) love