-
Notifications
You must be signed in to change notification settings - Fork 1
Understanding Next.js App Router Dynamic API Params
James Cross edited this page Jun 17, 2025
·
1 revision
A reusable note explaining why dynamic params like [slug] break in app/api/, the root cause, the fix, and a copy-pastable pattern.
Error:
Route "/api/[slug]" used `params.slug`. `params` should be awaited before using its properties.
When this happens:
- You create a dynamic API route in
app/api/:src/app/api/service-providers/[slug]/route.ts - You write:
export async function GET(req: Request, { params }: { params: { slug: string } }) { const { slug } = params; // ... }
- You expect
paramslike inpage.tsx.
Result:
- Next.js App Router does NOT pass
paramsto API handlers yet.
-
app/route.tsis a pure serverless function. - It gets only
Request. - No
contextorparamsinjection for dynamic segments. - This is by design (still evolving).
See Next.js Dynamic API Routes docs.
👉 Always parse the param manually:
export async function GET(req: Request) {
const url = new URL(req.url);
const parts = url.pathname.split('/');
const slug = parts[parts.length - 1]; // Last segment
// Use slug...
}✔️ Works for [slug], [id], etc.
function getLastPathSegment(url: string): string {
const u = new URL(url);
const segments = u.pathname.split('/');
return segments[segments.length - 1];
}
// Usage
const slug = getLastPathSegment(req.url);| Context |
params available? |
|---|---|
app/page.tsx |
✅ Yes |
app/api/[slug]/route.ts |
❌ No — parse manually |
- If you see:
paramsis undefined orshould be awaitedin dynamic API. - If migrating old
pages/api/[slug].tstoapp/api/. - Always for App Router dynamic APIs.
In App Router dynamic API routes,
paramsdo NOT auto-inject — manually parse from the URL.