The `google-cloud-service-usage` library provides a Python client for the Google Cloud Service Usage API. It enables programmatic management of Google Cloud services within your projects, including listing available services, checking their status, and enabling or disabling them. Part of the `google-cloud-python` monorepo, it is currently at version 1.16.0. Releases for this library, like others in the monorepo, occur frequently, often several times a month, reflecting updates to the underlying API or client code.
pip install google-cloud-service-usageVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the `ServiceUsageClient` and list all enabled Google Cloud services for a specified project. Ensure your `GOOGLE_CLOUD_PROJECT` environment variable is set or replace the placeholder project ID, and that your environment is authenticated to Google Cloud.
Ensure `GOOGLE_APPLICATION_CREDENTIALS` environment variable points to a service account key, or run `gcloud auth application-default login` for user authentication. For Cloud Run/Functions/Compute Engine, default service accounts are often automatically used.
Grant the authenticated principal appropriate roles for Service Usage. For listing, `Service Usage Consumer` (roles/serviceusage.serviceUsageConsumer) is typically sufficient. For enabling/disabling, `Service Usage Admin` (roles/serviceusage.serviceUsageAdmin) or custom roles are needed.
Always refer to the API documentation for the exact resource name format. For Service Usage, project resources are `projects/PROJECT_ID_OR_NUMBER` and service names are full identifiers like `compute.googleapis.com`.
Call the `.result()` method on the `Operation` object to wait for the operation to complete and retrieve its final status or response. For example: `operation = client.enable_service(...)` then `response = operation.result()`.
Grant the `Service Usage Consumer` role (for listing) or `Service Usage Admin` role (for enabling/disabling) to the principal via the Google Cloud Console IAM page or `gcloud iam roles` commands.
Correct the import statement to `from google.cloud import service_usage_v1` and then instantiate `client = service_usage_v1.ServiceUsageClient()`.
For local development, run `gcloud auth application-default login`. For production, ensure the `GOOGLE_APPLICATION_CREDENTIALS` environment variable points to a valid service account key file path.
Verify the exact service name (e.g., `compute.googleapis.com`, `storage.googleapis.com`) using the Google Cloud Console Service Usage page or by programmatically listing available services.