Skip to content

Apigee API Hub tools for ADK

Supported in ADKPython v0.1.0

ApiHubToolset lets you turn any documented API from Apigee API hub into a tool with a few lines of code. This section shows you the step by step instructions including setting up authentication for a secure connection to your APIs.

Prerequisites

  1. Install ADK
  2. Install the Google Cloud CLI.
  3. Apigee API hub instance with documented (i.e. OpenAPI spec) APIs
  4. Set up your project structure and create required files
project_root_folder
 |
 `-- my_agent
     |-- .env
     |-- __init__.py
     |-- agent.py
     `__ tool.py

Create an API Hub Toolset

Note: This tutorial includes an agent creation. If you already have an agent, you only need to follow a subset of these steps.

  1. Get your access token, so that APIHubToolset can fetch spec from API Hub API. In your terminal run the following command

    gcloud auth print-access-token
    # Prints your access token like 'ya29....'
    
  2. Ensure that the account used has the required permissions. You can use the pre-defined role roles/apihub.viewer or assign the following permissions:

    1. apihub.specs.get (required)
    2. apihub.apis.get (optional)
    3. apihub.apis.list (optional)
    4. apihub.versions.get (optional)
    5. apihub.versions.list (optional)
    6. apihub.specs.list (optional)
  3. Create a tool with APIHubToolset. Add the below to tools.py

    If your API requires authentication, you must configure authentication for the tool. The following code sample demonstrates how to configure an API key. ADK supports token based auth (API Key, Bearer token), service account, and OpenID Connect. We will soon add support for various OAuth2 flows.

    from google.adk.tools.openapi_tool.auth.auth_helpers import token_to_scheme_credential
    from google.adk.tools.apihub_tool.apihub_toolset import APIHubToolset
    
    # Provide authentication for your APIs. Not required if your APIs don't required authentication.
    auth_scheme, auth_credential = token_to_scheme_credential(
        "apikey", "query", "apikey", apikey_credential_str
    )
    
    sample_toolset = APIHubToolset(
        name="apihub-sample-tool",
        description="Sample Tool",
        access_token="...",  # Copy your access token generated in step 1
        apihub_resource_name="...", # API Hub resource name
        auth_scheme=auth_scheme,
        auth_credential=auth_credential,
    )
    

    For production deployment we recommend using a service account instead of an access token. In the code snippet above, use service_account_json=service_account_cred_json_str and provide your security account credentials instead of the token.

    For apihub_resource_name, if you know the specific ID of the OpenAPI Spec being used for your API, use `projects/my-project-id/locations/us-west1/apis/my-api-id/versions/version-id/specs/spec-id`. If you would like the Toolset to automatically pull the first available spec from the API, use `projects/my-project-id/locations/us-west1/apis/my-api-id`

  4. Create your agent file Agent.py and add the created tools to your agent definition:

    from google.adk.agents.llm_agent import LlmAgent
    from .tools import sample_toolset
    
    root_agent = LlmAgent(
        model='gemini-2.0-flash',
        name='enterprise_assistant',
        instruction='Help user, leverage the tools you have access to',
        tools=sample_toolset.get_tools(),
    )
    
  5. Configure your __init__.py to expose your agent

    from . import agent
    
  6. Start the Google ADK Web UI and try your agent:

    # make sure to run `adk web` from your project_root_folder
    adk web
    

Then go to http://localhost:8000 to try your agent from the Web UI.