Quick Start
Get a REstJS server running in under 2 minutes.
2 min readEdit this page
Install the CLI
npm install -g @restjs/cliCreate and run
rest create my-api
cd my-api
npm install
rest runYour server is running at http://localhost:3000.
Add a route
Open src/main.ts and add:
import { Application, cors } from '@restjs/core'
import { helmet, rateLimit } from '@restjs/security'
const app = new Application()
app.use(helmet())
app.use(cors())
app.use(rateLimit({ max: 100, windowMs: 60000 }))
app.get('/health', (req, res) => {
res.json({ status: 'ok', uptime: process.uptime() })
})
app.get('/users/:id', (req, res) => {
res.json({ id: req.params.id })
})
app.post('/users', (req, res) => {
const body = req.body as Record<string, unknown>
res.status(201).json({ data: body })
})
app.listen()Save the file. The server restarts automatically.
Tip
rest run uses incremental TypeScript compilation. Restarts happen in under 300ms.
Test it
curl http://localhost:3000/health
curl http://localhost:3000/users/42
curl -X POST http://localhost:3000/users \
-H "Content-Type: application/json" \
-d '{"name":"Tariq","email":"tariq@restjs.dev"}'Add authentication
import { JWT, authenticate } from '@restjs/auth'
const jwt = new JWT(process.env.JWT_SECRET)
app.get('/profile',
authenticate(jwt),
(req, res) => {
res.json({ user: req.locals.user })
}
)Add validation
import { v, validate } from '@restjs/validation'
app.post('/users',
validate({
body: v.object({
name: v.string().min(2).max(100),
email: v.string().email()
})
}),
(req, res) => {
res.status(201).json({ data: req.body })
}
)Next steps
- HTTP Server — routing, middleware, static files
- Authentication — JWT, RBAC, password hashing
- Database — ORM, migrations, query builder
- AI Platform — LLM providers, RAG, agents