Docs menu

Guides

Filter questions by category, difficulty and tag

Get only the trivia questions you want from The Trivia API, by category, difficulty, tag or region, and check how many questions match before you build a quiz around them.

By default, /v2/questions gives you 10 questions from anywhere in the question bank. Query parameters narrow that down, so you can build a quiz on one subject at the right level for your players.

Pick categories

Every question is in one of 10 categories: arts_and_literature, film_and_tv, food_and_drink, general_knowledge, geography, history, music, science, society_and_culture and sport_and_leisure.

Pass one or more, separated by commas. You get questions from any of them:

curl "https://the-trivia-api.com/v2/questions?categories=history,geography"

Pick difficulties

difficulties takes easy, medium and hard, again separated by commas:

curl "https://the-trivia-api.com/v2/questions?difficulties=easy,medium"

Difficulty is a judgement about how many players will know the answer, so treat it as a guide rather than an exact score.

Narrow it down with tags

Categories are broad. Tags are specific: animal:dinosaurs, music:the-beatles, place:france or era:1960s. Every question has at least one, and usually five or six. They're the way to build a themed quiz:

curl "https://the-trivia-api.com/v2/questions?tags=animal:dinosaurs&limit=10"

Each tag is a prefix, a colon and a name. The prefix says what kind of thing the tag is (a place, a person, a sport, an era and so on), so place:jordan is the country and person:michael-jordan is the basketball player. The 10 categories are tags too, with hyphens: category:geography, category:sport-and-leisure.

Some things to know about tags:

  • They come from a fixed list of 1,047, and /v2/tag-registry has all of them, each with a label, a description and how many questions it has.
  • They're case-sensitive and lower case, with hyphens between words. place:france works, while Place:France and France match nothing.
  • Tags aren't nested. A question about Paris has place:paris, place:france and place:europe, so to get everything about Europe you filter on place:europe.
  • Several tags in one tags parameter means questions with any of them, not all of them (see below).
  • Some tags only have a handful of questions. Check the count before you promise anyone a 50-question dinosaur quiz.

Tags used to be free-form, like the_beatles or world_war_2. Those are gone, so if you stored any, look up the new slug in the registry. The tag quiz tutorial shows how to build a tag picker on top of the registry.

Several tags means any of them

Passing two tags widens the filter. France has 94 questions and rock music 296, and the two together have more, not fewer:

curl "https://the-trivia-api.com/v2/questions/count?tags=place:france"             # "count": 94
curl "https://the-trivia-api.com/v2/questions/count?tags=place:france,music:rock"  # "count": 390

There's no way to ask for questions that have all of several tags. Either pick the narrower tag, or fetch with one tag and filter the tags array yourself. That only filters the questions you got back, which is at most 50 per request, so it works best when the second tag is common among them:

const response = await fetch("https://the-trivia-api.com/v2/questions?tags=era:1960s&limit=50");
const questions = await response.json();
const sixtiesRock = questions.filter((question) => question.tags.includes("music:rock"));

Combine filters

Different parameters narrow each other down, while the values within one parameter widen it. This gets hard questions that are about history or geography:

curl "https://the-trivia-api.com/v2/questions?categories=history,geography&difficulties=hard&limit=20"

Check how many questions match

Before you build a quiz on a filter, check there are enough questions behind it. /v2/questions/count takes the same filters and returns a count, with the filters it applied:

curl "https://the-trivia-api.com/v2/questions/count?tags=animal:dinosaurs"

{
  "count": 21,
  "appliedFilters": {
    "categories": ["arts_and_literature", "film_and_tv", "..."],
    "difficulties": ["easy", "medium", "hard"],
    "types": ["text_choice"],
    "tags": ["animal:dinosaurs"]
  }
}

For a single tag, the count in the tag registry is the same number, so you can get the counts for every tag in one request.

If you ask /v2/questions for more questions than match, you just get fewer back. Your app should handle a short list (or an empty one) rather than assume it got limit questions.

Ask questions that suit your players

A few more filters help when you know who's playing:

ParameterWhat it doesExample
region
Adds questions that only make sense in one country, as an ISO 3166-1 alpha-2 code
region=GB
contentFilter
family leaves out questions marked as adult content
contentFilter=family

Without region, every question should be fair to ask anyone, anywhere. With region=GB you also get questions like "With which sport would you most associate the commentator Ted Lowe?", which are fine in the UK and baffling elsewhere.

From code

With the SDK, the filters are the same names, with multiple values as comma-separated strings:

import { getQuestionCount, getRandomQuestions } from "@trivia-api/sdk";
 
const filters = { tags: "animal:dinosaurs", difficulties: "easy,medium" };
 
const { count } = await getQuestionCount(filters);
const questions = await getRandomQuestions({ ...filters, limit: Math.min(count, 10) });

With fetch, URLSearchParams builds the query string for you:

const params = new URLSearchParams({ tags: "animal:dinosaurs", difficulties: "easy,medium" });
const response = await fetch(`https://the-trivia-api.com/v2/questions?${params}`);
const questions = await response.json();

Every parameter is in the API reference.

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