secure-nextjs-api-routes

Secure Next.js 13+ App Router API routes with composable middleware.

2|3|Updated Oct 21, 2025
One-click install
npx skills add https://github.com/vishalsachdev/claude-skills --skill secure-nextjs-api-routes
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: secure-nextjs-api-routes
Source: https://github.com/vishalsachdev/claude-skills/tree/main/secure-nextjs-api-routes
Command: npx skills add https://github.com/vishalsachdev/claude-skills --skill secure-nextjs-api-routes

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

A comprehensive security middleware system for Next.js 13+ App Router API routes that provides authentication, rate limiting, CSRF protection, audit logging, and security headers in a composable, production-ready pattern.

Core Features & Use Cases

  • Security Middleware: Composable wrapper to enforce policies on API routes.
  • CSRF Protection: Double-submit cookie pattern to prevent cross-site request forgery.
  • Rate Limiting: Supabase-backed limiter to throttle abusive clients.
  • Audit Logging: Track security events and anomalies.
  • Security Headers: Automatically apply best-practice headers.

Quick Start

Wrap your API route with withSecurity and configure options like requireAuth, rateLimit, csrfProtection, and allowedMethods.

Frequently Asked Questions about secure-nextjs-api-routes

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add authentication and rate limiting to Next.js API routes?

Apply composable security middleware to Next.js 13+ App Router routes using the withSecurity wrapper, which enforces authentication, rate limiting via Supabase, CSRF protection, audit logging, and security headers through configurable options like requireAuth, rateLimit, and csrfProtection.

What is CSRF protection and why do I need it on API routes?

CSRF protection prevents cross-site request forgery attacks by implementing a double-submit cookie pattern that validates requests originate from your application. It's essential for state-changing operations to block unauthorized requests from external sites.

Can I use rate limiting to prevent abuse on high-traffic Next.js endpoints?

Yes, configure rate limiting through the middleware's rateLimit option, which uses Supabase-backed throttling to restrict abusive clients and protect high-traffic surfaces from overwhelming traffic and denial-of-service attacks.

How do I track security events and API access on my Next.js routes?

Enable audit logging through the security middleware to automatically track security events and anomalies on API endpoints, providing visibility into authentication attempts, rate limit violations, and suspicious activity for compliance and incident investigation.

Does this security middleware work with existing Next.js API routes?

Yes, the middleware applies to both new and existing Next.js 13+ App Router endpoints. Wrap any route requiring security with withSecurity and configure options for allowed methods, body size limits, and other security policies without redesigning your routes.

What security headers does the middleware apply automatically?

The middleware enforces best-practice security headers as part of its composable policy enforcement, protecting against common web vulnerabilities and ensuring compliance with security standards across your API routes.