Skip to main content

Prerequisites

Before you begin, make sure you have the following installed:
  • Node.js 20 or higher
  • PostgreSQL 16
  • npm (comes with Node.js)
  • Git

Setup steps

1

Clone the repository

git clone https://github.com/RafaeltiMoreira/mais-habito-api.git
cd mais-habito-api
2

Configure environment variables

Copy the example environment file and fill in your values:
cp .env.example .env
Open .env and set the following variables:
.env
# Server
PORT=3000
NODE_ENV=development

# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mais_habito
DB_USER=postgres
DB_PASSWORD=your_password_here

# JWT
JWT_SECRET=your_super_secret_key_here

FRONTEND_URL=http://localhost:3000
In production, set JWT_SECRET to a long, random string. Generate one with:
openssl rand -base64 32
3

Install dependencies

npm install
4

Create the database and run migrations

First, create the PostgreSQL database:
CREATE DATABASE mais_habito;
Then run the migrations to create all tables:
npm run migrate:dev
This runs all migration files in the src/database/migrations/ directory, creating the users, tasks, challenges, and related tables.
5

Start the development server

npm run dev
The API will start on http://localhost:3000. You should see output indicating the server is listening.
6

Test that it works

Create your first account with a signup request:
curl -X POST http://localhost:3000/api/auth/signup \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Rafael Moreira",
    "email": "[email protected]",
    "password": "minhasenha123"
  }'
A successful response returns a JWT token and your user profile:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "uuid-here",
    "name": "Rafael Moreira",
    "email": "[email protected]",
    "points": 0,
    "current_streak": 0,
    "max_streak": 0
  }
}
Save your token — you’ll need it for all subsequent requests via the Authorization: Bearer <token> header.

Available npm scripts

ScriptCommandDescription
devnpm run devStart development server with nodemon + ts-node
buildnpm run buildCompile TypeScript to dist/
startnpm startRun compiled server from dist/
migrate:devnpm run migrate:devRun migrations using ts-node (development)
migratenpm run migrateRun migrations from compiled dist/ (production)

Next steps

Authentication

Learn how to use your JWT token to access protected endpoints.

API Reference

Explore the full API documentation for all endpoints.

Gamification

Understand how points and streaks work.

Configuration

Full reference for all environment variables.

Build docs developers (and LLMs) love