Open Trivia DB

Games API · 20+ categories · Multiple difficulty levels · No key

TL;DR

Open Trivia Database (OPENTDB) is the go-to API for quiz and trivia games, offering thousands of questions across 24 categories — from General Knowledge and Science to Entertainment and Sports. Each question comes with the correct answer plus three plausible wrong answers (for multiple choice) or a true/false pair. You can filter by category, difficulty (easy/medium/hard), and question type. It's been powering quiz apps for over a decade.

Quick start: https://opentdb.com/api.php?amount=10

No API key needed — just make a request!

How to Use This API

1. Get 10 Random Questions

https://opentdb.com/api.php?amount=10

Returns 10 questions with category, difficulty, type (multiple/boolean), question text (HTML-encoded), correct_answer, and incorrect_answers array.

2. Filter by Category & Difficulty

https://opentdb.com/api.php?amount=5&category=18&difficulty=hard

Category 18 is Science: Computers. Combined with difficulty=hard for challenging programming questions.

3. True/False Only

https://opentdb.com/api.php?amount=5&type=boolean

Filter to true/false questions only. type=multiple for standard 4-choice questions.

4. Get Category List

https://opentdb.com/api_category.php

Returns all categories with their IDs. Use the ID in category parameter.

5. JavaScript — Quiz Game Engine

async function getQuestions(amount = 10, category = null) {
  const params = new URLSearchParams({ amount });
  if (category) params.set('category', category);
  params.set('encode', 'base64'); // avoid HTML encoding issues
  
  const resp = await fetch('https://opentdb.com/api.php?' + params);
  const data = await resp.json();
  
  return data.results.map(q => ({
    category: atob(q.category),
    difficulty: q.difficulty,
    question: atob(q.question),
    correct: atob(q.correct_answer),
    options: shuffle([
      ...q.incorrect_answers.map(a => atob(a)),
      atob(q.correct_answer)
    ])
  }));
}

function shuffle(arr) {
  for (let i = arr.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [arr[i], arr[j]] = [arr[j], arr[i]];
  }
  return arr;
}

getQuestions(5, 18).then(questions => {
  questions.forEach((q, i) => {
    console.log(`${i+1}. [${q.difficulty}] ${q.question}`);
    console.log('   Answers:', q.options.join(', '));
  });
});

6. Python — Category Quiz

import requests
import html

categories = {
    'General Knowledge': 9,
    'Science & Nature': 17,
    'Science: Computers': 18,
    'Mythology': 20,
    'Sports': 21,
    'Geography': 22,
    'History': 23,
    'Animals': 27
}

def get_quiz(category_name, count=5):
    cat_id = categories.get(category_name, 9)
    resp = requests.get('https://opentdb.com/api.php', params={
        'amount': count,
        'category': cat_id
    })
    data = resp.json()
    
    for i, q in enumerate(data['results'], 1):
        question = html.unescape(q['question'])
        correct = html.unescape(q['correct_answer'])
        print(f"\n{i}. {question}")
        print(f"   Answer: {correct}")

get_quiz('Science: Computers', 3)
Try 10 random questions: https://opentdb.com/api.php?amount=10

Frequently Asked Questions

How many questions are in the database?
Thousands of questions across 24 categories. New questions are periodically added by the community. The exact count changes, but there are enough for thousands of unique quizzes.
What are all the category IDs?
9=General Knowledge, 10=Books, 11=Film, 12=Music, 13=Musicals/Theatre, 14=Television, 15=Video Games, 16=Board Games, 17=Science & Nature, 18=Science: Computers, 19=Math, 20=Mythology, 21=Sports, 22=Geography, 23=History, 24=Politics, 25=Art, 26=Celebrities, 27=Animals, 28=Vehicles, 29=Comics, 30=Gadgets, 31=Anime/Manga, 32=Cartoon/TV.
Are there session tokens for duplicate questions?
Yes! Use https://opentdb.com/api_token.php?command=request to get a session token, then pass token=YOUR_TOKEN to the API to avoid getting repeat questions in the same session.
What encoding options are available?
Default is HTML entities (&). Add &encode=url3986 for URL-safe, &encode=base64 for base64, or &encode=none for raw text.
Does the API have rate limits?
To prevent abuse, requests are rate-limited to about 1 per 5 seconds from the same IP. Use session tokens for legitimate quiz apps.
Can I use this for a commercial trivia app?
Yes, Open Trivia DB is free for commercial use. Just include attribution to opentdb.com in your app.

API Details

API URL
https://opentdb.com/api.php
Documentation
opentdb.com/api
Category
Games
Authentication
Not Required
Geographic Coverage
Global — English-language questions

What You Can Build