Exemples Node.js – API Health Fitness¶
Utilisez Node.js pour accéder à l'API Health Fitness de manière programmatique dans les applications côté serveur.
1. Utilisation du module intégré https¶
Exemple de requête GET :
````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); }); `axios` axios
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 node-fetch ``:
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 node-fetch récupérer les métadonnées ``:¶
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
_enterpriselorsque cela est applicable pour la surveillance.
- PHP
- Python
- JavaScript
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- C# / .NET
- Go / Golang
- Ruby / Rails
Commencez à intégrer l'API Health Fitness dans vos applications Python dès aujourd'hui !
API Health Fitness Alimentant l'infrastructure numérique de santé moderne