Docs menu

Guides

Avoid repeated questions with sessions

Use sessions in The Trivia API so players never see the same trivia question twice, across as many requests as your quiz needs.

Every request to /v2/questions picks questions at random, so over enough requests the same question will come up twice. For a single round that rarely matters. For a game people play every day, or a long-running quiz night, players notice.

A session remembers which questions it has given you, and never gives you the same one again.

Sessions need an API key on a Regular or Complete plan.

Create a session

Make a POST request to /v2/session with your key in the X-API-Key header:

curl -X POST -H "X-API-Key: $TRIVIA_API_KEY" "https://the-trivia-api.com/v2/session"

You get back the session, and its id is what you need:

{
  "id": "3kmTmW29aDTf",
  "totalQuestions": 15590,
  "remainingQuestions": 15590,
  "createdAt": "2026-10-17T09:15:12.117+00:00",
  "updatedAt": "2026-10-17T09:15:12.117+00:00"
}

Create one session for each thing that shouldn't repeat: one per player for a daily quiz, or one per game for a quiz night.

Get questions from the session

Pass the session's id as session when you get questions:

curl -H "X-API-Key: $TRIVIA_API_KEY" \
  "https://the-trivia-api.com/v2/questions?session=3kmTmW29aDTf&limit=10"

Every question you get is marked as used, and later requests with the same session skip it. The usual filters still work, so a session can serve a history round and then a science round without repeating anything.

The one exception is query, which can't be combined with a session.

When a session runs out

Once a session has used every question that matches your filters, requests start returning fewer questions than you asked for, and then an empty array. That's the signal to start a new session (or loosen the filters):

import { createSession, getRandomQuestions } from "@trivia-api/sdk";
 
const options = { apiKey: process.env.TRIVIA_API_KEY };
 
export async function nextRound(sessionId: string) {
  const questions = await getRandomQuestions({ session: sessionId, limit: 10 }, options);
  if (questions.length > 0) {
    return { sessionId, questions };
  }
 
  // Everything matching has been asked: start again with a fresh session
  const session = await createSession(options);
  return {
    sessionId: session.id,
    questions: await getRandomQuestions({ session: session.id, limit: 10 }, options),
  };
}

With broad filters that takes a long time, as there are thousands of questions. With a narrow tag it can happen after a few rounds, so check the count first.

Preview questions without using them up

Sometimes you want to fetch questions without committing to them, e.g. to let a quiz host swap out questions they don't like before the game starts. Add preview=true and the questions aren't marked as used:

curl -H "X-API-Key: $TRIVIA_API_KEY" \
  "https://the-trivia-api.com/v2/questions?session=3kmTmW29aDTf&limit=10&preview=true"

Once you've picked the ones to keep, mark them as used yourself with POST /v2/session/{id}/questions:

import { markQuestionsAsUsed } from "@trivia-api/sdk";
 
await markQuestionsAsUsed(
  { id: sessionId, requestBody: { questionIds: chosen.map((question) => question.id) } },
  options
);

Until you do, a preview can return the same questions again.

Keep the key on your server

Sessions need your API key, and anyone can read a key from code that runs in their browser. Create sessions and fetch their questions from your server (or a serverless function), then send the questions on to the browser. Store each session's id wherever you keep the player's or game's other state.

The API reference covers every session endpoint.

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