Search code examples
next.jsnext.js13app-router

Is it possible to use path slugs for API routes in Next.JS?


I use app router of Next.js v.14 and have the following directories structure:

/app/api/github/webhook/[token]/route.ts

How can I get the [token] value inside the POST request handler? Are there dedicated fields in the NextRequest structure?

The official documentation completely missed the point of using [slugs] inside API routes.


Solution

  • Here is the official example dynamic-route-segments.

    You can access the token as below:

    export const POST = async ( req, {params} ) => {
        console.log("token value ");
        console.log(params.token);
        const postData = await req.json();
        ...
    }