Building Quiz APIs with Next.js: A Complete Guide
Learn how to build a production-ready quiz API using Next.js App Router, Prisma, and TypeScript. We cover authentication, rate limiting, and best practices.
Introduction
Building a quiz API might seem straightforward, but creating one that's production-ready requires careful thought about authentication, rate limiting, data modeling, and developer experience.
In this guide, we'll walk through building a complete quiz API using Next.js 16 App Router, Prisma ORM, and TypeScript.
Setting Up the Project
First, create a new Next.js project with TypeScript and Tailwind CSS:
npx create-next-app@latest quiz-api --typescript --tailwind --app
Install the required dependencies:
npm install prisma @prisma/client zod
Data Modeling with Prisma
The core of any quiz API is the data model. We need to represent quizzes, questions, and answers:
model Quiz {
id String @id @default(cuid())
title String
description String?
category String?
difficulty Difficulty @default(EASY)
published Boolean @default(false)
questions Question[]
createdAt DateTime @default(now())
}
model Question {
id String @id @default(cuid())
text String
type QuestionType @default(MULTIPLE_CHOICE)
difficulty Difficulty @default(EASY)
quizId String
quiz Quiz @relation(fields: [quizId], references: [id])
answers Answer[]
}
API Route Structure
With Next.js App Router, we organize our API routes under /api/v1/:
GET /api/v1/quizzes— List all published quizzesGET /api/v1/quizzes/:id— Get a specific quiz with questionsGET /api/v1/questions— List questions with filtering
Authentication with API Keys
For public APIs, API key authentication is the standard approach. We generate keys with the nanoid package and store hashed versions in the database.
Rate Limiting
To protect the API from abuse, implement rate limiting using an in-memory store or Redis for distributed deployments.
Conclusion
Building a quiz API with Next.js gives you the best of both worlds: a modern React frontend and a powerful API backend in a single codebase. The App Router makes it easy to organize routes, and Prisma provides type-safe database access.
Check out the QuizAPI documentation for the complete API reference.
Enjoyed this article?
Share it with your team or try our quiz platform.