The Trivia API provides random trivia from a database containing hundreds of thousands questions and answers about science, literature, philosophy and other fascinating topics.
https://api.api-ninjas.com/v1/trivia
Returns a random trivia.
category
optionalCategory of trivia. The possible values are:
artliterature |
language |
sciencenature |
general |
fooddrink |
peopleplaces |
geography |
historyholidays |
entertainment |
toysgames |
music |
mathematics |
religionmythology |
sportsleisure |
limit
optional premiumHow many results to return. Must be between 1
and 30
. Default is 1
.
X-Api-Key
requiredAPI Key associated with your account.
https://api.api-ninjas.com/v1/trivia?category=music
1
2
3
4
5
6
7
[
{
"category": "music",
"question": "Who Was The Midnight Rider",
"answer": "Paul Davidson"
}
]
1
2
3
4
5
6
7
8
9
import requests
category = 'music'
api_url = 'https://api.api-ninjas.com/v1/trivia?category={}'.format(category)
response = requests.get(api_url, headers={'X-Api-Key': 'YOUR_API_KEY'})
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)
If your programming language is not listed in the Code Example above, you can still make API calls by using a HTTP request library written in your programming language and following the above documentation.