A plug-and-play Next.js API application with built-in logging and error handling—all the stuff I wish I had from the start.
MIT License
A plug-and-play Next.js application template designed for seamless API handling. Just drop in your business logic and start building robust APIs in minutes.
This project utilizes Pino logger for streamlined logging, enabling you to easily trace logs throughout the application.
Each inbound request is assigned a unique request ID for precise tracking.
You can throw errors anywhere in your implementation using the APIError.
These errors are consistently caught and returned as HTTP responses.
Example:
const user = await db.user.find({ id: '1' });
if (!user) {
throw new ApiError({
type: 'NotFoundError',
message: 'User not found', // optional
});
}
The above example will return Http Status 404
with body { message: 'User not found'}
.
Running yarn prettier
will format your code to a consistent style, ensuring a uniform codebase.
Running yarn validate
will validate the entire codebase, helping to maintain code quality and consistency.
This project includes an example implementation for handling a specific API route, demonstrating how to manage HTTP requests within your application:
/api/example-route
To start working with this project locally, follow these steps:
yarn install
to install all required dependencies.yarn dev
to start the development server.Once your business logic is implemented, deploy your application using your preferred hosting service (e.g., Vercel, Netlify, Heroku).
Licensed under MIT.
Looking for more exciting projects and insights?
👇👇
Connect with me on 𝕏 @renenielsendk for updates, tips, and more awesome content!
#nextjs, #nextjs-api, #api, #template