Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/floriansalvi/HEIG-VD_Ocha-api/llms.txt

Use this file to discover all available pages before exploring further.

This guide walks you through cloning the Ocha API, configuring your environment, starting the server, and making your first authenticated request. You will need Node.js 18 or higher, a running MongoDB instance, and a Cloudinary account before you begin.
1

Clone the repository and install dependencies

Clone the repository and install the required npm packages.
git clone https://github.com/floriansalvi/HEIG-VD_Ocha-api.git
cd HEIG-VD_Ocha-api
npm install
2

Create the .env file

Create a .env file at the project root with the following variables. All four are required for the server to start correctly.
PORT=5001
MONGO_URI=mongodb+srv://<username>:<password>@<host>/<database>?<options>
JWT_SECRET=<your-secret-key>
CLOUDINARY_URL=cloudinary://<api_key>:<api_secret>@<cloud_name>
JWT_SECRET can be any long random string. Use a secret manager or openssl rand -hex 32 to generate one. Do not commit this file to version control.
3

Start the server

Start the API server. Use development mode while building locally — it restarts automatically on file changes.
npm run dev
The server starts on http://localhost:5001. You can verify it is running by visiting http://localhost:5001/api/v1/health.
4

Seed sample data

Populate the database with sample products and stores so you have data to work with immediately.
npm run seed:all
The seeders include Cloudinary image URLs for each product. Make sure your Cloudinary account has the corresponding images published, or product images will return broken URLs.
5

Register a user

Create a new account by sending a POST request to /api/v1/auth. The response includes a JWT token you will use for all subsequent authenticated requests.
curl -X POST http://localhost:5001/api/v1/auth \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "Password_123!",
    "display_name": "your_username"
  }'
A successful response returns HTTP 201 with your token:
{
  "message": "User successfully registered",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "6641f2c3a4e2b10012d3e456",
    "email": "[email protected]",
    "display_name": "your_username"
  }
}
Copy the token value — you will need it in the next step.
6

Make your first authenticated request

Pass the JWT token in the Authorization header to access protected endpoints. The example below retrieves the list of products.
curl http://localhost:5001/api/v1/products \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
The response returns a paginated list of products. By default, page 1 with up to 10 items is returned.
Tokens expire after 7 days. When a request returns 401 Unauthorized, log in again via POST /api/v1/auth/login to get a new token.

Build docs developers (and LLMs) love