Deviopath › Next.js

Route Handler with Async params

Medium · Route Handlers

Route Handlers can live in dynamic folders too. The second argument of a handler is a context object with params, and since Next.js 15 **params is a Promise**:

``ts export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; } ``

Reading params.id without await gives undefined. (Real handlers usually type the first argument as NextRequest, which extends the Web Request used here.)

Implement GET and DELETE for /api/users/[id] on the in-memory users array:

- The id must contain only digits (/^[0-9]+$/). Otherwise respond 400 with { error: "Invalid user id" }.

- GET: respond with the user as JSON (status 200), or 404 with { error: "User <id> not found" } (use the number, e.g. "User 7 not found").

- DELETE: remove the user and respond 204 with **no body** (new Response(null, { status: 204 })), or the same 404 error.

The driver callUsersApi(seed, requests) is already written. It returns { status, body } for each request (body is null when empty).

Next: Todos API (GET + POST)