Secure, modular, and extensible API starter built with NestJS, Prisma, JWT auth, RBAC, Winston logging, and TypeScript.
BaseKitAPI is a secure and modular API template with JWT authentication (access/refresh), roles (admin, user), users (CRUD), and Prisma ORM. It is ready to grow with business modules, provides structured logs (Winston + Daily Rotate), a success interceptor and a global exception filter for consistent responses, and OpenAPI/Swagger for documentation.
βStart small, scale comfortably.β
- NestJS (Modular architecture, dependency injection)
- TypeScript (endβtoβend typing, DTOs with
class-validator/class-transformer) - Prisma (ORM with PostgreSQL, generated client)
- JWT (access/refresh tokens), RBAC (roles
admin,user) - Winston +
winston-daily-rotate-file(file logs: app, exceptions, success, console) - Swagger (
@nestjs/swagger) for documentation - Throttler (
@nestjs/throttler) for rateβlimiting (optional) - Passport /
@nestjs/passport/passport-jwt
BaseKitAPI/src
βββ app.module.ts
βββ auth
β βββ auth.controller.ts
β βββ auth.module.ts
β βββ auth.service.ts
β βββ decorators
β β βββ public.decorator.ts
β β βββ roles.decorator.ts
β βββ dto
β β βββ login.dto.ts
β β βββ refres-token.dto.ts
β β βββ register.dto.ts
β β βββ token.dto.ts
β β βββ verify-token.ts
β βββ enums
β β βββ role.enums.ts
β βββ guards
β β βββ jwt.guard.ts
β β βββ roles.guard.ts
β βββ interfaces
β β βββ payload-token.ts
β βββ strategies
β βββ jwt.strategy.ts
βββ logger
β βββ logger.module.ts
β βββ logger.service.ts
βββ main.ts
βββ middleware
β βββ middleware-all-exceptions-filter.service.ts
β βββ middleware-all-success-interceptor.service.ts
β βββ middleware.module.ts
βββ prisma
β βββ prisma.module.ts
β βββ prisma.service.ts
βββ security
β βββ password-hasher.service.ts
β βββ security.module.ts
βββ user
βββ dto
β βββ create-user.dto.ts
β βββ update-password.dto.ts
β βββ update-user.dto.ts
β βββ user.dto.ts
βββ user.controller.ts
βββ user.module.ts
βββ user.repository.ts
βββ user.service.ts
- Node.js >= 18
- Yarn or pnpm
- PostgreSQL running
git clone <your-repo>
cd BaseKitAPI
yarn installCopy the example and adjust values:
cp .env.example .envRecommended variables:
# App
NODE_ENV=development
PORT=3000
# Database (PostgreSQL)
DATABASE_URL="postgresql://user:password@localhost:5432/basekitapi?schema=public"
# JWT
JWT_SECRET="super-secret"
JWT_EXPIRES_IN="15m"
JWT_REFRESH_SECRET="another-super-secret"
JWT_REFRESH_EXPIRES_IN="7d"
# Logs
LOG_LEVEL=info
LOG_DIR=logs
# Throttler (optional)
THROTTLE_TTL=60
THROTTLE_LIMIT=60# Initialize (creates prisma/schema.prisma and .env if missing)
npx prisma init --datasource-provider postgresql
# Edit prisma/schema.prisma and set client output if you want:
# generator client {
# provider = "prisma-client-js"
# output = "./generated/prisma" # or ../generated/prisma as you prefer
# }
# Apply initial migration
npx prisma migrate dev --name init
# Generate the client
npx prisma generate
# (Optional) Explore data
npx prisma studioNote: In this project the Prisma client is exposed via
prisma.module.ts/prisma.service.ts. If you change the client output path, update your imports accordingly.
# Development
yarn start:dev
# Production
yarn build && yarn start:prod- Swagger (if enabled in
main.ts): http://localhost:3000/docs - OpenAPI JSON: http://localhost:3000/docs-json
The app integrates @nestjs/swagger. Check main.ts for Swagger bootstrap and the configured route (defaults to /docs). All DTOs use class-validator and class-transformer to generate models and validate payloads.
- /auth/login β returns accessToken (shortβlived) and refreshToken (longβlived)
- /auth/refresh-token β receives
refreshTokenand returns a new token pair - /auth/verify β verifies a token
- /auth/register β user registration
Send the token on each protected request:
Authorization: Bearer <accessToken>
- Available roles:
admin,user - Applied via
@Roles()andRolesGuard @Public()marks endpoints that skip theJwtGuard
-
POST /auth/register-
Example body:
{ "email": "user@example.com", "password": "Str0ngP@ss!", "username": "Ada Lovelace", "role": "user" }
-
-
POST /auth/login-
Body:
{ "email": "user@example.com", "password": "Str0ngP@ss!" }
-
-
POST /auth/refresh-token-
Body:
{ "refreshToken": "<token>" }
-
-
POST /auth/verify-
Body:
{ "token": "<access_or_refresh_token>" }
-
GET /usersβ list (requiresadmin)GET /users/:idβ detailPOST /usersβ create (may requireadmin)PATCH /users/:idβ updatePATCH /users/:id/passwordβ change passwordDELETE /users/:idβ delete (requiresadmin)
Formats successful responses consistently:
{
"statusCode": 200,
"message": "Users found",
"data": [
/* ... */
],
"meta": {
/* optional: pagination, etc. */
}
}Unifies errors into a readable, loggable shape:
{
"message": "Invalid token",
"statusCode": 400,
"timestamp": "2025-08-28T12:34:56.789Z",
"path": "/auth/verify"
}-
Log categories with daily rotated files:
appβ application eventsexceptionsβ errors and stack tracessuccessβ audit of successful responsesconsoleβ console output (formatted)
-
Default location:
logs/folder (configurable viaLOG_DIR) -
Each entry includes timestamp, level, context, and relevant payload.
See
logger.module.ts/logger.service.tsto customize formats, levels, and transports.
- Password hashing via
security/password-hasher.service.ts - JWT with configurable expiration for access and refresh tokens
- Rate limiting optional with
@nestjs/throttler - Strict DTOs with input validation
Make sure to use strong secrets in production and avoid committing
.env.
-
Prisma client available via
PrismaService -
Versioned migrations with
prisma migrate -
Useful commands:
npx prisma migrate dev --name <name> npx prisma generate npx prisma studio
# development
yarn start:dev
# production
yarn build && yarn start:prod
# tests (if enabled)
yarn test
yarn test:e2e
yarn test:cov- PM2 or Docker for production processes
- Jenkins/GitLab CI for pipelines (build, lint, tests, migrations, deploy)
- Environment variables injected at runtime
Simple PM2 example:
pm2 start dist/main.js --name basekitapi
pm2 logs basekitapi- Generate a new module (
nest g resource <module>) with controller/service/repository. - Define DTOs and validate inputs.
- Add Prisma entities and run migrations.
- Protect routes with
JwtGuardand@Roles()when applicable. - Log key actions (create/update/delete) for traceability.
- Fork and create a branch:
feat/my-feature - Ensure lint and tests pass
- Open a PR describing changes and motivation
Created by hacksJuanDavid. Built on NestJS and the OSS ecosystem.
yarn add prisma @prisma/client
yarn add passport @nestjs/passport @nestjs/jwt passport-jwt
yarn add @nestjs/swagger
yarn add @nestjs/config @nestjs/throttler
yarn add class-transformer class-validator
yarn add winston nest-winston winston-daily-rotate-file
# Types
yarn add -D @types/passport @types/passport-jwt @types/validator