Build REST APIs in Minutes, Not Days
JSON REST API is a lightweight, plugin-based framework that makes building REST APIs incredibly simple. With automatic validation, smart relationships, and native JSON:API support, you can focus on your business logic instead of boilerplate.
Why JSON REST API?
π Zero Configuration
Get a fully functional API running in under 5 minutes. No complex setup or configuration files needed.
π Plugin Architecture
Extend your API with powerful plugins. Authentication, validation, CORS, and more - just plug and play.
π Smart Relationships
Define relationships once and get automatic joins, nested queries, and eager loading out of the box.
β Built-in Validation
Schema-based validation ensures your data is always clean. No more manual validation code.
π¦ Multiple Storage Options
Start with in-memory storage for development, switch to MySQL for production. Same API, no code changes.
π― JSON:API Compliant
Follow industry standards with native JSON:API support. Compatible with any JSON:API client library.
Quick Example
import { RestApiPlugin, RestApiKnexPlugin, ExpressPlugin } from 'json-rest-api';
import { Api } from 'hooked-api';
import knexLib from 'knex';
import express from 'express'; // npm install Express
// Create a Knex instance connected to SQLite in-memory database
const knex = knexLib({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true
});
// Create API instance
const api = new Api({ name: 'book-catalog-api', version: '1.0.0' });
// Install plugins
await api.use(RestApiPlugin, { publicBaseUrl: '/api/1.0' }); // Basic REST plugin
await api.use(RestApiKnexPlugin, { knex }); // Knex connector
await api.use(ExpressPlugin, { mountPath: '/api' }); // Express plugin
// Countries table
await api.addResource('countries', {
schema: {
name: { type: 'string', required: true, max: 100, search: true },
code: { type: 'string', max: 2, unique: true, search: true }, // ISO country code
}
});
await api.resources.countries.createKnexTable()
// Create the express server and add the API's routes
const app = express();
app.use(api.http.express.router);
app.use(api.http.express.notFoundRouter);
app.listen(3000, () => {
console.log('Express server started on port 3000. API available at http://localhost:3000/api');
}).on('error', (err) => {
console.error('Failed to start server:', err);
process.exit(1)
});
Thatβs it! You now have a fully functional REST API with:
GET /api/countries
- List all countrieGET /api/countries/:id
- Get a specific countryPOST /api/countries
- Create a new countryPATCH /api/countries/:id
- Update a countryDELETE /api/countries/:id
- Delete a country
Try It Out
# Create a country
curl -i -X POST http://localhost:3000/api/countries \
-H "Content-Type: application/json" \
-d '{"data":{"type": "countries", "attributes": { "name": "United Kingdom", "code": "UK" }}}'
# List all countries
curl -i http://localhost:3000/api/countries
# Get a specific country
curl -i http://localhost:3000/api/countries/1
# Update a country
curl -i -X PATCH http://localhost:3000/api/countries/1 \
-H "Content-Type: application/json" \
-d '{"data":{"id":"1", "type": "countries", "attributes": { "name": "England", "code": "UK" }}}'
# Delete a country
curl -i -X DELETE http://localhost:3000/api/countries/1
Ready to Start?
Installation
npm install json-rest-api
Learn More
- Complete Guide - Everything you need to know
- API Reference - Detailed API documentation
- Tutorial - Step-by-step walkthrough
- GitHub - Source code and issues