Read the documentation to understand how is our API works
First sign up for our API whitelist. You can do that on "get acces
page". If you get your API key (we send it via email) you can create an API request with your
key, as shown below:
Get the full library:
fetch('https://cseteandor.hu/animal-facts/api/YOUR-API-KEY/api.json')
.then(response => response.json())
.then(data => {
//your code here
//use data to access the library
});
Code copied!
This piece of code request the data (the animal facts) from the server, and formats the data as JSON. You can use that data, for displaying or printing.
Note that you can use the API for personal use only. If you want to use for commercial use, please contact us at [email protected].
Get a random fact
fetch('https://cseteandor.hu/animal-facts/api/YOUR-API-KEY/api.json')
.then(response => response.json())
.then(data => {
const animals = data.animals;
const randomIndex = Math.floor(Math.random() * animals.length);
const animal = animals[randomIndex];
let fact;
let animalName;
if (Array.isArray(animal.facts)) {
// If animal has multiple facts, select one randomly
const randomFactIndex = Math.floor(Math.random() * animal.facts.length);
fact = animal.facts[randomFactIndex];
animalName=animal.name;
} else {
// If animal has only one fact, use that fact
fact = animal.facts;
animalName=animal.name;
}
});
Code copied!
The structure of the JSON database:
{
"animals": [
{
"name": "Animal1",
"facts": [
"Fact1",
"Fact2",
"Fact3"
]
},
{
"name": "Animal2",
"facts": [
"Fact1",
"Fact2",
"Fact3"
]
}
]
}
Get the full library:
fetch('https://cseteandor.hu/animal-facts/api/YOUR-API-KEY/api.json')
.then(response => response.json())
.then(data => {
//your code here
//use data to access the library
});
Code copied!
This piece of code request the data (the animal facts) from the server, and formats the data as JSON. You can use that data, for displaying or printing.
Note that you can use the API for personal use only. If you want to use for commercial use, please contact us at [email protected].
Get a random fact
fetch('https://cseteandor.hu/animal-facts/api/YOUR-API-KEY/api.json')
.then(response => response.json())
.then(data => {
const animals = data.animals;
const randomIndex = Math.floor(Math.random() * animals.length);
const animal = animals[randomIndex];
let fact;
let animalName;
if (Array.isArray(animal.facts)) {
// If animal has multiple facts, select one randomly
const randomFactIndex = Math.floor(Math.random() * animal.facts.length);
fact = animal.facts[randomFactIndex];
animalName=animal.name;
} else {
// If animal has only one fact, use that fact
fact = animal.facts;
animalName=animal.name;
}
});
Code copied!
The structure of the JSON database:
{
"animals": [
{
"name": "Animal1",
"facts": [
"Fact1",
"Fact2",
"Fact3"
]
},
{
"name": "Animal2",
"facts": [
"Fact1",
"Fact2",
"Fact3"
]
}
]
}