Skip to main content
POST
/
api
/
generate-flashcards
Generate Flashcards
curl --request POST \
  --url https://api.example.com/api/generate-flashcards \
  --header 'Content-Type: application/json' \
  --data '
{
  "topic": "<string>",
  "content": "<string>",
  "numCards": 123
}
'
{
  "cards": [
    {
      "front": "<string>",
      "back": "<string>"
    }
  ],
  "topic": "<string>",
  "error": "<string>"
}

Endpoint

POST /api/generate-flashcards
Generates AI-powered flashcards using the Groq API. Creates between 5 and 50 flashcard pairs based on the provided topic and content.

Request Body

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

Response

cards
array
required
Array of generated flashcards.
topic
string
The topic that was provided in the request.

Example Request

curl -X POST https://yourdomain.com/api/generate-flashcards \
  -H "Content-Type: application/json" \
  -d '{
    "topic": "Spanish Vocabulary - Food",
    "content": "Common Spanish words for food and dining...",
    "numCards": 10
  }'

Example Response

{
  "cards": [
    {
      "front": "What is 'bread' in Spanish?",
      "back": "pan"
    },
    {
      "front": "What is 'water' in Spanish?",
      "back": "agua"
    },
    {
      "front": "What is 'apple' in Spanish?",
      "back": "manzana"
    },
    {
      "front": "What is 'milk' in Spanish?",
      "back": "leche"
    }
  ],
  "topic": "Spanish Vocabulary - Food"
}

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 flashcards"
}
Returned when:
  • The Groq API request fails
  • The AI response cannot be parsed
  • No valid flashcards could be generated
{
  "error": "Failed to parse flashcards"
}
Returned when the AI-generated JSON is invalid or malformed.
{
  "error": "Failed to generate flashcards"
}
Returned when the response contains no valid flashcard pairs.

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
  • Card count is clamped between 5-50

Build docs developers (and LLMs) love