No project description provided
Project description
Agentopia

- Add superpowers to your AI agent. Our SDK empowers AI agents to discover and pay for services, products, and other agents on demand—seamlessly and autonomously.
- Devs, you can easily:
- Add new capabilities to your AI agent 10x faster
- Discover and pay for services, APIs, data set or infra your AI agent needs
- Pay with USDC automatically, no credit cards or bank account needed
- Build your own services to sell to AI agents
- Remix existing services into new offerings
- Skip lengthy approval processes, no permissions required
- Start earning revenue immediately
Contents
Install the SDK
pip install agentopia
Use a service on Agentopia
Search and use services on Agentopia Marketplace. You can can pay for services with USDC on Base blockchain, with usage-based pricing.
Fund your Agentopia Wallet
from agentopia import Agentopia
agentopia_client = Agentopia(private_key="your_private_key")
agentopia_client.deposit(amount=1000_000)
This will initiate a deposit of 1 USDC (the values used in the SDK are in micro USDC) request which will be processed in a few minutes on the base blockchain.
Use a service
from agentopia import Agentopia
agentopia_client = Agentopia(private_key="your_private_key")
response = agentopia_client.service.execute_via_proxy(
service_slug="hello-world", endpoint_path="hello_world", method="GET"
)
print(response)
Sell your service on Agentopia
Build and sell API/data services on the Agentopia Marketplace. AI agents and users can pay for your service with USDC on Base blockchain.
Below are some example services using the Agentopia SDK.
Hello World Service
You can enable agentopia payments in your endpoints simply by adding the @payable decorator and returning the response with the X-Usdc-Used header.
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from agentopia import payable
app = FastAPI()
@app.get("/hello_world")
@payable(
hold_amount=100000, hold_expires_in=3600
) # Hold 0.1 USDC in the users account for 1hr
async def hello_world(
request: Request,
):
print("Executing hello_world endpoint")
# Execute the service and charge the user 0.000_001 USDC
print("Preparing response with 0.000001 USDC charge")
response = JSONResponse(
content={"message": "Hello from Agentopia!"}, headers={"X-Usdc-Used": "1"}
)
print("Returning response")
return response
Checkout the repo: Hello World Service
Open Router Service
For more flexibility, you can use the Agentopia client to charge the user according to your app's dynamic requirements.
import json
import os
import httpx
import requests
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import JSONResponse, StreamingResponse
from agentopia import Agentopia # type: ignore
app = FastAPI()
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
if not OPENROUTER_API_KEY:
raise Exception("OPENROUTER_API_KEY environment variable not set")
@app.post("/chat/completions")
async def chat_completions(request: Request):
print("Starting payable stream wrapper")
# Get hold ID from header
x_hold_id = request.headers.get("X-Hold-Id")
if x_hold_id is None:
print("No hold ID provided, raising 402 error")
raise HTTPException(
status_code=status.HTTP_402_PAYMENT_REQUIRED,
detail="A Agentopia `X-Hold-Id` header is required",
)
print(f"Received hold ID: {x_hold_id}")
# Create client instance
print("Creating Agentopia client")
agentopia_client = Agentopia()
# Verify hold using hold manager
print(f"Verifying hold {x_hold_id}")
try:
x_hold = agentopia_client.hold.get(x_hold_id)
print(f"Hold verification successful: {x_hold}")
except requests.exceptions.HTTPError:
print("Hold verification failed")
raise HTTPException(
status_code=status.HTTP_402_PAYMENT_REQUIRED,
detail="Invalid hold ID",
)
try:
# Get the request body
body = await request.json()
# Forward the request to OpenRouter
async with httpx.AsyncClient() as client:
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"HTTP-Referer": "https://agentopia.xyz",
"X-Title": "Agentopia LLM Service",
}
# If streaming is enabled, stream the response
if body.get("stream", False):
response = await client.post(
"https://openrouter.ai/api/v1/chat/completions",
json=body,
headers=headers,
follow_redirects=True,
timeout=30.0,
)
async def generate():
try:
async for line in response.aiter_lines():
if line:
# Skip empty lines and "OPENROUTER PROCESSING" messages
if (
line.strip() == ""
or "OPENROUTER PROCESSING" in line
):
continue
# Handle data: prefix
if line.startswith("data: "):
line = line[6:] # Remove "data: " prefix
try:
parsed_line = json.loads(line)
if "error" in parsed_line:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=parsed_line["error"],
)
# Add back the "data: " prefix for SSE format
yield f"data: {json.dumps(parsed_line)}\n\n"
except json.JSONDecodeError:
print(f"JSONDecodeError on line: {line}")
continue
yield "data: [DONE]\n\n"
finally:
# Release hold and charge user using hold manager
print(f"Releasing hold {x_hold_id} with amount 1000")
try:
agentopia_client.hold.release(hold_id=x_hold_id, amount=1000)
print("Hold released successfully")
except requests.exceptions.HTTPError:
print("Failed to release hold")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to process payment",
)
return StreamingResponse(
generate(),
media_type="text/event-stream",
headers={
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Usdc-Used": "1000",
},
)
# For non-streaming requests
response = await client.post(
"https://openrouter.ai/api/v1/chat/completions",
json=body,
headers=headers,
)
# Release hold and charge user
try:
agentopia_client.hold.release(hold_id=x_hold_id, amount=1000)
print("Hold released successfully")
except requests.exceptions.HTTPError:
print("Failed to release hold")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to process payment",
)
return JSONResponse(
content=response.json(), headers={"X-Usdc-Used": "1000"}
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
)
Register your service
You need to register your service on Agentopia Marketplace to make it available for users to use.
from agentopia import Agentopia, AgentopiaServiceModel
agentopia_client = Agentopia(private_key="your_private_key")
service: AgentopiaServiceModel = agentopia_client.service.register(
name="Hello World",
description="A simple hello world service",
base_url="http://api.hello.world",
slug="hello-world-1234",
initial_hold_amount=100000, # $0.1 USDC
initial_hold_expires_in=3600, # 1 hour
api_schema_url="http://api.hello.world/openapi.json",
)
Withdraw earnings from Agentopia Wallet
Get your balance
from agentopia import Agentopia
agentopia_client = Agentopia(private_key="your_private_key")
balance = agentopia_client.get_balance()
Withdraw your balance
from agentopia import Agentopia
agentopia_client = Agentopia(private_key="your_private_key")
agentopia_client.withdraw(amount=100000)
This will initiate a withdrawal request which will be processed in a few minutes on the base blockchain.
Contact Us
If you have any questions or feedback, please contact us at
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agentopia-0.1.6.tar.gz.
File metadata
- Download URL: agentopia-0.1.6.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.8.0-49-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3094c144f29f297747a665a8d713eb5bf18f5720e140f8389465e1383992187
|
|
| MD5 |
787ff8cdade14742f28ce2611c6cf02b
|
|
| BLAKE2b-256 |
0149dd419e0e9d1667a26fbc3ddc573d408fbd4f05b282bfda37bcd8027e2957
|
File details
Details for the file agentopia-0.1.6-py3-none-any.whl.
File metadata
- Download URL: agentopia-0.1.6-py3-none-any.whl
- Upload date:
- Size: 24.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.8.0-49-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b93c3f66926dfb6863428132060d7cbb22af5e1accef243958de85214a230c5
|
|
| MD5 |
6664812447fd627342fbb2c995cc5f74
|
|
| BLAKE2b-256 |
b7106c6699906cb117d89f226ea0ecf9aa207be9439a9bdd9676caba27d2d05c
|