# Practical and Useful Patterns with ADK
📜 Got an OpenAPI/Swagger spec? That's all you need to turn APIs into tools — ADK's OpenAPI Tools integrate existing REST APIs into agents with minimal effort.
📌 Title: OpenAPI Tools — Auto-Generating Tools from OpenAPI Specs
🔗 URL:
🧩 Overview
ADK's OpenAPIToolset auto-generates RestApiTool instances from OpenAPI (Swagger) specifications. Each endpoint becomes an agent tool with automatic input validation. Set `auth_scheme` and `auth_credential` once, and authentication is automatically applied to all generated tools — no per-tool auth code needed.
🛠 Usage
Auto-generating tools from an OpenAPI spec.
Import `OpenAPIToolset` from ` and `APIKeyAuth` from `google.adk.auth`. Create the toolset with `OpenAPIToolset(spec_url="", auth_scheme=APIKeyAuth(header_name="X-API-Key"), auth_credential="your-api-key-here")`, which auto-generates tools from the spec with authentication automatically applied to all endpoints. Pass the `toolset` to an `Agent`'s `tools` list to make every API endpoint available as an agent tool.
Using a local OpenAPI spec file:
For local OpenAPI spec files, load the YAML with ` and pass it as `OpenAPIToolset(spec_dict=spec, base_url="")`, using the `spec_dict` parameter for the parsed dictionary and `base_url` to specify the API's base URL.
🏗 Practical Patterns
**Instant Integration of Existing APIs**: If your internal microservices publish OpenAPI specs, they become agent tools without writing code. The spec's `description` fields serve as tool descriptions, so spec quality directly impacts agent accuracy.
To integrate multiple APIs, create separate `OpenAPIToolset` instances for each service -- for example, a user service with `OpenAPIToolset(spec_url="https://user-service.internal/openapi.json", auth_scheme=APIKeyAuth(header_name="Authorization"), auth_credential="Bearer token123")` and an order service with its own spec URL and credentials. Pass both to the `Agent` as `tools=[user_api, order_api]`, allowing per-service access control with different authentication configurations.
**Input Validation**: Automatic validation based on OpenAPI schema definitions (required, type, enum, etc.) prevents invalid API calls.
**Incremental Integration**: Start by tooling only read-only GET endpoints. Verify behavior, then gradually add POST/PUT/DELETE endpoints.
💡 Use Cases
🏢 Agent integration of internal microservices
🛒 E-commerce API tooling (product search, order management)
📊 Natural language queries via data analysis API integration
🔗 Third-party SaaS API integration
⚠️ Caveats
- Insufficient `description` fields in OpenAPI specs prevent the LLM from selecting the right tool. Review spec quality beforehand.
- Never hardcode `auth_credential`. Retrieve from environment variables or Secret Manager.
- Too many endpoints make LLM tool selection inaccurate. Configure the Toolset with only the endpoints you need.
✨ With OpenAPI Tools, your API specs become agent capabilities directly. Make the most of your existing API documentation!
#
ADK# #
AIAgent#