3

I want to check for a project whether the "API and services" are enabled or not via an API call.

2 Answers2

3

You can list enabled and disabled Apis and services by calling the services.list method using the curl command.

If you want to list enabled APIs and services, run the following:

gcurl https://serviceusage.googleapis.com/v1/projects/[Project_Number]/services?filter=state:ENABLED

To list the disabled APIs and services, run:

gcurl https://serviceusage.googleapis.com/v1/projects/[Project_Number]/services?filter=state:DISABLED

You can read more about the different ways of checking APIs and services here. You can also learn more about the "services.list" method here as well.

2

If you'd like to check whether a particular API is enabled using the Python API, this snippet of code should help:

from googleapiclient import discovery

project = "your-project-id"
api_name = "compute" # API name

service = discovery.build('serviceusage', 'v1')
request = service.services().get(
    name=f"projects/{project}/services/{api_name}.googleapis.com"
)
response = request.execute()

if response.get('state') == 'DISABLED':
    # Do something
KJH
  • 402
  • 2
  • 14
  • Don't you need to call (nice to have) `service.close()` near the end according to https://github.com/googleapis/google-api-python-client/blob/main/docs/start.md#build-the-service-object? – Vincent Yin Apr 18 '23 at 14:40
  • 1
    @VincentYin - sure. This was just a code fragment to show how. I wasn't thinking about wrapping it all up cleanly in this snippet. – KJH Apr 18 '23 at 18:05
  • Thanks for the answer. I also just encountered and then wrote up this quirk: https://stackoverflow.com/a/76047799/13676217 – Vincent Yin Apr 18 '23 at 18:10