Zum Inhalt

Node.js Beispiele – Health Fitness API

Verwenden Sie Node.js, um den Health Fitness API programmatisch in server-seitigen Anwendungen zu nutzen.


1. Verwendung des integrierten Moduls https

Beispiel für eine GET-Anfrage:

````javascript 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); }); `` Verwenden Sie `axios` oder `node-fetch`, um Metadaten wie

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 und

Install und:

bash npm install axios

GET request example:

```javascript 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:

javascript 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 sowie:

bash npm install node-fetch

GET request example:

```javascript 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:

```javascript 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 `` (z.B. Enterprise-Metadaten) abzurufen, falls erforderlich, zur Überwachung.

Beginnen Sie noch heute mit der Integration des Health Fitness API in Ihre Python-Anwendungen!


Health Fitness API Die Grundlage für moderne digitale Gesundheitsinfrastruktur