Production-ready TypeScript API template built with modern tooling and best practices.
/api/v1
)# Clone the repository
git clone https://github.com/vlhsmylv/hono-template.git
cd hono-template
# Install dependencies
bun install
# Set up environment variables
cp .env.example .env
# Edit .env with your database URL and JWT secrets
# Set up database
bun run db:push # Push schema to database
bun run db:generate # Generate Prisma client
# Set up git hooks (optional)
bun run prepare
# Run in development
bun dev
# Open in browser
open http://localhost:3000
The API uses URL path versioning for backward compatibility (like mastering different flame forms):
/api/v1/users # First Form: Unknowing Fire
/api/v2/users # Second Form: Rising Scorching Sun (future)
v1
(Master one form before moving to the next)/api/v1
src/
โโโ modules/ # API modules with clean architecture
โ โโโ auth/ # Authentication (login, register, logout)
โ โโโ users/ # User management (CRUD operations)
โ โโโ profile/ # User profile management
โ โโโ README.md # Module development guide
โโโ middleware/ # Custom middleware
โ โโโ auth.ts # JWT authentication with refresh tokens
โ โโโ logger.ts # Production logging system
โ โโโ user-injection.ts # Automatic user context injection
โโโ prisma/ # Database client & utilities
โ โโโ index.ts # Prisma client singleton
โโโ public/ # Static files & landing page
โโโ shared/ # Shared utilities & schemas
โโโ utils/ # Helper functions & configurations
โโโ versions/ # API versioning (v1, v2, etc.)
โโโ index.ts # Application entry point
# Development with hot reload
bun dev
# Database operations
bun run db:generate # Generate Prisma client
bun run db:push # Push schema changes
bun run db:migrate # Create and run migrations
bun run db:studio # Open Prisma Studio
# Code quality
bun run lint # Lint TypeScript code
bun run lint:fix # Fix auto-fixable lint errors
bun run format # Format code with Prettier
bun run format:check # Check formatting
bun run type-check # TypeScript type checking
# Testing
bun test # Run tests
bun test:watch # Run tests in watch mode
# Build image
docker build -t hono-template .
# Run container
docker run -p 3000:3000 hono-template
# With environment file
docker run -p 3000:3000 --env-file .env hono-template
Required environment variables (see .env.example
):
# Database Connection
DATABASE_URL="postgresql://user:password@localhost:5432/hono_template"
# JWT Authentication (generate strong secrets!)
ACCESS_TOKEN_SECRET="your-super-secret-access-token-key"
REFRESH_TOKEN_SECRET="your-super-secret-refresh-token-key"
# Application Settings
NODE_ENV="development"
PORT=3000
๐ Security Note: Generate strong, unique secrets for production environments. Never commit real secrets to version control.
The template includes a production-ready logging system:
logs/
directoryinfo
, warn
, error
, debug
import { logger } from "@/middleware/logger";
// Basic logging
logger.info("User registered", { userId: "123", email: "user@example.com" });
logger.warn("Rate limit approaching", { attempts: 8 });
logger.error("Database connection failed", { error: "Connection timeout" });
logger.debug("Debug info", { data: processedData }); // Dev only
// HTTP request logging
logger.request("POST", "/api/users", 201, 145, {
ip: "192.168.1.1",
userAgent: "Mozilla/5.0...",
requestId: "req-abc123",
});
logs/
โโโ 2025-01-14-info.log # All logs (combined)
โโโ 2025-01-14-warn.log # Warning logs only
โโโ 2025-01-14-error.log # Error logs only
โโโ 2025-01-14-debug.log # Debug logs only
The template includes a comprehensive auth system:
// Apply auth middleware to protected routes
controller.use("/*", authMiddleware);
controller.use("/*", injectUser);
// Use decorator pattern for clean user access
controller.openapi(
route,
withUser(async (user, c) => {
// User automatically injected!
return c.json({ profile: user });
})
);
DATABASE_URL
in .env
bun run db:push
bun run db:generate
Current models: User with authentication fields. Easily extendable for your needs.
# Create migration
bun run db:migrate
# Reset database (dev only)
bun run db:push --force-reset
git checkout -b feat/amazing-feature
)git commit -m 'feat: add amazing feature'
)git push origin feat/amazing-feature
)This project is licensed under the MIT License - see the LICENSE file for details.
Built with โค๏ธ by Valeh Ismayilov
Hono no Kokyu ;)
โญ Star this repository if you find it helpful!