Python Examples – Health Fitness API¶
Python makes it simple to interact with RESTful APIs like Health Fitness API. Below you'll find examples using multiple Python HTTP libraries for both synchronous and asynchronous requests.
1. Using requests (Synchronous, most common)¶
import requests
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {
"height": 178,
"weight": 82,
"units": "metric",
"lang": "en"
}
headers = {"X-API-Key": API_KEY}
response = requests.get(url, headers=headers, params=params)
data = response.json()
print(data)
POST request example:
import requests
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi/post"
payload = {
"height": 178,
"weight": 82,
"units": "metric"
}
headers = {
"X-API-Key": API_KEY,
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, json=payload)
data = response.json()
print(data)
2. Using httpx (Synchronous and Asynchronous)¶
Synchronous GET example:
import httpx
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}
with httpx.Client() as client:
response = client.get(url, headers=headers, params=params)
data = response.json()
print(data)
Asynchronous GET example:
import httpx
import asyncio
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}
async def fetch_bmi():
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers, params=params)
data = response.json()
print(data)
asyncio.run(fetch_bmi())
POST example (async):
async def post_bmi():
payload = {"height": 178, "weight": 82, "units": "metric"}
async with httpx.AsyncClient() as client:
response = await client.post(url + "/post", headers=headers, json=payload)
data = response.json()
print(data)
asyncio.run(post_bmi())
3. Using urllib (Standard Library)¶
import json
from urllib import request, parse
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
query_string = parse.urlencode(params)
full_url = f"{url}?{query_string}"
req = request.Request(full_url, headers={"X-API-Key": API_KEY})
with request.urlopen(req) as response:
data = json.load(response)
print(data)
POST request using urllib:
payload = json.dumps({"height": 178, "weight": 82, "units": "metric"}).encode("utf-8")
req = request.Request(url + "/post", data=payload, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("X-API-Key", API_KEY)
with request.urlopen(req) as response:
data = json.load(response)
print(data)
4. Using aiohttp (Async, popular for async frameworks)¶
import aiohttp
import asyncio
API_KEY = "YOUR_API_KEY"
url = "https://api.hefitapi.com/api/v1/bmi"
params = {"height": 178, "weight": 82, "units": "metric", "lang": "en"}
headers = {"X-API-Key": API_KEY}
async def fetch_bmi():
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, params=params) as resp:
data = await resp.json()
print(data)
asyncio.run(fetch_bmi())
POST example (aiohttp):
async def post_bmi():
payload = {"height": 178, "weight": 82, "units": "metric"}
async with aiohttp.ClientSession() as session:
async with session.post(url + "/post", headers=headers, json=payload) as resp:
data = await resp.json()
print(data)
asyncio.run(post_bmi())
5. Notes & Best Practices¶
- Always replace
YOUR_API_KEYwith your actual API key. - Use
langparameter for multi-language support (en,fr,de,es, etc.). - For production, consider connection pooling (
httpxclient) and async requests for performance. - Handle errors gracefully: HTTP status codes, timeouts, and exceptions.
- Responses include risk signals, action plans, and enterprise metadata when available.
Next Steps¶
Check other language-specific examples:
- PHP
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
Start integrating Health Fitness API into your Python apps today!
Health Fitness API Powering modern digital health infrastructure