Using Dynamic Routes with Vercel Functions?

Seems like there should be a way to do this...

For the route /api/events/init/[key] I have this code:
import { useParams } from 'next/navigation';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export function GET(request: Request) {
  const { key } = useParams();

  if (key !== process.env.INITIALIZATION_KEY) {
    return new Response('Invalid key', { status: 403 });
  }

  return new Response(`Keys match!`);
}

This results in the error:
You're importing a component that needs useParams. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.


Seems like there should be a way to use dynamic routes in Vercel Functions, right?

What am I doing wrong?
Was this page helpful?