Any Anime

FREE
By Kurizu Taz | Updated 1ヶ月前 | Video, Images
Popularity

9.6 / 10

Latency

879ms

Service Level

100%

Health Check

N/A

Back to All Discussions

Difficulty In Fetch Api

When used to generate image it is showing me broken or the image url is broken here is the code :

<script>
const API_URL = “https://any-anime.p.rapidapi.com/anime/img”;
const API_KEY = “87f14d9521msh3509002ffa2ec4dp1cb958jsn8f5ba98cab9c”;

const getImage = async () => {
  try {
    const response = await fetch(API_URL, {
      method: "GET",
      headers: {
        "X-Rapidapi-Key": API_KEY,
        "X-Rapidapi-Host": "any-anime.p.rapidapi.com",
        "Host": "any-anime.p.rapidapi.com"
      },
    });

    if (!response.ok) {
      throw new Error("Failed to fetch image.");
    }

    const data = await response.json();

    // Ensure that the image URL is not empty or null
    if (!data.url) {
      throw new Error("Image URL not found.");
    }

    return data.url;
  } catch (error) {
    console.error(error);
    return null;
  }
};

const displayImage = async () => {
  const imageContainer = document.getElementById("image-container");
  imageContainer.innerHTML = "Loading...";

  const imageUrl = await getImage();

  if (imageUrl) {
    imageContainer.innerHTML = `<img src="${imageUrl}" alt="Random Anime Image">`;
  } else {
    imageContainer.innerHTML = "Failed to load image.";
  }
};

document.getElementById("generate-button").addEventListener("click", displayImage);

</script>

Rapid account: Kurizu
Kurizu Commented 6ヶ月前

I’ve updated the tests and docs on rapid.
Hope the updated docs help !

Join in the discussion - add comment below:

Login / Signup to post new comments