Back to Snippets

Dog/Cat API Snippet.

This code displays random dog and cat images using two public APIs with fetch requests. It includes buttons that let users load new images dynamically.
How to Use?
Use the buttons to fetch new images via fetch() from the Dog CEO API and The Cat API. The script handles API calls and image updates with plain JavaScript.
Credits
Inspired by beginner-friendly API projects showcasing dynamic content loading.

<button id="getDog">Get Doggo 🐕</button>
<br><br>
<img id="dogImg" src="" width="300" />

<script>
  document.getElementById("getDog").onclick = async () => {
    const res = await fetch("https://dog.ceo/api/breeds/image/random");
    const data = await res.json();
    document.getElementById("dogImg").src = data.message;
  };
</script>