Sometimes a project may require a simple REST API (e.g. to send an email). Next.js brought API routes support in v9, but you have to provide your own implementation for handling different HTTP methods, validation, error handling and so on. We have created
@moxy/next-rest-api to solve these problems, so that you can focus on writing your business logic.
Besides getting familiar with the
@moxy/next-rest-api, you might want to give a quick read over Next.js API routes documentation. Next.js extends Node.js
res objects with additional functionality and ships with built-in middleware.
⚠️ Please note that if you require more than a simple API with one or two endpoints, it's better if you create a separate project (and repository) for it.
/www, which is where our frontend code lives, we have a convention to put all our API code into the
Next.js requires pages to be defined in
pages/ so we must create
4. Access your API at
Next.js will map every file in
/pages/api to a corresponding
/api/... URL. For example, the file
/pages/api/products.js will map to the