{"library":"next-rest-framework","title":"Next REST Framework","description":"Next REST Framework (NRF) is an open-source, opinionated, and lightweight set of tools designed for building type-safe, self-documenting APIs within Next.js applications. Currently at version 6.1.1, it receives active maintenance, with frequent patch and minor releases addressing fixes, security updates, and compatibility with the latest Next.js and ecosystem libraries like Zod v4. A key differentiator is its automatic generation of OpenAPI specification-compliant documents and interactive API documentation (using Redoc/SwaggerUI), leveraging TypeScript and object schemas (e.g., Zod) to ensure robust type-safety across API definitions. NRF supports various API patterns, including REST, Form, and RPC endpoints, for both the App Router and Pages Router paradigms in Next.js, and integrates with Next.js Middleware and Edge runtime, making it a flexible choice for modern Next.js API development.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install next-rest-framework"],"cli":null},"imports":["import { docsRoute } from 'next-rest-framework'","import { routeHandler } from 'next-rest-framework'","import { TypedNextResponse } from 'next-rest-framework'","import { createRestApiHandler } from 'next-rest-framework'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { docsRoute, routeHandler, routeOperation, TypedNextResponse } from 'next-rest-framework';\nimport { z } from 'zod';\n\n// 1. Define your data schema using Zod\nconst todoSchema = z.object({\n  id: z.number().int().positive(),\n  title: z.string().min(1),\n  completed: z.boolean(),\n});\n\ntype Todo = z.infer<typeof todoSchema>;\n\n// In-memory data store for demonstration\nconst todos: Todo[] = [\n  { id: 1, title: 'Learn Next.js', completed: false },\n  { id: 2, title: 'Build an API with NRF', completed: true },\n  { id: 3, title: 'Deploy to Vercel', completed: false },\n];\n\n// 2. Create your API docs endpoint (App Router: src/app/api/docs/route.ts)\n// Visit /api/docs to see the auto-generated documentation\nexport const GET_DOCS = docsRoute({\n  openApiObject: {\n    info: {\n      title: 'My Todo API',\n      version: '1.0.0',\n      description: 'Automatically generated API documentation for the Todo List.',\n    },\n    servers: [{ url: 'http://localhost:3000/api' }],\n  },\n  // The CLI will use this path to generate the openapi.json file\n  // By default, it generates to public/openapi.json if not specified here\n});\n\n// 3. Create your REST API route (App Router: src/app/api/todos/route.ts)\nexport const { GET, POST } = routeHandler({\n  GET: routeOperation({\n    operationId: 'getTodos',\n    tags: ['Todos'],\n    summary: 'Retrieve all todos',\n    description: 'Fetches a list of all available todo items.',\n  })\n    .output({\n      status: 200,\n      contentType: 'application/json',\n      schema: z.array(todoSchema),\n    })\n    .handler(async () => {\n      // Simulate network delay\n      await new Promise(resolve => setTimeout(resolve, 50));\n      return TypedNextResponse.json(todos, { status: 200 });\n    }),\n  POST: routeOperation({\n    operationId: 'createTodo',\n    tags: ['Todos'],\n    summary: 'Create a new todo',\n    description: 'Adds a new todo item to the list.',\n  })\n    .input({\n      contentType: 'application/json',\n      body: todoSchema.omit({ id: true }), // ID is auto-generated\n    })\n    .output({\n      status: 201,\n      contentType: 'application/json',\n      schema: todoSchema,\n    })\n    .handler(async (req) => {\n      const newTodo: Todo = {\n        id: todos.length > 0 ? Math.max(...todos.map(t => t.id)) + 1 : 1,\n        ...req.body,\n      };\n      todos.push(newTodo);\n      return TypedNextResponse.json(newTodo, { status: 201 });\n    }),\n});\n\n// To generate the OpenAPI specification file (public/openapi.json by default),\n// add a script to your package.json:\n// {\n//   \"scripts\": {\n//     \"generate-api-spec\": \"npx next-rest-framework generate\"\n//   }\n// }\n// Then run: npm run generate-api-spec\n// After running, visit /api/docs in your browser to see the interactive documentation.","lang":"typescript","description":"This quickstart demonstrates how to set up a self-documenting REST API with GET and POST methods using Next.js App Router and Next REST Framework, including a Zod schema for validation and a docs endpoint for auto-generated OpenAPI documentation. It also shows the CLI command for spec generation.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}