[Next.js + Typescript] How to add req.params with NextApiRequest
See original GitHub issueFeature request
Is your feature request related to a problem? Please describe.
When I use typescript and nextjs. I have some problem with NextApiRequest. I mean there is no params property in NextApiRequest. so How about add params property in NextApiRequest interface?
now I use like this
server.get(
"/post/:postId",
(req: NextApiRequest & Express.Request, res: NextApiResponse) => {
return app.render(req, res, "/post", { postId: req.params.postId });
},
);
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
nextjs API with typescript, restricting NextApiRequest query ...
In this endpoint handler, is there a way to restrict req.query in NextJS NextApiRequest to just string types rather than string | string[]...
Read more >Dynamic API Routes
You can add the dynamic routes used for pages to API Routes too. ... export default function handler(req, res) { const { pid...
Read more >API Routes: Request Helpers
API Routes provide built-in request helpers that parse the incoming request. Learn more about them here.
Read more >Basic Features: TypeScript | Next.js
Next.js supports TypeScript by default and has built-in types for pages ... import type { NextApiRequest, NextApiResponse } from 'next' export default (req: ......
Read more >API Routes: Introduction
For an API route to work, you need to export a function as default (a.k.a request handler), which then receives the following parameters:....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I was dicussing how to define API endpoints in Next.js with a colleague at work and when he told me that it is a bit iffy because you have to put the endpoint definitions under
pages/in a new folder calledapi.Upon some discussion, it feels like there is a bit of a mixing of concerns here. Here’s my reasoning and my questions about the choice here
I expect all routes under
pages/to be returning an actual page, something that has HTML content in its reponse. Thinking along these lines, an API endpoint is fundamentally different, it is meant to interface with another program over HTTP to deal with data, not markup. (I guess you could argue that HTML endpoints are a specialized case of API endpoints, but still…)Moreover, I would expect API endpoints and HTML endpoints to have different scaling/refactoring needs as an application grows and evolves.
My questions are:
What was the choice behind having the
api/folder underpages/? Maybe I’m missing something critical here but apart from ease of implementation, I can’t find a good reason for mixing these two.Has this been discussed before or are there any ongoing discussions about this? I also don’t want to hijack this issue because it is a bit out of the scope for the original question. Should I create another issue and take the discussion there?
They are very similar yet also different and I believe it would be a better idea to have a top level
api/folder for API endpoint definitions (the naming of the folder can be different of course).This is documented here: https://nextjs.org/docs/api-routes/api-middlewares#extending-the-reqres-objects-with-typescript
Thanks!