API Request Setup

Code examples on how to set up your requests after retrieving a token

Code Examples

These examples assume you have followed the steps from Requesting the Token and have received a response from that route.

Python

import requests

response = {
    "access_token": "eyJhbGciOiJSUzI1NiIsI...",
    "expires_in": 86155.034762118,
    "token_type": "Bearer",
    "customer_id": "0d8bb889-36e1-11ee-b9e9-92e504c243a4"
}

headers = {
    "Authorization": f"Bearer {response['access_token']}",
    "Passio-ID": response['customer_id']
}

url = "https://api.passiolife.com/v2/products/nutrition-advisor/..."

r = requests.get(url, headers=headers)
print(r.json())

Go

package main

import (
    "io/ioutil"
    "net/http"
)

func main() {
    response := map[string]interface{}{
        "access_token": "eyJhbGciOiJSUzI1NiIsI...",
        "expires_in": 86155.034762118,
        "token_type": "Bearer",
        "customer_id": "0d8bb889-36e1-11ee-b9e9-92e504c243a4",
    }

    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://api.passiolife.com/v2/products/nutrition-advisor/...", nil)
    req.Header.Add("Authorization", "Bearer "+response["access_token"].(string))
    req.Header.Add("Passio-ID", response["customer_id"].(string))

    resp, _ := client.Do(req)
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    println(string(body))
}

React/JS

import axios from 'axios';
import React, { useEffect } from 'react';

const response = {
    "access_token": "eyJhbGciOiJSUzI1NiIsI...",
    "expires_in": 86155.034762118,
    "token_type": "Bearer",
    "customer_id": "0d8bb889-36e1-11ee-b9e9-92e504c243a4"
};

const fetchData = async () => {
    const headers = {
        Authorization: `Bearer ${response.access_token}`,
        'Passio-ID': response.customer_id
    };
    try {
        const result = await axios.get('https://api.passiolife.com/v2/products/nutrition-advisor/...', { headers });
        console.log(result.data);
    } catch (error) {
        console.error(error);
    }
};

function App() {
    useEffect(() => {
        fetchData();
    }, []);

    return <div>App Component</div>;
}

export default App;

Swift

import Foundation

let response = [
    "access_token": "eyJhbGciOiJSUzI1NiIsI...",
    "expires_in": 86155.034762118,
    "token_type": "Bearer",
    "customer_id": "0d8bb889-36e1-11ee-b9e9-92e504c243a4"
]

guard let url = URL(string: "https://api.passiolife.com/v2/products/nutrition-advisor/...") else { return }

var request = URLRequest(url: url)
request.addValue("Bearer \(response["access_token"]!)", forHTTPHeaderField: "Authorization")
request.addValue(response["customer_id"]!, forHTTPHeaderField: "Passio-ID")

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data, error == nil else {
        print(error?.localizedDescription ?? "No data")
        return
    }
    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
        print("HTTP Status Code: \(httpStatus.statusCode)")
        return
    }
    if let responseString = String(data: data, encoding: .utf8) {
        print(responseString)
    }
}
task.resume()

cURL

curl -H "Authorization: Bearer eyJhbGciOi..." \
     -H "Passio-ID: 0d8bb889-36e1-11ee-b9e9-92e504c243a4" \
     "https://api.passiolife.com/v2/products/nutrition-advisor/..."

Last updated