Next.js isn't just a React framework — it's a full-stack API platform hiding in plain sight.
The Built-in API Routes
Every Next.js app comes with a powerful API layer. Drop a file in pages/api/ (Pages Router) or app/api/ (App Router), and you instantly have a serverless endpoint.
// app/api/hello/route.js
export async function GET(request) {
return Response.json({ message: 'Hello from Next.js API!' })
}
Route Handlers in App Router
The App Router brought a cleaner API pattern:
// app/api/users/[id]/route.js
export async function GET(request, { params }) {
const { id } = await params
const user = await getUser(id)
return Response.json(user)
}
export async function PUT(request, { params }) {
const body = await request.json()
const updated = await updateUser(params.id, body)
return Response.json(updated)
}
Middleware — The Hidden Power
Next.js middleware runs BEFORE your routes, giving you authentication, redirects, and header manipulation at the edge:
// middleware.js
export function middleware(request) {
const token = request.cookies.get('auth-token')
if (!token) {
return NextResponse.redirect(new URL('/login', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*', '/api/protected/:path*']
}
Server Actions — API Without the API
With Server Actions, you don't even need API routes for mutations:
// app/actions.js
'use server'
export async function createPost(formData) {
const title = formData.get('title')
const post = await db.post.create({ data: { title } })
revalidatePath('/posts')
return post
}
Real-World Use Case
A SaaS founder I know was paying $200/month for a separate API server. They moved everything to Next.js API routes, deployed on Vercel's free tier, and cut their infrastructure cost to $0 for the first 100K requests.
The best API is the one you don't have to build separately.
Build Smarter Data Pipelines
Need to scrape websites, extract APIs, or automate data collection? Check out my ready-to-use scrapers on Apify — no coding required.
Custom scraping solution? Email me at spinov001@gmail.com — fast turnaround, fair prices.
Top comments (0)