Open Trivia DB API
Free trivia questions across many categories and difficulties — no key.
What it does
The Open Trivia Database (OpenTDB) is a free API of user-contributed, verified multiple-choice and true/false trivia questions across ~24 categories and three difficulty levels, licensed CC BY-SA.
Base URL
Authentication
None. /api.php?amount=10 is the core call, with optional category, difficulty, type (multiple/boolean) and encode. /api_category.php lists categories with their ids.
Code examples
curl -s "https://opentdb.com/api.php?amount=5&category=18&difficulty=medium&type=multiple"
import requests
tok = requests.get("https://opentdb.com/api_token.php",
params={"command": "request"}, timeout=10).json()["token"]
r = requests.get("https://opentdb.com/api.php",
params={"amount": 10, "token": tok}, timeout=10).json()
for q in r["results"]:
print(q["question"], "->", q["correct_answer"])Rate limits & quotas
One request every 5 seconds per IP — faster returns response_code 5. A single call returns at most 50 questions. Use a session token so you never get the same question twice until the pool is exhausted (then reset the token).
Key response fields
| response_code | 0 success, 1 no results, 2 bad parameter, 3 token not found, 4 token empty, 5 rate limited |
| results[].question / correct_answer / incorrect_answers[] | The question and options (HTML-encoded by default) |
| results[].category / difficulty / type | Metadata |
Common errors
| response_code 1 | Not enough questions for that category/difficulty combination — ask for fewer. |
| response_code 4 | The session token has returned every available question — call api_token.php?command=reset. |
| response_code 5 | You called within 5 seconds of the last request — wait. |
| (HTML entities) | Questions come URL/HTML-encoded — decode, or pass encode=base64 and decode that. |
When to use it (and alternatives)
The default for quiz and trivia games, Discord/Slack trivia bots and pub-quiz generators. Respect the 5-second limit — fetch a batch of up to 50 at the start of a round rather than one question at a time. For number facts see Numbers API.
FAQ
Why do I keep getting response_code 5?
The rate limit is one request per 5 seconds per IP. Batch: request amount=50 once per quiz session instead of one question per call.
How do I stop questions repeating?
Request a session token (api_token.php?command=request) and pass it on every call. OpenTDB then excludes questions you have already seen until you reset the token.
Why is the text full of " and '?
The default encoding is HTML entities. Decode client-side, or request encode=base64 / encode=url3986 and decode that format instead.
New to APIs?
Short guides for working with this API — and any other.