How to Fix 'API Not Enabled' Error in GCP Quickly
The
API not enabled error in GCP happens when your project tries to use a service API that is not activated. To fix it, go to the Google Cloud Console, find the API library, and enable the required API for your project.Why This Happens
This error occurs because your Google Cloud project has not activated the specific API your code or service is trying to use. Each API in GCP must be explicitly enabled before use to control access and billing.
python
import googleapiclient.discovery service = googleapiclient.discovery.build('compute', 'v1') request = service.instances().list(project='my-project', zone='us-central1-a') response = request.execute()
Output
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://compute.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances?alt=json returned "API has not been used in project before or it is disabled.">
The Fix
To fix this, open the Google Cloud Console, navigate to APIs & Services > Library, search for the API you need (e.g., Compute Engine API), and click Enable. This activates the API for your project, allowing your code to access it.
python
import googleapiclient.discovery service = googleapiclient.discovery.build('compute', 'v1') request = service.instances().list(project='my-project', zone='us-central1-a') response = request.execute() print(response)
Output
{'items': [...], 'kind': 'compute#instanceList', 'selfLink': 'https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances'}
Prevention
Always check and enable required APIs before deploying your application. Use Infrastructure as Code tools like Terraform to automate API enabling. Regularly review your project’s enabled APIs to avoid surprises.
Related Errors
- Permission Denied: Ensure your service account has the right roles.
- Quota Exceeded: Check API usage limits and request increases if needed.
Key Takeaways
Enable the required API in Google Cloud Console before using it in your project.
API not enabled errors happen because GCP requires explicit activation of each API.
Automate API enabling with Infrastructure as Code to avoid manual errors.
Check service account permissions if you face access issues after enabling APIs.
Review enabled APIs regularly to keep your project clean and secure.