Skip to main content
POST
/
api
/
generate-quiz
Generate Quiz
curl --request POST \
  --url https://api.example.com/api/generate-quiz \
  --header 'Content-Type: application/json' \
  --data '
{
  "topic": "<string>",
  "content": "<string>",
  "numQuestions": 123
}
'
{
  "questions": [
    {
      "question": "<string>",
      "options": [
        {}
      ],
      "correctAnswer": 123,
      "explanation": "<string>"
    }
  ],
  "topic": "<string>",
  "error": "<string>"
}

Endpoint

POST /api/generate-quiz
Generates AI-powered multiple-choice quiz questions using the Groq API. Creates between 3 and 20 questions based on the provided topic and content.

Request Body

topic
string
required
The topic or subject for the quiz questions. This field is required.
content
string
Optional additional content or context to base the quiz questions on. The first 500 characters will be used.
numQuestions
number
default:"5"
Number of questions to generate. Must be between 3 and 20. Defaults to 5 if not provided.

Response

questions
array
required
Array of generated quiz questions.
topic
string
The topic that was provided in the request.

Example Request

curl -X POST https://yourdomain.com/api/generate-quiz \
  -H "Content-Type: application/json" \
  -d '{
    "topic": "World War II",
    "content": "World War II was a global conflict that lasted from 1939 to 1945...",
    "numQuestions": 5
  }'

Example Response

{
  "questions": [
    {
      "question": "When did World War II begin?",
      "options": ["1939", "1941", "1937", "1945"],
      "correctAnswer": 0,
      "explanation": "World War II began in 1939 when Germany invaded Poland."
    },
    {
      "question": "Which countries were part of the Allied Powers?",
      "options": [
        "Germany, Italy, Japan",
        "United States, United Kingdom, Soviet Union",
        "France, Germany, Italy",
        "Japan, China, Thailand"
      ],
      "correctAnswer": 1,
      "explanation": "The main Allied Powers were the United States, United Kingdom, and Soviet Union."
    }
  ],
  "topic": "World War II"
}

Error Responses

error
string
Error message describing what went wrong.

400 Bad Request

{
  "error": "Topic is required"
}
Returned when the topic field is missing or empty.

500 Internal Server Error

{
  "error": "Failed to generate quiz"
}
Returned when:
  • The Groq API request fails
  • The AI response cannot be parsed
  • No valid questions could be generated
{
  "error": "Failed to parse quiz"
}
Returned when the AI-generated JSON is invalid or malformed.
{
  "error": "Failed to generate questions"
}
Returned when the response contains no valid questions.

Implementation Details

  • Uses Groq’s mixtral-8x7b-32768 model
  • Temperature set to 0.7 for balanced creativity
  • Maximum 2000 tokens per response
  • Content is truncated to first 500 characters
  • Question count is clamped between 3-20

Build docs developers (and LLMs) love