import os
from crewai import Agent, Task, Crew, Process
from crewai.tools import BaseTool
from l402kit import L402Client
from l402kit.wallets import BlinkWallet
# Set up wallet
wallet = BlinkWallet(api_key=os.environ["BLINK_API_KEY"])
client = L402Client(wallet=wallet, budget_sats=1000)
# Wrap L402Client as a CrewAI tool
class L402FetchTool(BaseTool):
name: str = "l402_fetch"
description: str = "Fetch data from a paid API using Bitcoin Lightning micropayment"
def _run(self, url: str) -> str:
import asyncio
response = asyncio.run(client.get(url))
return response.text
l402_tool = L402FetchTool()
# Define agent with L402 capability
researcher = Agent(
role="API Researcher",
goal="Fetch data from paid APIs and return structured results",
backstory="""You are a research agent with access to premium data APIs.
When an API requires payment (HTTP 402), you use the l402_fetch tool
to pay automatically with Bitcoin Lightning and retrieve the data.
Budget: 1000 sats per session.""",
tools=[l402_tool],
verbose=True
)
# Define task
fetch_task = Task(
description="Fetch Bitcoin price data from https://l402kit.com/api/demo",
expected_output="JSON response with the fetched data",
agent=researcher
)
# Run the crew
crew = Crew(
agents=[researcher],
tasks=[fetch_task],
process=Process.sequential,
verbose=True
)
result = crew.kickoff()
print(result)
print(f"Sats spent: {client.spent_sats}")