Unified API Response Architecture (ASP.NET Minimal API + Next.js)
This repository demonstrates a production-ready, fully typed Unified API Response System
for projects using ASP.NET Minimal API on the backend and Next.js on the frontend.
It solves common problems with API consistency, error handling, validation, and frontend–backend synchronization — while keeping the developer experience clean and strongly typed.
All endpoints return a consistent structure through ApiResponse<T>:
successerrorCodemessagedatavalidationErrors(when needed)
No HTML errors, no raw exceptions — everything is structured JSON.
Forget about magic strings and typos like "UNAUHORIZED".
- Backend defines
ErrorCode.cs(enum) - Frontend imports generated
errorCodes.ts - Fully type-safe error handling
- No mismatch between backend and frontend — ever
The backend includes:
- Global exception handler (500 → structured JSON)
- Authorization handler for:
401 Unauthorized403 Forbidden404 Not Found
- Automatic validation response
Zero duplicated try/catch blocks.
Frontend wraps API errors via custom ApiError, allowing:
- consistent error handling
- IDE autocomplete for
error.errorCode,error.validationErrors - easy integration with React Hook Form, React Query, SWR, etc.
Backend returns:
{
"success": false,
"errorCode": "ValidationError",
"message": "Validation failed",
"validationErrors": [
{ "field": "email", "message": "Email is required" },
{ "field": "password", "message": "Password must be at least 6 characters" }
]
}