> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/RafaeltiMoreira/mais-habito-api/llms.txt
> Use this file to discover all available pages before exploring further.

# Quickstart

> Get the Mais Hábito API running locally in under 5 minutes.

## 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

<Steps>
  <Step title="Clone the repository">
    ```bash theme={null}
    git clone https://github.com/RafaeltiMoreira/mais-habito-api.git
    cd mais-habito-api
    ```
  </Step>

  <Step title="Configure environment variables">
    Copy the example environment file and fill in your values:

    ```bash theme={null}
    cp .env.example .env
    ```

    Open `.env` and set the following variables:

    ```bash .env theme={null}
    # 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
    ```

    <Warning>
      In production, set `JWT_SECRET` to a long, random string. Generate one with:

      ```bash theme={null}
      openssl rand -base64 32
      ```
    </Warning>
  </Step>

  <Step title="Install dependencies">
    ```bash theme={null}
    npm install
    ```
  </Step>

  <Step title="Create the database and run migrations">
    First, create the PostgreSQL database:

    ```sql theme={null}
    CREATE DATABASE mais_habito;
    ```

    Then run the migrations to create all tables:

    ```bash theme={null}
    npm run migrate:dev
    ```

    This runs all migration files in the `src/database/migrations/` directory, creating the users, tasks, challenges, and related tables.
  </Step>

  <Step title="Start the development server">
    ```bash theme={null}
    npm run dev
    ```

    The API will start on `http://localhost:3000`. You should see output indicating the server is listening.
  </Step>

  <Step title="Test that it works">
    Create your first account with a signup request:

    ```bash theme={null}
    curl -X POST http://localhost:3000/api/auth/signup \
      -H "Content-Type: application/json" \
      -d '{
        "name": "Rafael Moreira",
        "email": "rafael@example.com",
        "password": "minhasenha123"
      }'
    ```

    A successful response returns a JWT token and your user profile:

    ```json theme={null}
    {
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
      "user": {
        "id": "uuid-here",
        "name": "Rafael Moreira",
        "email": "rafael@example.com",
        "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.
  </Step>
</Steps>

## Available npm scripts

| Script        | Command               | Description                                       |
| ------------- | --------------------- | ------------------------------------------------- |
| `dev`         | `npm run dev`         | Start development server with nodemon + ts-node   |
| `build`       | `npm run build`       | Compile TypeScript to `dist/`                     |
| `start`       | `npm start`           | Run compiled server from `dist/`                  |
| `migrate:dev` | `npm run migrate:dev` | Run migrations using ts-node (development)        |
| `migrate`     | `npm run migrate`     | Run migrations from compiled `dist/` (production) |

## Next steps

<CardGroup cols={2}>
  <Card title="Authentication" icon="lock" href="/authentication">
    Learn how to use your JWT token to access protected endpoints.
  </Card>

  <Card title="API Reference" icon="code" href="/api-reference/auth/signup">
    Explore the full API documentation for all endpoints.
  </Card>

  <Card title="Gamification" icon="trophy" href="/concepts/gamification">
    Understand how points and streaks work.
  </Card>

  <Card title="Configuration" icon="gear" href="/configuration/environment">
    Full reference for all environment variables.
  </Card>
</CardGroup>
