Back to Blog
Tutorial

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.

Bobby Iliev2026-02-208 min read
Share:

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 quizzes
  • GET /api/v1/quizzes/:id — Get a specific quiz with questions
  • GET /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.

Test Your Knowledge

Think you understand Tutorial? Put your skills to the test with hands-on quiz questions.

Tutorial
Start Practicing

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.