Getting Started¶
This guide takes you from zero to a running API in a few minutes.
0. Configure Environment¶
Create .env in your project root:
OPENAI_API_KEY=
GEMINI_API_KEY=
OPENROUTER_API_KEY=
DEFAULT_PROVIDER=openai
1. Create main.py¶
from agentapi import Agent, AgentApp
app = AgentApp()
agent = Agent(
system_prompt="You are a helpful assistant",
provider="openai",
)
@app.chat("/chat")
async def chat(message: str):
return await agent.run(message)
@app.chat("/stream")
async def stream_chat(message: str):
return agent.stream(message)
2. Run the Server¶
uvicorn main:app --reload
3. Test Endpoints¶
Chat¶
curl -X POST "http://127.0.0.1:8000/chat?message=Hello"
Stream¶
curl -N -X POST "http://127.0.0.1:8000/stream?message=Write%20a%20short%20poem"
4. Explore Interactive Docs¶
- Swagger UI:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
5. Use the CLI (Optional)¶
agentapi new myproject
cd myproject
uvicorn main:app --reload
The scaffold includes:
main.pytools.pyagents.py.env
6. Add Your First Tool¶
from agentapi import tool
@tool(
description="Get the current date in ISO format.",
context="Use when user asks for date/time.",
)
def get_date() -> str:
from datetime import datetime
return datetime.now().isoformat()
Attach it via Agent(..., tools=[get_date]).