Skip to content

Node.js Examples – Health Fitness API

Use Node.js to access Health Fitness API programmatically in server-side applications.


1. Using the built-in https module

GET request example:

const https = require("https");

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

https.get(url, { headers: { "X-API-Key": apiKey } }, (res) => {
  let data = "";
  res.on("data", (chunk) => { data += chunk; });
  res.on("end", () => { console.log(JSON.parse(data)); });
}).on("error", (err) => { console.error("Error:", err); });
````

**POST request example:**

```javascript
const https = require("https");

const payload = JSON.stringify({ height: 178, weight: 82, units: "metric" });
const options = {
  hostname: "api.hefitapi.com",
  path: "/api/v1/bmi/post",
  method: "POST",
  headers: {
    "X-API-Key": "YOUR_API_KEY",
    "Content-Type": "application/json",
    "Content-Length": Buffer.byteLength(payload)
  }
};

const req = https.request(options, (res) => {
  let data = "";
  res.on("data", (chunk) => { data += chunk; });
  res.on("end", () => { console.log(JSON.parse(data)); });
});

req.on("error", (err) => { console.error("Error:", err); });
req.write(payload);
req.end();

2. Using axios

Install axios:

npm install axios

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 node-fetch

Install node-fetch:

npm install node-fetch

GET request example:

const fetch = require("node-fetch");

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(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.error("Error:", err));

POST request example with async/await:

const fetch = require("node-fetch");

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

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

async function postBMI() {
  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "X-API-Key": apiKey,
        "Content-Type": "application/json"
      },
      body: JSON.stringify(payload)
    });
    const data = await response.json();
    console.log(data);
  } catch (err) {
    console.error("Error:", err);
  }
}

postBMI();

4. Notes & Best Practices

  • Node.js allows server-side requests without CORS issues.
  • Use async/await or Promises for cleaner control flow.
  • Protect your API key; never expose it in client-side code.
  • Responses include _enterprise metadata when applicable for monitoring.

Start integrating Health Fitness API into your Python apps today!


Health Fitness API Powering modern digital health infrastructure