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:
1model Quiz { 2 id String @id @default(cuid()) 3 title String 4 description String? 5 category String? 6 difficulty Difficulty @default(EASY) 7 published Boolean @default(false) 8 questions Question[] 9 createdAt DateTime @default(now()) 10} 11 12model Question { 13 id String @id @default(cuid()) 14 text String 15 type QuestionType @default(MULTIPLE_CHOICE) 16 difficulty Difficulty @default(EASY) 17 quizId String 18 quiz Quiz @relation(fields: [quizId], references: [id]) 19 answers Answer[] 20}
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.
Think you understand Tutorial? Put your skills to the test with hands-on quiz questions.
Enjoyed this article?
Share it with your team or try our quiz platform.
Stay Updated
Get the latest tutorials and API tips delivered to your inbox.
No spam, unsubscribe anytime.
Related Articles
How to Build a Quiz App with Django and QuizAPI
Step-by-step guide to building a quiz application with Django using the QuizAPI REST API. Fetch questions, render a quiz UI, and submit scores.
Building a Quiz Component in React with QuizAPI
Build a reusable React quiz component that fetches questions from QuizAPI, manages quiz state, and displays scores. Full TypeScript implementation included.
Building a Quiz Leaderboard with Real-Time Updates
Build a live quiz leaderboard with ranking algorithms, efficient data models, and real-time delivery using SSE and WebSockets.