Skip to content

Niyander Tech

Learn with fun

Menu
  • Home
  • Categories
    • Android
    • Alpine.js Components
    • Genshin Impact News
    • Jobs and Internship
    • Coursera Hub
  • Our Policies
    • Privacy Policy
    • Terms and Conditions
    • Contact Us
  • Coursera Search Engine
  • AI Tools
    • AI Background Remover
    • AI Video Downloader
    • 98+ Beautiful CSS Box-Shadow
    • G-Drive Download Link Generator
Menu
How to Create Cat Fact application Using Free Api

How to Create a Cat Fact Application Using a Free API

Posted on November 30, 2021September 6, 2024 by Niyander

Hello friends! In this article, I will guide you through creating a delightful Cat Fact application using a free API. Whether you’re a beginner or just looking to add a fun feature to your web projects, this tutorial will walk you through every step. / How to Create a Cat Fact Application Using a Free API

Introduction

Creating a Cat Fact application is a fun way to learn web development basics while working with real data. This project uses a free API to fetch cat facts and display them dynamically on your page. With some basic knowledge of HTML, CSS, and JavaScript, you’ll be able to build this application and customize it to your liking.

Demo and Source Code / Before diving into the code, you can see a live demo of the Cat Fact application here:
See Demo

Download Full Source Code
View Full Source Code

 

Building the Application

Step 1: Create a Basic HTML Template
Start by creating a basic HTML template. Save this file as index.html.

<!DOCTYPE html> 
<html>
<head> 
<title>Cat Fact Application</title>
</head>
<body>
<h1>Cat Fact</h1>
<p>This is a paragraph.</p>
</body>
</html>

 

Step 2: Add CDN Links for Styling
To enhance the appearance of your application, include Bootstrap 5 and Google Fonts CDN links in the <head> section of your HTML.

<!-- Bootstrap CSS --> 
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Google Fonts CSS --> 
<link rel="preconnect" href="https://fonts.googleapis.com"> 
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> 
<link href="https://fonts.googleapis.com/css2?family=Comfortaa&display=swap" rel="stylesheet">

 

Step 3: Update the HTML Body
Replace the content inside the <body> tag with the following code. This code creates a card layout for displaying cat facts and includes a refresh button.

<div class="container shadow mt-5 p-2 rounded bg-white border">
<div class="d-flex justify-content-between">
<div class="">
<p class="mt-3 ms-3 datep" id="date">Date</p>
</div>
<div class=""> 
<button type="button" class="btn btn-primary btn-sm m-2 p-2 shadow-sm" onclick="fetchText()"> Refresh </button> 
</div>
</div>
<h1 class="text-center mb-3">Cat Facts</h1>
<div class="d-flex justify-content-between p-4">
<div class="p-0 ms-1"> <img id="myImg" src="https://static.wikia.nocookie.net/p__/images/0/06/1618209642054.png/revision/latest/scale-to-width-down/350?cb=20210412064150&path-prefix=protagonist"/> </div>
<div class="p-0 ms-3">
<p id="demo" class="b"> </p>
</div>
</div>
</div>
<p id="error"> </p>

 

Step 4: Apply Custom CSS
Add the following CSS code within the <style> tags in the <head> section to style your card and page.

<style>
* {
font-family: 'Comfortaa', cursive;
}
body {
background-color: #A2D2FF;
}
.container {
width: 650px;
}
img {
width: 200px;
}
.datep {
background-color: #97f7ada3;
padding: 5px;
font-size: 12px;
border-radius: 4px;
}
</style>

 

Step 5: Implement JavaScript
Finally, include the following JavaScript code just before the closing </body> tag. This script fetches a new cat fact from the API and updates the page accordingly.

<script>
// Fetch API module 
async function fetchText() {
let response = await fetch('https://catfact.ninja/fact');

console.log(response.status); // 200
console.log(response.statusText); // OK

if (response.status === 200) {
let data = await response.json();
// handle data
console.log(data);

var nfact = data.fact; 
document.getElementById("demo").innerHTML = nfact;

var rand = Math.floor(Math.random() * 4) + 1;
if(rand == 1) {
document.getElementById("myImg").src = "images/pic1.png";
} else if(rand == 2) {
document.getElementById("myImg").src = "images/pic2.png";
} else if(rand == 3) {
document.getElementById("myImg").src = "images/pic3.png";
} else if(rand == 4) {
document.getElementById("myImg").src = "images/pic4.png";
} else {
document.getElementById("myImg").src = "images/pic2.png";
}
}
}

fetchText();
setInterval(function(){ 
fetchText(); 
}, 25000); 

// Date Module 
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; 
var yyyy = today.getFullYear();
if(dd < 10) {
dd = '0' + dd;
} 
today = dd + '-' + mm + '-' + yyyy;
console.log(today);
document.getElementById("date").innerHTML = today;
</script>

 

Conclusion

Congratulations! You have successfully created a Cat Fact application using a free API. By following these steps, you’ve learned how to fetch data from an API, style your application with Bootstrap and Google Fonts, and update your content dynamically with JavaScript. Feel free to further customize your app and explore more features.

Follow me for more cool projects and tutorials!

 

Category: Bootstrap

Post navigation

← How to Create a Gmail-Style Bootstrap 5 Modal Box
How to Create a Beautiful Love Calculator Using JavaScript →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Notice

Hey guys! Need a Google Drive Direct Download Link Generator? I made a simple tool that does just that. 🚀
Convert any shared Drive link into a direct download link instantly.
Try it out now: https://niyander.com/projects/tools/gdrive/
Let me know your thoughts!

Notice

Hi everyone! I've built a collection of 98+ CSS Box-Shadow Examples for developers and designers! 🎨
Click on any card to copy the shadow — super handy for your projects.
Explore them all here: https://niyander.com/projects/tools/box-shadow/
Hope you find it useful!

Notice

Hey folks! Excited to share my new tool — an All-in-One Social Media Downloader! 📥
Download videos, photos, and audio from TikTok, YouTube, Instagram, Facebook, and more.
Check it out here: https://niyander.com/projects/tools/sm/
Your feedback is welcome!

Notice

Hey friends! I just launched a free AI Background Remover called Panda AI! 🐼
Easily remove backgrounds from images with just one click.
Try it out now: https://niyander.com/projects/tools/bg/
Let me know what you think!

May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Apr    

About

Greetings, Hey i am Niyander, and I hail from India, we strive to impart knowledge and offer assistance to those in need.

  • Alpine.js Components
  • Android
  • Bootstrap
  • Coursera Hub
  • Genshin Impact News
  • Jobs and Internship
  • Uncategorized

Hot Topics

  • Microsoft Azure Data Scientist Associate (DP-100) Quiz Answers + Review
  • Prepare for DP-100: Data Science on Microsoft Azure Exam Answers + Review
  • Genshin Impact Version 5.6 redeem codes
  • More Software Engineering Jobs in Japan for Expats – April 2025 Edition
© 2025 Niyander Tech | Powered by Minimalist Blog WordPress Theme