1

Prerequisite 1: Install xpander-sdk and build

Log in to your Xpander account. - Navigate to the Connectors section and enable few connectors

2

Prerequisite 2: Obtain API Keys for xpander Agent and LLM

Use the workbench to add source node and the connectors from step 1.

Start coding AI Agents

Here’s how to set up your environment:

python
python3 -m venv .venv
source .venv/bin/activate
pip install xpander.sdk
pip freeze > requirements.txt #Optional

Next, create a Python script (agent_example.py):

agent_example.py
import os
from dotenv import load_dotenv  # Optional: Used for loading environment variables from a .env file
from xpander_sdk import XpanderClient, LLMProvider  # SDK for interacting with Xpander services
from openai import OpenAI  # SDK for interacting with OpenAI services
from loguru import logger  # Optional: Logging library for detailed logging

# Load environment variables from a .env file (if available)
load_dotenv()  # Optional

# Initialize the XpanderClient with your agent key and URL
xpanderAPIKey = os.environ.get("XPANDER_API_KEY", "")
client = XpanderClient(
    agent_key=xpanderAPIKey,
    agent_url="https://inbound.xpander.ai/agent/5d<AgentID>4",
    llm_provider=LLMProvider.OPEN_AI
)

# Retrieve available tools for the OpenAI plugin from Xpander
tools = client.tools(llm_provider=LLMProvider.OPEN_AI)

# Initialize the OpenAI client with your API key
OpenAPIKey = os.environ.get("OPENAI_API_KEY", "")
openai_client = OpenAI(api_key=OpenAPIKey)

# Define the messages to be sent to the OpenAI API
messages = [
    {
        "role": "user",
        "content": "Get the last blogpost from ContentDB"
    }
]

# Create a chat completion request using the OpenAI client
response = openai_client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,  # Provide the tools retrieved from Xpander
    tool_choice="required"  # Indicate that tool usage is required
)

# Process the chat completion response with the Xpander client
tool_responses = client.xpander_tool_call(tool_selector_response=response.model_dump())

# Output the responses from the tools
for tool_response in tool_responses:
    print(tool_response.response_message)

Run the Python script:

python agent_example.py

View the response: This will retrieve the latest blog post from your ContentDB database in Notion.