Docs menu

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.

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": ["science", "words"],
    "type": "text_choice",
    "regions": [],
    "isNiche": false
  }
]

The fields you'll use most:

FieldWhat 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. words

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"

ParameterWhat it doesExample
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 about these topics
tags=dinosaurs,space

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/tags lists every tag.

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/sdk

import { 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 and API keys

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. It also unlocks larger requests, sessions that never repeat a question, image questions and other languages. Send your key in the X-API-Key header:

curl -H "X-API-Key: $TRIVIA_API_KEY" "https://the-trivia-api.com/v2/questions?limit=100"

Keep the key on your server. Anyone can read a key that's in code running in their browser.

Next steps

License

Information about the Creative Commons license for The Trivia API.

The API and all data returned from it are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Creative Commons License

This means that the API and the questions it returns are free for noncommercial use. If you have somewhere suitable in your app, then please credit the API somewhere discreet — we love having our hard work shared!

If you would like to use the API commercially, or would like enhanced functionality, this service is offered through a paid subscription.

Sign in

Sign in to access your API key and manage your account.

or continue with email