# API Documentation

A real-time trivia system designed for consistency and replayability.

Canonical URL: https://kb.byaditya.com/entry/docs/TriviaUP/API
Source: https://docs.adityabaindur.com/docs/TriviaUP/API
Source updated: 2026-07-18T21:55:06Z
Indexed: 2026-11-05T03:56:29.576Z

---

## Quick Links

### [Frontend](https://triviaup.adityabaindur.com)

React + Vite with real-time state handling

### [Backend](https://backend.triviaup.adityabaindur.com)

Next.js API routes powering trivia logic

### [Github](https://triviaup.adityabaindur.com/github)

Repository with all of the Source Code

---

## Core System

### Problem

Traditional trivia systems frequently repeat questions across sessions.

### Mechanism

A session-based token is issued and attached to all requests to opentdb.

### Outcome

Each session (6 hours) guarantees unique question delivery.

---

## Token Flow

Client token request → Request proxied by Next.js → Token issued by opentdb.

---

## Implementation

- Generate a session token

- Store token on client

- Attach token to all requests

- Reset after expiration (~6 hours)

---

## API

### `/api/trivia`

**warn**

Get a list of questions. Amount is how many questions will be returned

```http
GET /api/trivia?amount={value}
```

#### Json Schema

```json
{
  "amount": number,
  "data": {
    "response_code": number,
    "results": [
      {
        "type": string,
        "difficulty": string,
        "category": string,
        "question": string,
        "correct_answer": string,
        "incorrect_answers": string[]
      }
    ]
  }
}
```

**API request**

```http
GET https://backend.triviaup.adityabaindur.com/api/trivia
```

Request fields (as configured in the original documentation):

```javascript
[
    {
      name: 'amount',
      label: 'Amount',
      type: 'range',
      location: 'query',
      min: 1,
      max: 20,
      step: 1,
      defaultValue: 5,
      required: true,
    },
    {
      name: 'difficulty',
      label: 'Difficulty',
      type: 'select',
      location: 'query',
      options: ['easy', 'medium', 'hard'],
    },
    {
      name: 'type',
      label: 'Question Type',
      type: 'select',
      location: 'query',
      options: [
        { label: 'Multiple Choice', value: 'multiple' },
        { label: 'True / False', value: 'boolean' },
      ],
    },
    {
      name: 'token',
      label: 'Session Token',
      type: 'text',
      location: 'query',
      placeholder: 'Optional',
    },
  ]
```

### `/api/trivia/token`

**warn**

Call once per session. Reuse across requests. Valid for upto 6 hours

```http
GET /api/trivia/token
```

#### Json Schema

```json
{
  "data": {
    "response_code": number,
    "response_message": string,
    "token": string
  },
  "token": string
}
```

**Generate Token**

```http
GET https://backend.triviaup.adityabaindur.com/api/trivia/token
```

---

