v1.0.0
Quickstart GitHub

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:

bash
npm install @atozapi/nextjs

bash
yarn add @atozapi/nextjs
bash
pnpm add @atozapi/nextjs

---

Complete Operational Example

In Next.js App Router, export HTTP method functions wrapped in nextAtozapi:

javascript
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:

javascript
// 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:

javascript
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.

Type to search documentation...