Saltar a contenido

Ejemplos de JavaScript – API de Salud y Fitness

Puedes interactuar con la API de Salud y Fitness utilizando JavaScript moderno en navegadores o Node.js. A continuación, se muestran varios métodos que cubren las solicitudes GET y POST.


1. Utilizando fetch (JavaScript moderno)

Ejemplo de solicitud GET:

const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

fetch(url, {
  headers: {
    "X-API-Key": apiKey
  }
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.error("Error:", err));

Ejemplo de solicitud POST:

const postUrl = "https://api.hefitapi.com/api/v1/bmi/post";

const payload = {
  height: 178,
  weight: 82,
  units: "metric"
};

fetch(postUrl, {
  method: "POST",
  headers: {
    "X-API-Key": apiKey,
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.error("Error:", err));

2. Utilizando axios (navegador y Node.js)

Instalar en Node.js:

npm install axios

Ejemplo de solicitud GET:

const axios = require("axios");

const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi";

axios.get(url, {
  headers: { "X-API-Key": apiKey },
  params: { height: 178, weight: 82, units: "metric", lang: "en" }
})
.then(response => console.log(response.data))
.catch(err => console.error("Error:", err));

Ejemplo de solicitud POST:

axios.post("https://api.hefitapi.com/api/v1/bmi/post", {
  height: 178,
  weight: 82,
  units: "metric"
}, {
  headers: { "X-API-Key": apiKey, "Content-Type": "application/json" }
})
.then(response => console.log(response.data))
.catch(err => console.error("Error:", err));

3. Utilizando XMLHttpRequest (método clásico para navegadores)

Ejemplo de solicitud GET:

const xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en");
xhr.setRequestHeader("X-API-Key", "YOUR_API_KEY");

xhr.onload = () => {
  if (xhr.status === 200) {
    console.log(JSON.parse(xhr.responseText));
  } else {
    console.error("Error:", xhr.statusText);
  }
};

xhr.send();

Ejemplo de solicitud POST:

const xhrPost = new XMLHttpRequest();
xhrPost.open("POST", "https://api.hefitapi.com/api/v1/bmi/post");
xhrPost.setRequestHeader("X-API-Key", "YOUR_API_KEY");
xhrPost.setRequestHeader("Content-Type", "application/json");

xhrPost.onload = () => {
  if (xhrPost.status === 200) {
    console.log(JSON.parse(xhrPost.responseText));
  } else {
    console.error("Error:", xhrPost.statusText);
  }
};

xhrPost.send(JSON.stringify({ height: 178, weight: 82, units: "metric" }));

4. Sintaxis Async/Await (JavaScript moderno)

async function fetchBMI() {
  const apiKey = "YOUR_API_KEY";
  const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

  try {
    const response = await fetch(url, {
      headers: { "X-API-Key": apiKey }
    });
    const data = await response.json();
    console.log(data);
  } catch (err) {
    console.error("Error:", err);
  }
}

fetchBMI();

5. Notas y Mejores Prácticas

  • Reemplace YOUR_API_KEY con su clave API real.
  • El parámetro lang admite respuestas multilingües (en, fr, de, es, etc.).
  • Maneje errores y excepciones, especialmente para operaciones asíncronas.
  • Utilice axios o fetch para aplicaciones front-end modernas; XMLHttpRequest principalmente para soporte legado.
  • Las respuestas de la API incluyen objetos JSON completos con señales de riesgo, planes de acción y metadatos _enterprise cuando estén disponibles.

¡Comience a integrar la API de Salud y Fitness en sus aplicaciones de Python hoy mismo!


API de Salud y Fitness Potenciando la infraestructura digital de salud moderna