OpenTDB-Wrapper 1.9.1

dotnet add package OpenTDB-Wrapper --version 1.9.1                
NuGet\Install-Package OpenTDB-Wrapper -Version 1.9.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenTDB-Wrapper" Version="1.9.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenTDB-Wrapper --version 1.9.1                
#r "nuget: OpenTDB-Wrapper, 1.9.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpenTDB-Wrapper as a Cake Addin
#addin nuget:?package=OpenTDB-Wrapper&version=1.9.1

// Install OpenTDB-Wrapper as a Cake Tool
#tool nuget:?package=OpenTDB-Wrapper&version=1.9.1                

OpenTDB-Wrapper

An async C# wrapper for the Open Trivia DB API.

For a more detailed README checkout the GitHub.

Usage

Setup

As a good rule of thumb, add these using statements.

using OpenTDB; // For fetching questions from the API
using OpenTDB.Enumerators; // For specifying optional paramters
using OpenTDB.Exceptions; // For handling OpenTDB exceptions
using OpenTDB.Models; // For interacting with the Question class

Create an instance of OpenTDB.

OpenTDB.OpenTDB openTDB = new();

If you want to pass in your own HttpClient you can do it like so:

HttpClient httpClient = new();

OpenTDB.OpenTDB openTDB = new(httpClient);

Using Tokens

The Open Trivia Database API provides session tokens for ensuring no duplicate questions are retrieved. You can start usage of a token like so:

await openTDB.InitializeTokenAsync();
// Initializes the session Token.

After one of the following happens you will need to call ResetTokenAsync(), this will get a new session token:

  • After 6 hours your session token is automatically deleted by Open Trivia Database.
  • If you ever exhaust all questions in the database.

You will know for certain that you should call this method if you get one of the following exceptions, these indicate your session token is dead:

  • Code 3: Token Not Found | Session Token does not exist.
  • Code 4: Token Empty | Session Token has returned all possible questions for the specified query. Resetting the Token is necessary.
await openTDB.ResetTokenAsync();
// Sets the Token to a newly created one.

Getting Questions

Use GetQuestionsAsync() to get a list of questions. This method has default values of Category.Any, Difficulty.Any, and QuestionType.Any.

await openTDB.GetQuestionsAsync(10);
// returns a List<Question> with a Count of 10.

Here is a more complicated example. If you wanted to get 10 Nature questions, that were of easy difficulty, and in the form of multiple-choice questions, it would look like so:

await openTDB.GetQuestionsAsync(10, Category.Nature, Difficulty.Easy, QuestionType.MultipleChoice);
// returns a List<Question> with a Count of 10.

[!IMPORTANT]
All values in the Question class will be encoded in whatever you specified when calling GetQuestionsWithEncodingAsync(). You must handle parsing of this data.

Here is a more complicated example. If you wanted to get 10 Nature questions, that were of easy difficulty, in the form of a multiple-choice questions, and had legacy URL encoding it would look like so:

await openTDB.GetQuestionsWithEncodingAsync(10, Category.Nature, Difficulty.Easy, QuestionType.MultipleChoice, Encoding.LegacyURL);
// returns a List<Question> with a Count of 10.

Question

The Question class looks like this:

public class Question
{
  public string Type { get; set; }
  public string Difficulty { get; set; }
  public string Category { get; set; }
  public string QuestionTitle { get; set; }
  public string CorrectAnswer { get; set; }
  public string[] IncorrectAnswers { get; set; }
}

Category Question Count Lookup

If you need to determine how many questions are in a specific category you can do so with GetCategoryQuestionTotalsAsync()
This can be done with a specific Category:

await GetCategoryQuestionTotalsAsync(Category.Nature);
// returns a CategoryCount object.

Or with an integer representing the Category ID:

await GetCategoryQuestionTotalsAsync(17);
// returns a CategoryCount object.

CategoryCount

The CategoryCount class looks like this:

public class CategoryCount
{
  public int CategoryId { get; set; }
  public int TotalQuestions { get; set; }
  public int TotalEasyQuestions { get; set; }
  public int TotalMediumQuestions { get; set; }
  public int TotalHardQuestions { get; set; }
}

Global Question Count Lookup

If you need to find out things like how many questions there are in the entire database you should use GetGlobalQuestionTotalsAsync().

await GetGlobalQuestionTotalsAsync();
// returns a GlobalCount object.

GlobalCount

The GlobalCount class looks like this:

public class GlobalCount
{
  public int TotalQuestions { get; set; }
  public int TotalPendingQuestions { get; set; }
  public int TotalVerifiedQuestions { get; set; }
  public int TotalRejectedQuestions { get; set; }
  public List<GlobalCategoryCount> Categories { get; set; }
}

API Category Lookup

If you need to find just the categories provided by the API you should use GetApiCategoriesAsync() (although, you could always just look at the Category enum).

await GetApiCategoriesAsync()
// returns a List<ApiCategory> object.

ApiCategory

The ApiCategory class looks like this:

public class ApiCategory
{
  public int Id { get; set; }
  public string Name { get; set; }
}

API Coverage

This wrapper has 100% API coverage!

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.9.1 106 5/16/2024
1.8.1 131 4/3/2024
1.5.0 125 2/5/2024
1.0.0 117 2/3/2024
0.0.1 112 2/1/2024

Added support for the api_categories endpoint.