Skip to content

Swift / iOS Examples – Health Fitness API

Use Swift to access Health Fitness API from your iOS apps. Below are examples with native URLSession and Alamofire.


1. Using URLSession

GET request example:

import Foundation

let apiKey = "YOUR_API_KEY"
let url = URL(string: "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en")!

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue(apiKey, forHTTPHeaderField: "X-API-Key")

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let error = error {
        print("Error:", error)
        return
    }
    guard let data = data else { return }
    do {
        let json = try JSONSerialization.jsonObject(with: data, options: [])
        print(json)
    } catch {
        print("Decoding error:", error)
    }
}

task.resume()

POST request example:

import Foundation

let apiKey = "YOUR_API_KEY"
let url = URL(string: "https://api.hefitapi.com/api/v1/bmi/post")!

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(apiKey, forHTTPHeaderField: "X-API-Key")

let payload: [String: Any] = ["height": 178, "weight": 82, "units": "metric"]
request.httpBody = try? JSONSerialization.data(withJSONObject: payload, options: [])

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let error = error {
        print("Error:", error)
        return
    }
    guard let data = data else { return }
    do {
        let json = try JSONSerialization.jsonObject(with: data, options: [])
        print(json)
    } catch {
        print("Decoding error:", error)
    }
}

task.resume()

Install Alamofire via Swift Package Manager:

// In Xcode: File > Swift Packages > Add Package Dependency
// URL: https://github.com/Alamofire/Alamofire.git

GET request example:

import Alamofire

let apiKey = "YOUR_API_KEY"
let url = "https://api.hefitapi.com/api/v1/bmi"

AF.request(url, method: .get, parameters: ["height": 178, "weight": 82, "units": "metric", "lang": "en"], headers: ["X-API-Key": apiKey])
    .responseJSON { response in
        switch response.result {
        case .success(let value):
            print(value)
        case .failure(let error):
            print("Error:", error)
        }
    }

POST request example:

import Alamofire

let apiKey = "YOUR_API_KEY"
let url = "https://api.hefitapi.com/api/v1/bmi/post"
let payload: [String: Any] = ["height": 178, "weight": 82, "units": "metric"]

AF.request(url, method: .post, parameters: payload, encoding: JSONEncoding.default, headers: ["X-API-Key": apiKey])
    .responseJSON { response in
        switch response.result {
        case .success(let value):
            print(value)
        case .failure(let error):
            print("Error:", error)
        }
    }

3. Notes & Best Practices

  • Use async network calls to avoid blocking the main thread.
  • Always protect your API key; do not embed it in client-side code that can be reverse-engineered.
  • Responses include _enterprise metadata when applicable for monitoring and quota tracking.

Other Languages

Explore how to integrate Health Fitness API in other programming languages:

Start integrating Health Fitness API into your apps today!


Health Fitness API Powering modern digital health infrastructure