Next.js SDK (@atozapi/nextjs)
The @atozapi/nextjs package provides a higher-order wrapper function nextAtozapi for Next.js App Router route handlers (app//route.js or app//route.ts).
---
Installation
Install in your Next.js project:
npm install @atozapi/nextjs
yarn add @atozapi/nextjs
pnpm add @atozapi/nextjs
---
Complete Operational Example
In Next.js App Router, export HTTP method functions wrapped in nextAtozapi:
import { nextAtozapi } from '@atozapi/nextjs';
const userSchema = {
name: {
type: 'string',
required: true,
minLength: 2
},
age: {
type: 'number',
required: true,
min: 18,
max: 120
}
};
export const POST = nextAtozapi({
schema: userSchema,
handler: async (req, { validatedBody }) => {
// verified data is injected directly into context
return Response.json({
id: 101,
name: validatedBody.name,
age: validatedBody.age,
createdAt: new Date().toISOString()
}, { status: 201 });
}
});
---
Error Handling
When validation fails, nextAtozapi intercepts the request before reaching your handler and immediately returns a standard Web API Response object with status 422 and the standardized { detail: [...] } payload.
---
Multiple HTTP Methods (PUT, PATCH, DELETE)
You can export handlers for any HTTP mutation method inside app/api/.../route.js:
// Export PUT endpoint
export const PUT = nextAtozapi({
schema: updateUserSchema,
handler: async (req, { validatedBody }) => {
return Response.json({ success: true, updated: validatedBody });
}
});
// Export standard DELETE without body validation
export async function DELETE(req) {
return new Response(null, { status: 204 });
}
---
Alternative Shorthand Syntax
In addition to the options object, nextAtozapi also supports direct argument passing:
export const POST = nextAtozapi(userSchema, async (req, { validatedBody }) => {
return Response.json({ user: validatedBody }, { status: 201 });
});
For advanced validation rules including nested objects and array constraints, see the Schema Reference.