IBM Cloudability

ย View Only

 Fetching cloudability data using API

Jump to  Best Answer
Gines Planas's profile image
Gines Planas posted Thu February 27, 2025 12:57 PM

Dear All,

I am trying to connect to Cloudability using API but I dont know how to make it happen.

I tried with the API key and secret, I was able to get a auth_token, but when calling a list of cost reports (as an example), I was not able to get it, getting an Unauthorized response from the server.

I found some threads here, saying that I should use the Cloudability API Key instead of the Frontdoor one, so I enabled the API Key for Cloudability in Account Settings Preferences, and changed my code to use that API Key instead the Frontdoor one, and then was not able to connect.

Is there any documentation with examples in how we should do it? I didnt find anything that could illustrate it.

Below is my code, in python.

I appreciate any help ;-)

Thanks 

Gines

import requests
import json

# ----------------------- STEP 1: AUTHENTICATION -----------------------
def get_auth_token(api_key, api_secret):
    """Authenticate with Cloudability and return the token."""
    auth_url = "https://frontdoor.apptio.com/service/apikeylogin"
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }
    payload = {
        "keyAccess": api_key,
        "keySecret": api_secret
    }

    response = requests.post(auth_url, json=payload, headers=headers)
    
    # Debug: Print full response details
    print("\n๐Ÿ”น Status Code:", response.status_code)
    print("๐Ÿ”น Response Headers:", response.headers)
    print("๐Ÿ”น Response Body:", response.text)
    
    if response.status_code == 200:
        auth_token = response.headers.get("apptio-opentoken")
        if not auth_token:
            # Fallback: Check if token is provided in the JSON body
            try:
                body = response.json()
                auth_token = body.get("token")
            except Exception as e:
                print("Error parsing JSON:", e)
        if auth_token:
            print("\nโœ… Authentication Successful!")
            print("๐Ÿ”น Token:", auth_token)
            return auth_token
        else:
            print("\nโŒ Authentication Failed! Token not found in response.")
    else:
        print("\nโŒ Authentication Failed! Status Code:", response.status_code)
        print("๐Ÿ”น Response:", response.text)
    return None

# ----------------------- STEP 2: FETCH COST REPORT -----------------------
def get_cost_report(auth_token):
    """Fetch cost report data using the retrieved authentication token."""
    reports_url = "https://api.cloudability.com/v3/reporting/cost/run"

    # Prepare the payload with properly formatted dates (YYYY-MM-DD)
    payload = {
        "start_date": "2024-04-01",  # Replace with the actual beginning date of last quarter
        "end_date": "2024-06-30",    # Replace with the actual end date of last quarter
        "dimensions": ["vendor"],
        "metrics": ["total_adjusted_amortized_cost"]
    }
    
    # Use the token in the Authorization header with Bearer scheme
    headers = {
        "Authorization": f"Bearer {auth_token}",
        "Accept": "application/json",
        "Content-Type": "application/json"
    }
    
    # Disable redirects to ensure the header is not dropped if a redirect occurs
    response = requests.post(reports_url, headers=headers, json=payload, allow_redirects=False)
    
    print("\n๐Ÿ”น Status Code:", response.status_code)
    print("๐Ÿ”น Response Headers:", response.headers)
    
    if response.status_code == 200:
        reports = response.json()
        print("\n๐Ÿ”น Cost Report (Formatted JSON):")
        print(json.dumps(reports, indent=4))
        return reports
    else:
        print("\nโŒ Failed to retrieve cost report!")
        print("๐Ÿ”น Response Body:", response.text)
    return None

# ----------------------- MAIN EXECUTION -----------------------
if __name__ == "__main__":
    # ๐Ÿ” API Credentials (Store these securely in a .env file or environment variables)
    API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXX"
    API_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

    # Step 1: Get the authentication token
    token = get_auth_token(API_KEY, API_SECRET)

    if token:
        # Step 2: Fetch the cost report using the token
        get_cost_report(token)
Jeremy Bryant's profile image
Jeremy Bryant  Best Answer

Hi Gines,

                we use this all the time and authenitcation is via basic authentication passing the API_Key from cloudability as the User name with a blank password. This is the example from Cloudability API documentation

https://api.cloudability.com/v3/budgets -u 'Your_API_Key:'
Varad Rajeev Muthal's profile image
Varad Rajeev Muthal

Hello @Gines Planas Thanks for adding details about the query, I'll get back to you on it!

Varad Rajeev Muthal's profile image
Varad Rajeev Muthal

Hello @Gines Planas if you still facing issue with Cloudability APIs, you can reach out to your Customer Success Manager (CSM) for further technical help.
Thank you!