For the complete documentation index, see llms.txt. This page is also available as Markdown.

Connecting AgentB to Your API (OpenAPI)

Now that you have a basic chat agent, let's give it the ability to interact with an external API. AgentB makes this incredibly easy if you have an OpenAPI (formerly Swagger) specification for your API.

Goal: Create an agent that can fetch data from a public API (the FakeStoreAPI in this case) using tools automatically generated from its OpenAPI spec.

Prerequisites

Step 1: The OpenAPI Specification

For this tutorial, we'll use the public FakeStoreAPI. It has a simple OpenAPI specification available.

  1. Get the Spec: You can view it here: https://fakestoreapi.com/fakestoreapi.json

  2. Save it Locally (Optional but Recommended): For reliability and to avoid network issues during development, save this JSON content into your project.

    • Create a directory, e.g., specs in your project root.

    • Save the JSON content as specs/fakestoreapi.json.

    {
      "swagger": "2.0", // Note: AgentB works best with OpenAPI 3.x, but can often handle Swagger 2.0
      "info": {
        "version": "1.0.0",
        "title": "FakeStore API"
      },
      "host": "fakestoreapi.com",
      "basePath": "/",
      "schemes": ["https"],
      "paths": {
        "/products": {
          "get": {
            "summary": "Get all products",
            "operationId": "getAllProducts",
            // ... more details ...
          }
        },
        "/products/{id}": {
          "get": {
            "summary": "Get a single product",
            "operationId": "getSingleProduct",
            "parameters": [
              {
                "name": "id",
                "in": "path",
                "required": true,
                "description": "ID of the product to retrieve",
                "type": "integer"
              }
            ],
            // ... more details ...
          }
        }
        // ... other paths ...
      }
    }

    Note: While AgentB's OpenAPIConnector primarily targets OpenAPI 3.x, it can often parse and utilize Swagger 2.0 specs like this one. For your own APIs, using OpenAPI 3.x is recommended for best compatibility.

Step 2: Update Your Script

Let's modify our basicChat.ts (or create a new file like apiAgent.ts).

Step 3: Run Your API-Connected Agent

  1. Save apiAgent.ts (and specs/fakestoreapi.json if you saved it).

  2. Compile: npx tsc apiAgent.ts (or your build command).

  3. Run: node apiAgent.js (or npx ts-node apiAgent.ts).

Example Interaction:

Key Takeaways

  • ToolProviderSourceConfig: This configuration object tells AgentB how to create a tool provider.

    • id: A unique name for this source.

    • type: 'openapi': Specifies that the tools come from an OpenAPI spec.

    • openapiConnectorOptions.spec: You provided the loaded JSON spec object directly. You could also use specUrl.

    • openapiConnectorOptions.sourceId: This ID is used internally by OpenAPIConnector and is important for features like dynamic authentication overrides. It should typically match the top-level id of the ToolProviderSourceConfig.

    • toolsetCreationStrategy: Determines how tools from the API are grouped. 'byTag' is common. Since FakeStoreAPI doesn't have rich tags, AgentB might create a single toolset or group by common path prefixes.

  • AgentB.initialize({ toolProviders: [...] }): You registered your API tool provider during initialization.

  • Tool-Related Events: You saw new event types:

    • thread.message.completed (with metadata.tool_calls): Shows the LLM's plan to call specific tools with arguments.

    • agent.tool.execution.started: Indicates a tool is now actually running.

    • agent.tool.execution.completed: Shows the outcome (success/failure and data/error) of the tool execution.

  • Multi-Turn Tool Use: The agent can use tools, get results, and then reason about those results to answer follow-up questions or decide on next steps.

This tutorial demonstrates how AgentB can automatically generate and use tools from an OpenAPI specification, significantly simplifying the process of connecting AI agents to your existing services.

Next Up: Adding the Chat UI (@ulifeai/agentb-ui) to create a web-based interface for your agent.

Last updated