{"library":"openapi-backend","title":"OpenAPI Backend","description":"openapi-backend is a robust and framework-agnostic middleware library for Node.js, designed to streamline API development by leveraging OpenAPI Specification definitions. It provides capabilities for request validation, routing based on operationIds, authentication via security schemes, and automatic response mocking from examples or schemas. The current stable version is 5.16.1, with an active release cadence reflecting continuous development and maintenance. Key differentiators include its agnosticism towards specific web frameworks (e.g., Express, Hapi, Koa, Serverless Lambda), its use of AJV for highly performant JSON Schema validation, and its TypeScript-first design which includes comprehensive type definitions. It operates without generating any code, offering a clean and efficient runtime, and supports OpenAPI 3.1. This library simplifies API implementation by consolidating common backend concerns into a single, declarative tool, minimizing boilerplate code and ensuring adherence to API contracts.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install openapi-backend"],"cli":null},"imports":["import OpenAPIBackend from 'openapi-backend';","import type { Context } from 'openapi-backend';","import type { Request, Response } from 'openapi-backend';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import OpenAPIBackend from 'openapi-backend';\nimport express from 'express';\n\n// In a real application, './petstore.yml' would be your OpenAPI definition file.\n// For demonstration, we'll use a minimal inline definition.\nconst api = new OpenAPIBackend({\n  definition: {\n    openapi: '3.0.0',\n    info: { title: 'Petstore API', version: '1.0.0' },\n    paths: {\n      '/pets': {\n        get: {\n          operationId: 'getPets',\n          responses: { 200: { description: 'A list of pets' } },\n        },\n      },\n      '/pets/{petId}': {\n        get: {\n          operationId: 'getPetById',\n          parameters: [\n            { name: 'petId', in: 'path', required: true, schema: { type: 'string' } },\n          ],\n          responses: { 200: { description: 'A single pet' } },\n        },\n      },\n    },\n  },\n});\n\n// register your framework specific request handlers here\napi.register({\n  getPets: (c, req, res) => res.status(200).json({ result: 'all pets' }),\n  getPetById: (c, req, res) => res.status(200).json({ result: `pet with id ${c.request.params.petId}` }),\n  validationFail: (c, req, res) => res.status(400).json({ err: c.validation.errors }),\n  notFound: (c, req, res) => res.status(404).json({ err: 'not found' }),\n});\n\n// initalize the backend\napi.init();\n\nconst app = express();\napp.use(express.json()); // Enable JSON body parsing\n\n// Connect openapi-backend to your Express application\napp.use((req, res) => api.handleRequest(req, req, res));\n\nconst PORT = process.env.PORT ?? 9000;\napp.listen(PORT, () => {\n  console.log(`Server running on http://localhost:${PORT}`);\n  console.log('Try visiting: http://localhost:9000/pets');\n  console.log('Try visiting: http://localhost:9000/pets/123');\n});","lang":"javascript","description":"This code snippet demonstrates the fundamental steps to initialize `openapi-backend` from an OpenAPI definition (here, a minimal inline one), register custom handlers for specific API operations (like `getPets` and `getPetById`), and implement generic handlers for validation failures and unmatched routes. It then integrates this setup with an Express application, showing how to connect the library's routing and validation logic to custom application logic, providing a basic functional API.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}