{"id":13608,"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.","status":"active","version":"6.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/blomqma/next-rest-framework","tags":["javascript","nextjs","rest","api","next-rest-framework","typescript"],"install":[{"cmd":"npm install next-rest-framework","lang":"bash","label":"npm"},{"cmd":"yarn add next-rest-framework","lang":"bash","label":"yarn"},{"cmd":"pnpm add next-rest-framework","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency as a Next.js framework, requires Next.js >= v12.","package":"next","optional":false},{"reason":"Required for input/output schema validation and OpenAPI schema generation, requires Zod >= v3.","package":"zod","optional":false},{"reason":"Needed for handling form data schemas, requires zod-form-data >= v2.","package":"zod-form-data","optional":true},{"reason":"Optional dependency for the CLI when working with TypeScript.","package":"tsx","optional":true}],"imports":[{"note":"Used for App Router documentation endpoints. For Pages Router, use `docsApiRoute`.","wrong":"const { docsRoute } = require('next-rest-framework')","symbol":"docsRoute","correct":"import { docsRoute } from 'next-rest-framework'"},{"note":"Primary function for defining App Router API routes. For Pages Router, use `apiRoute`.","wrong":"import routeHandler from 'next-rest-framework'","symbol":"routeHandler","correct":"import { routeHandler } from 'next-rest-framework'"},{"note":"Use `TypedNextResponse` for type-safe responses that integrate with NRF's schema validation and OpenAPI generation.","wrong":"import { NextResponse } from 'next/server'","symbol":"TypedNextResponse","correct":"import { TypedNextResponse } from 'next-rest-framework'"},{"note":"This is the Pages Router equivalent for REST APIs. Be careful not to confuse with `createRestHandler` for the App Router.","wrong":"import { createRestHandler } from 'next-rest-framework'","symbol":"createRestApiHandler","correct":"import { createRestApiHandler } from 'next-rest-framework'"}],"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."},"warnings":[{"fix":"Review custom middleware implementations, especially those using `async` functions, and ensure their signatures align with the updated `next-rest-framework` types for `TypedNextRequest` and `TypedNextApiResponse`. Refer to the official documentation for updated middleware typings.","message":"Next REST Framework v6.0.0 introduced internal type changes, notably fixing TypeScript errors when using async middleware. While not a direct API change, users with complex or custom middleware might need to review and update their middleware signatures to align with the new async typings.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Upgrade `zod` to version 4 or newer: `npm install zod@latest` or `yarn add zod@latest`.","message":"Ensure `zod` is updated to a compatible version. Version 6.1.0 of Next REST Framework added support for Zod v4. Using an older `zod` version with NRF v6.1.0+ might lead to unexpected validation issues or type mismatches.","severity":"gotcha","affected_versions":">=6.1.0"},{"fix":"If encountering issues, ensure your Next.js project is updated to version 15 or higher to leverage the latest NRF features and compatibility: `npm install next@latest react@latest react-dom@latest`.","message":"Next.js version compatibility is important. Version 6.0.7 of Next REST Framework added support for Next.js 15. Ensure your Next.js project is at a compatible version (>= v12, with specific features tested for newer versions).","severity":"gotcha","affected_versions":">=6.0.7"},{"fix":"Regularly update `next-rest-framework` to its latest patch version: `npm update next-rest-framework` or `yarn upgrade next-rest-framework`.","message":"Version 6.1.1 included security patches. It is crucial to keep `next-rest-framework` updated to mitigate potential security vulnerabilities.","severity":"breaking","affected_versions":">=6.1.1"},{"fix":"Install `tsx` as a dev dependency: `npm install --save-dev tsx` or `yarn add --dev tsx`.","message":"When using `next-rest-framework generate` CLI command in a TypeScript project, the `tsx` package is recommended to be installed as a dev dependency to ensure proper execution and parsing of your TypeScript route files.","severity":"gotcha","affected_versions":">=6.0.0"}],"env_vars":null,"search_vec":"'6.1.1':35 'across':94 'activ':38 'address':46 'api':28,77,95,100,135,140 'app':111 'applic':31 'automat':67 'build':21 'choic':131 'compat':51 'compliant':73 'current':32 'definit':96 'design':19 'develop':136 'differenti':64 'document':27,74,78 'e.g':86 'ecosystem':57 'edg':125 'endpoint':107 'ensur':89 'fix':47 'flexibl':130 'form':104 'framework':3,6,144 'frequent':41 'generat':68 'includ':102 'integr':120 'interact':76 'javascript':137 'key':63 'latest':54 'leverag':81 'librari':58 'lightweight':15 'like':59 'mainten':39 'make':127 'middlewar':123 'minor':44 'modern':133 'next':1,4,142 'next-rest-framework':141 'next.js':30,55,118,122,134 'nextj':138 'nrf':7,97 'object':84 'open':11 'open-sourc':10 'openapi':70 'opinion':13 'page':114 'paradigm':116 'patch':42 'pattern':101 'receiv':37 'redoc/swaggerui':80 'releas':45 'rest':2,5,103,139,143 'robust':90 'router':112,115 'rpc':106 'runtim':126 'safe':24 'safeti':93 'schema':85 'secur':48 'self':26 'self-docu':25 'set':16 'sourc':12 'specif':72 'specification-compli':71 'support':98 'tool':18 'type':23,92 'type-saf':22 'type-safeti':91 'typescript':82,145 'updat':49 'use':79 'v4':61 'various':99 'version':34 'within':29 'zod':60,87","created_at":"2026-04-20T01:55:22.810320+00:00","updated_at":"2026-04-20T01:55:22.810320+00:00","problems":[{"fix":"Ensure `TypedNextResponse` is imported from `next-rest-framework`: `import { TypedNextResponse } from 'next-rest-framework';`","cause":"`TypedNextResponse` was not imported or is misspelled.","error":"ReferenceError: TypedNextResponse is not defined"},{"fix":"Review the `openApiObject` configuration within your `docsRoute` handler and ensure it adheres to the OpenAPI Specification (OAS) structure, particularly the `info` object.","cause":"The `openApiObject` provided to `docsRoute` (or `docsApiRoute`) is malformed or missing required fields like `info.title` or `info.version`.","error":"Error: Invalid OpenAPI object configuration. Check your docsRoute options."},{"fix":"Ensure your `routeOperation` includes an `input` configuration with a `body` schema and the correct `contentType` matching what your API expects. Example: `.input({ contentType: 'application/json', body: yourZodSchema })`.","cause":"Attempting to access `req.body` in a route operation that doesn't explicitly define an `input` schema with a `body` property, or where the `contentType` doesn't match the incoming request.","error":"TypeError: Cannot read properties of undefined (reading 'body') in handler"},{"fix":"Ensure `tsx` is installed (`npm install --save-dev tsx`) and that your `generate` script correctly points to your `docsRoute` configuration if you have multiple or non-standard paths. For example: `npx next-rest-framework generate --config src/app/api/docs/route.ts`.","cause":"The CLI tool might be struggling to parse your TypeScript files, possibly due to missing `tsx` or incorrect project setup for the CLI.","error":"Error: `npx next-rest-framework generate` failed. Could not parse route files."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":"https://next-rest-framework.vercel.app","github":"https://github.com/blomqma/next-rest-framework","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/next-rest-framework","openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","serialization","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}