Apigee Python Libraries: No class / method for getting API Products

Hello there,

I’ve searched the posts here regarding the ability to have a method to list all the API products of an org with the objective of cross-referencing them with API proxies, but it seems that even though the REST API exists (organizations.apiproducts.list), there is no support in the python libraries: google-cloud-apigee-registry or google-cloud-apihub.

Is there any class / method that I’ve not identified that accomplishes obtaining a list of API products? Has anyone faced this problem and how did you solve it? Direct REST api call?

Thanks,

João

1 Like

Hey @jestevao ,

Thanks for reaching out surrounding this. This is a very good question and I was able to actually create this exact use case via a script. I ran this on my own org and it works just fine.

While this doesn’t directly answer your questions regarding google-cloud-apigee-registry or google-cloud-apihub, I believe this is a viable alternative.

What this script does:

  • It uses standard Google libraries to find your local credentials, so you don’t need to hardcode standard tokens.

  • It calls the Apigee API with a special ?expand=true parameter. It will get all details (including the proxy list) in one single request rather than making dozens of separate calls.

  • Lastly, It loops through that data to match every product with its specific list of proxies.

Script:

import sys
import requests
import google.auth
from google.auth.transport.requests import Request

def fetch_products(org, token):
    """
    Fetches all API products for the org.
    Uses the 'expand=true' query parameter to include nested details (like associated proxies)
    in a single response, avoiding the need for subsequent API calls per product.
    """
    url = f"https://apigee.googleapis.com/v1/organizations/{org}/apiproducts?expand=true"
    
    # Standard OAuth2 Bearer token header
    headers = {"Authorization": f"Bearer {token}"}
    
    resp = requests.get(url, headers=headers)
    resp.raise_for_status() 
    
    # Returns the list of products, or an empty list if the org has none.
    return resp.json().get('apiProduct', [])

def main():
    if len(sys.argv) < 2:
        print(f"Usage: python3 {sys.argv[0]} <org_name>", file=sys.stderr)
        sys.exit(1)

    org = sys.argv[1]

    print(f"Authenticating for org: {org}...")
    
    # Uses Application Default Credentials for authentication.
    creds, _ = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
    creds.refresh(Request()) # Refreshes the token to ensuring it's active before use.

    try:
        products = fetch_products(org, creds.token)
        
        print(f"\nFound {len(products)} products.\n")
        
        # Print a simple formatted table header here,f 
        print(f"{'API PRODUCT':<40} | ASSOCIATED PROXIES")
        print("-" * 80)
        
        for p in products:
            # The 'proxies' key may be missing if no proxies are bound to the product.
            # Use .get() to safely return an empty list in that case.
            proxy_list = p.get('proxies', [])
            
            # Join the proxy list into a readable string, or use a dash if empty.
            proxies_str = ", ".join(proxy_list) if proxy_list else "-"
            
            # Print the row, left-aligning the product name to 40 characters for readability.
            print(f"{p['name']:<40} | {proxies_str}")

    except Exception as e:
        # Catches and displays any auth or API errors cleanly.
        print(f"\nError: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == '__main__':
    main()

How to test this script:

  1. Save the code above into a file named list_products.py

  2. Install dependencies i.e. the Python library: pip install google-auth requests

  3. Make sure you authenticate to allow the script to use your local credentials run: gcloud auth application-default login

  4. Now run the script by replacing YOUR_ORG_NAME with your actual Apigee organization name: python3 list_product.py YOUR_ORG_NAME

3 Likes

To address THIS part of your question, … those libraries are not focused on the Apigee Gateway capability, which is where you would define and save your API Products. So that’s why you don’t see a method or class in those libraries, that allows you to do what you want.

Chris’ answer is a good one! and the pattern he showed will work for .. basically any entity under management in the Apigee Gateway - products, apps, developers, KVMs, proxies, Keystores, resources, and so on…

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.