Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
How to consume one API endpoint which requires data from another API endpoint (dog.ceo)?

how to consume from one api with another api.

var url_1 = 'https://dog.ceo/api/breeds/list/all';

fetch(url_1)
     .then( response => response.json())
     .then(data => {
const breeds = data.message;
var arr = [];

for (var b in breeds) {
      arr.push({
           breed : b,
           subBreeds : [
             breeds[b][0]
           ],
          images : [{
            url: ''
          }]
      })
  }

I also have this other api, from where I extract the images of each breed of dog, but here you need the variable that would be the name of the dog's breed.

var url_2 = 'https://dog.ceo/api/breed/{breed_name}/images';

fetch(url_2)
   .then( response => response.json())
   .then(data => {
      const images = data.message;
      var arr_images = [];  

   for (var i in images) {
        arr_images.push({
          images : [{
            url: images[i]
          }]    
        })
    }

So what I don't know, how can I join to send the name of the dog's breed to the second api to consume it?

And how can I join the arrangement of the images with the arrangement above?

it should be something like this

{ "breed": "Hound", 
  "subBreeds": [ 
    "subBreedA", 
    "subBreedB", 
    "subBreedC" 
   ], 
  "images":[
     {"url":"http://some.url.com"},
    {"url":"http://some.other.url"}
   ]
}

I hope I have been clear, thanks for your help, I will be very grateful.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would split it up into separate functions so that you can focus on one part at a time. Then, combine them to get all of the data that you want. In this way, you can also re-use each function in case you want to use the data in a different way:

TS Playground

// dog.ceo API

async function fetchDogApiResult (apiPath) {
  const response = await fetch(`https://dog.ceo/api/${apiPath}`);
  if (!response.ok) throw new Error(`Response not OK (${response.status})`);
  const data = await response.json();
  if (data.status !== 'success') throw new Error('Response not successful');
  return data.message;
}

async function fetchBreeds () {
  return fetchDogApiResult('breeds/list/all');
}

async function fetchSubBreeds (breed) {
  return fetchDogApiResult(`breed/${breed}/list`);
}

async function fetchImages (breed, subBreed) {
  return fetchDogApiResult(`breed/${breed}${subBreed ? `/${subBreed}` : ''}/images`);
}

async function fetchDogData () {
  const breeds = await fetchBreeds();
  return Promise.all(Object.entries(breeds).map(async ([breed, subBreeds]) => ({
    breed,
    subBreeds,
    images: (await fetchImages(breed)).map(url => ({url})),
  })));
}

(async () => {
  const dogData = await fetchDogData();
  console.log(JSON.stringify(dogData));
})();

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use async/await for call second api in second then of first api, after you get data from second api, you can use for loop for them. like this


        var url_1 = 'https://dog.ceo/api/breeds/list/all';

    fetch(url_1)
         .then( response => response.json())
         .then(async data => {
    const breeds = data.message;
    const resUrl2 = await fetch(url_2)
    const dataUrl2 = await resUrl2.json()
    var arr = [];

    for (var b in breeds) {
          arr.push({
               breed : b,
               subBreeds : [
                 breeds[b][0]
               ],
              images : [{
                url: ''
              }]
          })
      }

    const images = dataUrl2.message;
          var arr_images = [];  

       for (var i in images) {
            arr_images.push({
              images : [{
                url: images[i]
              }]    
            })
        }
    })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda