Retrieve Trivia Questions With open-trivia-db
Tags: javascript, node, opensource, typescript
Published: 2022-04-19 01:34:35

## Easy Trivia
open-trivia-db is a small, simple and fast wrapper for [OpenTriviaDatabase](https://opentdb.com/) - A Free to use, user-contributed trivia question database. This module is lightweight, easy to use and fully typed!

If you use **Discord.JS**, checkout [discord-trivia](https://www.npmjs.com/package/discord-trivia)

## Setup
Setup a JavaScript or TypeScript project and install open-trivia-db via NPM:
```
npm i open-trivia-db
```

## Basic API Calls
The bread and butter of this library is the `getQuestions()` function. Provide options describing what kind of questions you want to retrieve such as:

`amount` - The amount of questions to fetch (min. 1, max. 50)
`difficulty` - The difficulty of questions.
`type` - Question type (true/false or multiple choice)
`category` - The category of questions.

```js
import { getQuestions, CategoryNames } from "open-trivia-db";

const questions = await getQuestions({
  amount: 10,
  category: CategoryNames.Animals,
  difficulty: 'easy',
  type: 'multiple'
})
```

The result will be an array of questions with the following shape:
```json
[
{
    value: 'The Italian automaker Lamborghini uses what animal as its logo?',
    category: 'Vehicles',
    type: 'multiple',
    difficulty: 'easy',
    correctAnswer: 'Bull',
    incorrectAnswers: [ 'Bat', 'Horse', 'Snake' ],
    allAnswers: [ 'Bat', 'Horse', 'Snake', 'Bull' ],
    checkAnswer: [Function: checkAnswer]
  },
...
]
```

## Categories
OpenTDB contains 24 categories to choose from:

```
1. General Knowledge
2. Entertainment: Books
3. Entertainment: Film
4. Entertainment: Music
5. Entertainment: Musicals and Theatres
6. Entertainment: Television
7. Entertainment: Video Games
8. Entertainment: Board Games
9. Science and Nature
10. Science: Computers
11. Science Mathematics
12. Mythology
13. Sports
14. Geography
15. History
16. Politics
17. Art
18. Celebrities
19. Animals
20. Vehicles
21. Entertainment: Comics
22. Science: Gadgets
23. Entertainment: Japanese Anime and Manga
24. Entertainment: Cartoon and Animations
```

Use the `CategoryNames` enum to choose categories.

Initiating a category can be done by passing a [CategoryResolvable](https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation#CategoryResolvable) into the constructor

```js
import { Category, CategoryNames } from 'open-trivia-db';


let myCategory = Category.getCategory(9);

myCategory = await Category.getCategory(CategoryNames.Geography); // (9)

console.log(myCategory);

// {
//   id: 22,
//   name: 'Geography',
//   questionCount: { 
//     total: 275, 
//     easy: 80, medium: 139, hard: 56 
//   }
// }
```


## Sessions
OpenTDB API sessions track the questions it has served you and allows for prevention of duplicate questions throughout multiple API calls.

```js
import { Session, getQuestions } from 'open-trivia-db';

const session = new Session();

async function sessionCalls() {
   await session.start();

   const batch1 = await getQuestions({
      amount: 10,
      difficulty: 'hard',
      session
   });

   const batch2 = await getQuestions({
     amount: 10,
     difficulty: 'hard',
     session
   });


   const completeBatch = [...batch1, ...batch2]; // All unique!
}

session.end();
```

NPM: https://www.npmjs.com/package/open-trivia-db
GitHub: https://github.com/Elitezen/open-trivia-db-wrapper
Documentation: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation