Google Cloud Quotas API Client Library
The `google-cloud-quotas` library is the official Python client for the Google Cloud Quotas API. It allows developers to programmatically list and manage service quotas for their Google Cloud projects. As part of the larger `google-cloud-python` monorepo, it receives updates as new API features are released, typically aligning with the Google Cloud release cycle.
Common errors
-
ModuleNotFoundError: No module named 'google.cloud.quotas_v1'
cause The `google-cloud-quotas` library is not installed or not available in the current Python environment.fixRun `pip install google-cloud-quotas` to install the library. -
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or provide a project in google-cloud-core explicitly.
cause The client library cannot find valid Google Cloud authentication credentials in the environment.fixSet the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of your service account key file, or run `gcloud auth application-default login` for user credentials. -
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.
cause Often caused by an incorrectly formatted `parent` resource name or other request parameters.fixEnsure the `parent` argument is in the format `projects/{project_id}/locations/{location_id}` (e.g., `projects/my-project/locations/global`). Double-check all other parameters for correctness.
Warnings
- gotcha The `google-cloud-quotas` library requires Python 3.9 or newer. Running on older Python versions will lead to installation or runtime errors.
- gotcha Authentication with Google Cloud APIs is a common hurdle. If you encounter `DefaultCredentialsError` or similar authentication failures, your environment credentials are not properly configured.
- gotcha The `parent` argument for `list_quotas` must follow a specific format: `projects/{project_id}/locations/{location_id}`. Incorrect formatting will result in an `InvalidArgument` error.
- gotcha Even with correct authentication, API calls might fail due to insufficient IAM permissions. Listing quotas requires roles like 'Service Usage Viewer' (`serviceusage.quotas.get`).
Install
-
pip install google-cloud-quotas
Imports
- QuotasClient
from google.cloud.quotas import QuotasClient
from google.cloud.quotas_v1 import QuotasClient
Quickstart
import os
from google.cloud.quotas_v1 import QuotasClient
# Ensure GOOGLE_CLOUD_PROJECT environment variable is set or replace with your actual project ID.
# Ensure GOOGLE_APPLICATION_CREDENTIALS points to a service account key file
# or that you have logged in via `gcloud auth application-default login`.
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-project-id")
location = "global" # Quotas are often global or regional for many services
client = QuotasClient()
parent_resource = f"projects/{project_id}/locations/{location}"
try:
print(f"Attempting to list quotas for parent: {parent_resource}")
# Iterate through the first few quotas, as there can be many.
for i, quota in enumerate(client.list_quotas(parent=parent_resource)):
print(f" Quota: {quota.name}, Metric: {quota.quota_metric}, Value: {quota.value}, Limit: {quota.limit}")
if i >= 2: # Print up to 3 quotas
break
except Exception as e:
print(f"An error occurred: {e}")
print("Possible causes: Missing permissions (e.g., serviceusage.quotas.get), incorrect project ID, or authentication issues.")
print("Verify GOOGLE_CLOUD_PROJECT is set correctly and your credentials have the required roles.")