Overview
Encountering the "Usage export not found" error while using the CircleCI API can be frustrating. This issue often arises due to incorrect API requests or missing parameters. This guide provides a step-by-step solution to successfully create and retrieve a usage export report using the CircleCI API.
Prerequisites
- A valid CircleCI personal API token.
- The organization ID for which you want to generate the usage report.
- Python 3 installed on your system.
Instructions
-
Set Up Your Environment:
- Ensure you have Python 3 installed.
- Obtain your CircleCI personal API token from your user settings.
- Identify your organization ID from CircleCI.
-
Create a Usage Export:
- Use the following Python script to initiate a usage export job. Replace the placeholders with your actual values.
import http.client import json import time ORG="" CIRCLE_TOKEN="" #YEAR-MONTH-DATETHOUR:MINUTE:SECONDZ format #Example: 2024-12-24T14:15:22Z START_DATE="" #YEAR-MONTH-DATETHOUR:MINUTE:SECONDZ format #Example: 2025-01-07T14:15:22Z END_DATE="" conn = http.client.HTTPSConnection("circleci.com") payload = json.dumps({ "start": f"{START_DATE}", "end": f"{END_DATE}", "shared_org_ids": [ORG] }) headers = { "Content-Type": "application/json", "Circle-Token": CIRCLE_TOKEN } conn.request("POST", f"/api/v2/organizations/{ORG}/usage_export_job", payload, headers) res = conn.getresponse() data = res.read() response_data = json.loads(data.decode("utf-8")) usage_export_job_id = response_data.get("usage_export_job_id") print("Initial Response:", response_data) conn.close() if usage_export_job_id: conn = http.client.HTTPSConnection("circleci.com") while True: conn.request("GET", f"/api/v2/organizations/{ORG}/usage_export_job/{usage_export_job_id}", headers=headers) res = conn.getresponse() data = res.read() job_status = json.loads(data.decode("utf-8")) print("Current Status:", job_status) state = job_status.get("state") if state == "completed": print("Job has completed successfully.") break elif state == "failed": print("Job has failed.") break else: print("Job is still processing...") time.sleep(10) conn.close() else: print("Error: usage_export_job_id not found in response.")
Solution
Ensure that the usage_export_job_id
is correctly captured and used in subsequent API requests. The script provided will loop until the job is completed, printing the status and eventually the download URL for the usage report.
Comments
Article is closed for comments.