The API
The API (the-api) is a comprehensive JavaScript/TypeScript framework designed for rapidly building RESTful APIs. Currently at version 22.10.2, it appears to follow a relatively frequent release cadence, indicated by its high major version number. It differentiates itself by providing a robust set of features out-of-the-box, including flexible routing (leveraging Hono under the hood for context handling), automatic CRUD generation for database tables, powerful validation mechanisms, role-based access control, and a rich ecosystem of built-in middlewares for common tasks like CORS, logging, and error handling. The framework supports both Node.js (version 18 and above) and Bun runtimes, and ships with full TypeScript type definitions, enabling a strong development experience. Its design emphasizes convention over configuration, aiming to minimize boilerplate while still offering deep customization capabilities for various API needs.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES Module context, which is the default for `the-api`.fixEnsure your project is configured for ES Modules by adding `"type": "module"` to your `package.json`. Change `require()` statements to `import` statements. -
error: database "your_database_name" does not exist
cause The PostgreSQL database specified in your environment variables (e.g., `DB_DATABASE`) has not been created.fixManually create the database on your PostgreSQL server using `createdb your_database_name` or a GUI tool. -
Error: Missing environment variable: DB_HOST
cause A required environment variable (e.g., `DB_HOST`, `DB_USER`) for database connection or other configurations is not set, or your `.env` file is not being loaded.fixCheck your `.env` file for missing variables and ensure it's loaded correctly, e.g., by running your Node.js application with `node --env-file=.env index.js`.
Warnings
- breaking The-api package is designed for ES Modules (ESM) environments. If using Node.js, ensure your `package.json` includes `"type": "module"`.
- gotcha When using environment variables (e.g., for database connection) with Node.js, you must explicitly load them, for example, by running `node --env-file=.env index.js`. Unlike some other runtimes, Node.js does not automatically load `.env` files.
- gotcha The automatic CRUD features and database migrations provided by the-api rely on `knex`. You must install `knex` and the appropriate database client (e.g., `pg` for PostgreSQL) separately, and configure your database connection.
Install
-
npm install the-api -
yarn add the-api -
pnpm add the-api
Imports
- TheAPI
const { TheAPI } = require('the-api');import { TheAPI } from 'the-api'; - Routings
import Routings from 'the-api';
import { Routings } from 'the-api'; - middlewares
import * as middlewares from 'the-api/middlewares';
import { middlewares } from 'the-api';
Quickstart
import { Routings, TheAPI, middlewares } from 'the-api';
const router = new Routings();
// Define a GET route with a path parameter
router.get('/data/:id', async (c) => {
const { id } = c.req.param(); // Extract route parameter 'id'
// Simulate an async operation, e.g., fetching from a DB
await new Promise(resolve => setTimeout(resolve, 50));
c.set('result', { id, foo: 'bar', timestamp: new Date().toISOString() }); // Set response result
});
// Initialize TheAPI with common middlewares and our defined router
const theAPI = new TheAPI({ routings: [middlewares.common, router] });
// Start the server (using top-level await)
try {
await theAPI.up(); // For Node.js
console.log('TheAPI server started on http://localhost:7788');
// For Bun, you would typically export the result of theAPI.upBun();
// export default await theAPI.upBun();
} catch (error) {
console.error('Failed to start TheAPI server:', error);
process.exit(1);
}