API in Next JS
Next.js is a full-stack framework. You can legit ship an API right next to your App with no setup.
Any file inside the folder pages/api
is mapped to /api/*
and will be treated as an API endpoint instead of a page
. They are server-side only bundles and won't increase your client-side bundle size.
/pages/api/example.js:
The package next-connect
helps to split our logic based on the methods (GET, PUT, DELETE, etc.) :
A good use case for API Routes is handling form input. For example, you can create a form on your page and have it send a
POST
request to your API Route. You can then write code to directly save it to your database. The API Route code will not be part of your client bundle, so you can safely write server-side code.
Preview Mode
Dynamic API Routes
Last updated
Was this helpful?