Getting started
Quickstart
Get random trivia questions as JSON in a couple of minutes, from the command line, JavaScript, TypeScript or Python. You don't need to sign up or get an API key.
The Trivia API serves random multiple choice questions over plain HTTP. It's free for non-commercial projects, and you can make your first request right now without an account.
Make your first request
Every request for questions goes to https://the-trivia-api.com/v2/questions. Try it from a terminal:
curl "https://the-trivia-api.com/v2/questions?limit=1"Or open the same URL in your browser.
Using other languages, typed answers, image questions or sessions? Those need an API key in the
X-API-Keyheader. See Authentication.
Read the response
You get back a JSON array of questions:
[
{
"id": "622a1c377cc59eab6f95055f",
"category": "science",
"difficulty": "easy",
"question": { "text": "What is dialectology the study of?" },
"correctAnswer": "Dialects",
"incorrectAnswers": ["X-Rays", "Drugs", "Clouds"],
"tags": [
"category:science",
"category:society-and-culture",
"topic:ologies-and-phobias",
"topic:language",
"topic:languages-of-the-world"
],
"type": "text_choice",
"regions": [],
"isNiche": false
}
]The fields you'll use most:
| Field | What it is |
|---|---|
question.text | The question to show the player |
correctAnswer | The right answer |
incorrectAnswers | Three wrong answers. Shuffle them in with the right one before you show them. |
category | One of the 10 categories, e.g. science |
difficulty | easy, medium or hard |
tags | More specific topics, e.g. topic:language |
The API reference describes every field.
Choose your questions
Without any parameters you get 10 questions from any category, at any difficulty. Add query parameters to narrow them down:
curl "https://the-trivia-api.com/v2/questions?limit=5&categories=history,geography&difficulties=easy"| Parameter | What it does | Example |
|---|---|---|
limit | How many questions to get, up to 50 | limit=20 |
categories | Only questions from these categories | categories=science,history |
difficulties | Only questions at these difficulties | difficulties=easy,medium |
tags | Only questions with any of these tags | tags=place:france |
Separate multiple values with commas. The categories are music, sport_and_leisure, film_and_tv, arts_and_literature, history, society_and_culture, science, geography, food_and_drink and general_knowledge, and /v2/tag-registry lists every tag, like music:the-beatles or era:1960s, with how many questions it has.
Call it from your code
JavaScript
fetch works in browsers and in Node 18 or later:
const params = new URLSearchParams({ limit: "10", categories: "science" });
const response = await fetch(`https://the-trivia-api.com/v2/questions?${params}`);
if (!response.ok) {
throw new Error(`The Trivia API responded with ${response.status}`);
}
const questions = await response.json();
console.log(questions[0].question.text);TypeScript
The @trivia-api/sdk package has a typed function for every endpoint, and no dependencies:
npm install @trivia-api/sdkimport { getRandomQuestions } from "@trivia-api/sdk";
const questions = await getRandomQuestions({ limit: 10, categories: "science" });
for (const question of questions) {
// `type` tells TypeScript which kind of question this is
if (question.type === "text_choice") {
console.log(question.question.text, question.correctAnswer);
}
}When a request fails, it throws an ApiError with the HTTP status on error.status.
Python
With requests:
import requests
response = requests.get(
"https://the-trivia-api.com/v2/questions",
params={"limit": 10, "categories": "science"},
timeout=10,
)
response.raise_for_status()
for question in response.json():
print(question["question"]["text"], "→", question["correctAnswer"])Limits
Without a key you can get up to 50 questions per request, and up to 20 requests every 5 seconds from one IP address. Going over the rate limit gets you a 429 response, so fetch a whole quiz's questions in one request rather than one at a time.
Commercial use needs a subscription, which also unlocks larger requests and the features that need a key. Authentication shows how to send it.
Next steps
- Get exactly the questions you want with filters, stop questions repeating with sessions, or let players type their answers.
- Moving from Open Trivia DB? The migration guide maps its parameters, categories and session tokens across.
- Build a full quiz app by following the React or Vue tutorial, or fork one of the example apps.
- Look up every endpoint, parameter and response in the API reference.