{"id":16550,"library":"the-api","title":"The API","description":"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.","status":"active","version":"22.10.2","language":"javascript","source_language":"en","source_url":"https://github.com/the-api/the-api","tags":["javascript","THE API","API","REST","typescript"],"install":[{"cmd":"npm install the-api","lang":"bash","label":"npm"},{"cmd":"yarn add the-api","lang":"bash","label":"yarn"},{"cmd":"pnpm add the-api","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for database migrations and the automatic CRUD generation features if you choose to use them.","package":"knex","optional":true},{"reason":"PostgreSQL client library, often used with knex for database operations. Other database clients (e.g., mysql2, sqlite3) may be needed depending on your chosen database.","package":"pg","optional":true}],"imports":[{"note":"The-api is designed for ESM. CommonJS 'require' will not work directly.","wrong":"const { TheAPI } = require('the-api');","symbol":"TheAPI","correct":"import { TheAPI } from 'the-api';"},{"note":"Routings is a named export for defining route groups and individual routes.","wrong":"import Routings from 'the-api';","symbol":"Routings","correct":"import { Routings } from 'the-api';"},{"note":"Built-in middlewares are exposed as a named export from the main package.","wrong":"import * as middlewares from 'the-api/middlewares';","symbol":"middlewares","correct":"import { middlewares } from 'the-api';"}],"quickstart":{"code":"import { Routings, TheAPI, middlewares } from 'the-api';\n\nconst router = new Routings();\n\n// Define a GET route with a path parameter\nrouter.get('/data/:id', async (c) => {\n  const { id } = c.req.param();        // Extract route parameter 'id'\n  // Simulate an async operation, e.g., fetching from a DB\n  await new Promise(resolve => setTimeout(resolve, 50));\n  c.set('result', { id, foo: 'bar', timestamp: new Date().toISOString() }); // Set response result\n});\n\n// Initialize TheAPI with common middlewares and our defined router\nconst theAPI = new TheAPI({ routings: [middlewares.common, router] });\n\n// Start the server (using top-level await)\ntry {\n  await theAPI.up(); // For Node.js\n  console.log('TheAPI server started on http://localhost:7788');\n  // For Bun, you would typically export the result of theAPI.upBun();\n  // export default await theAPI.upBun();\n} catch (error) {\n  console.error('Failed to start TheAPI server:', error);\n  process.exit(1);\n}\n","lang":"typescript","description":"This quickstart demonstrates how to set up a basic The API server with a GET route using path parameters."},"warnings":[{"fix":"Add `\"type\": \"module\"` to your `package.json` file. Update `require()` calls to `import` statements if migrating from CommonJS.","message":"The-api package is designed for ES Modules (ESM) environments. If using Node.js, ensure your `package.json` includes `\"type\": \"module\"`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use `node --env-file=.env your-app.js` or integrate a package like `dotenv` for programmatic loading if the `--env-file` flag is not suitable.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install `knex` and your database client: `npm install knex pg`. Configure database connection details in your `.env` file or directly in the `TheAPI` constructor options.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure your project is configured for ES Modules by adding `\"type\": \"module\"` to your `package.json`. Change `require()` statements to `import` statements.","cause":"Attempting to use CommonJS `require()` syntax in an ES Module context, which is the default for `the-api`.","error":"ReferenceError: require is not defined"},{"fix":"Manually create the database on your PostgreSQL server using `createdb your_database_name` or a GUI tool.","cause":"The PostgreSQL database specified in your environment variables (e.g., `DB_DATABASE`) has not been created.","error":"error: database \"your_database_name\" does not exist"},{"fix":"Check 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`.","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.","error":"Error: Missing environment variable: DB_HOST"}],"ecosystem":"npm"}