Next.js API Route Refactoring Tool skill for Claude and Codex
Rewrites an existing Next.js API route into a tidy, modern file: imports grouped, middleware separated, error handling standardized, types added when you…
Install it once and Claude or Codex runs it for you. When your request matches, your AI loads the skill, asks for anything it is missing, and works through the template itself. Prefer to fill it in by hand? See the full prompt template.
Download the skill (.zip) · View SKILL.md
When it is used
Use when an existing Next.js API route file is messy and needs its imports, middleware, and error handling reorganized, optionally moving it to TypeScript. Do not use for writing a route from a spec, or optimizing a route for throughput. Related topics: coding, next.js.
What it will ask you for
- Original file path (free text, e.g. "src/pages/api/example.js")
- Current route source (longer free text)
- Language (choose one: JavaScript, TypeScript)
- Error handling style (choose one: try/catch, next-response, middleware)
- Include request validation (choose one: Yes, No)
- Rename exported handler (free text, e.g. "newHandlerName")
- Add inline comments (choose one: Yes, No)
How it works
- Record the route's observable behavior: accepted methods, status codes, bodies, and side effects, as the contract to preserve.
- Group imports, extract inline middleware into named functions, and route every response through one helper in the chosen error style.
- Add NextApiRequest and NextApiResponse types plus body and response types when TypeScript is selected.
- Insert a minimal body schema check returning 400 when validation is on, rename the handler as given, and add the changelog comment at the top.
- Diff the result against the behavior contract to confirm nothing changed and output the file plus a short change summary.
What you get
The refactored file in a fenced block tagged js or ts, starting with a changelog comment, followed by a brief plain-text summary of the applied changes.
What it watches out for
- Changing a status code or response field while standardizing error handling, which breaks existing clients silently.
- Adding a validation library when the options did not ask for it, which the team now has to audit.
- Renaming the exported handler and missing the import elsewhere in the project, which the summary should warn about.
How to install
Claude.ai and the Claude desktop app
- Download the skill .zip.
- Open Settings, then Capabilities, and find Skills.
- Choose Upload skill and select the .zip.
- Ask Claude for help with the task. It loads the skill when your request matches.
Claude Code
- Download the skill .zip.
- Unzip it into ~/.claude/skills/ (or .claude/skills/ inside a project).
- Start a new session. Claude Code loads the skill when your request matches.
Codex
- Download the skill .zip.
- Unzip it into ~/.agents/skills/ (or .agents/skills/ inside a project).
- Codex picks it up on its own. Type $ and the skill name to use it, or just ask for the task.
Cursor
- Download the Cursor rule (.mdc).
- Save it in .cursor/rules/ inside your project.
- Cursor's agent applies the rule when your request matches its description.
AGENTS.md (Codex, Cursor, and other coding agents)
- Copy the AGENTS.md section.
- Paste it into AGENTS.md at the root of your repository, creating the file if needed.
- Coding agents that read AGENTS.md follow it when your request fits.