JavaScript Examples – Health Fitness API¶
You can interact with Health Fitness API using modern JS in browsers or Node.js. Below are multiple methods covering GET and POST requests.
1. Using fetch (modern JS)¶
GET request example:
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));
POST request example:
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. Using axios (browser & Node.js)¶
Install in Node.js:
GET request example:
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));
POST request example:
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. Using XMLHttpRequest (classic browser method)¶
GET request example:
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();
POST request example:
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. Async/Await Syntax (modern JS)¶
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. Notes & Best Practices¶
- Replace
YOUR_API_KEYwith your real API key. langparameter supports multi-language responses (en,fr,de,es, etc.).- Handle errors and exceptions, especially for async operations.
- Use
axiosorfetchfor modern front-end apps;XMLHttpRequestmostly for legacy support. - API responses include full JSON objects with risk signals, action plans, and
_enterprisemetadata when available.
- PHP
- Python
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- C# / .NET
- Go / Golang
- Ruby / Rails
Start integrating Health Fitness API into your Python apps today!
Health Fitness API Powering modern digital health infrastructure