Back to agents
Agent #7
š API Endpoint Generation Agent
This agent generates REST API endpoints from requirements, database schemas, or OpenAPI specs. It creates routes, validation, error handling, and documentation automatically. python 07_api_generation.py /path/to/schema.sql python 07_api_generation.py "Create CRUD API for users table"
Key Features
- āREST endpoint design
- āOpenAPI documentation
- āInput validation
- āError handling
- āAuthentication patterns
Implementation
Download .py07_api_generation.py
#!/usr/bin/env python3
"""
API Endpoint Generation Agent
This agent generates REST API endpoints from requirements, database schemas,
or OpenAPI specs. It creates routes, validation, error handling, and
documentation automatically.
Usage:
python 07_api_generation.py /path/to/schema.sql
python 07_api_generation.py "Create CRUD API for users table"
"""
import sys
from anthropic import Anthropic
def main():
if len(sys.argv) < 2:
print("Usage: python 07_api_generation.py <schema_or_requirement>")
sys.exit(1)
target = " ".join(sys.argv[1:])
client = Anthropic()
system_prompt = """You are an API design expert. Your job is to:
1. Analyze requirements or schemas
2. Design clean, RESTful endpoints
3. Generate complete route implementations
4. Include validation and error handling
5. Add authentication/authorization patterns
6. Generate OpenAPI documentation
7. Create test cases for endpoints
Follow REST best practices and include proper HTTP status codes."""
task = f"""Generate a complete REST API for:
{target}
Create:
1. Complete endpoint definitions with:
- HTTP method (GET, POST, PUT, DELETE)
- Route paths
- Request/response schemas
- Status codes
2. Full implementation code (Express.js/FastAPI) with:
- Route handlers
- Input validation
- Error handling
- Database queries
- Proper HTTP responses
3. Authentication/authorization middleware
4. Request/response examples
5. Error handling strategy
6. OpenAPI/Swagger documentation
7. Integration test cases
Include comments explaining each endpoint."""
print("š Starting API Generation Agent...\n")
print("=" * 60)
try:
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=3500,
system=system_prompt,
messages=[{"role": "user", "content": task}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n" + "=" * 60)
print("\nā
API generation complete!")
except Exception as e:
print(f"\nā Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
How to Use
This agent is designed to be run from the command line:
python 07_api_generation.py <input>The agent will stream its response to the console as it works through the task. Use Ctrl+C to interrupt at any time.
Quick Download
07_api_generation.pyFile
07_api_generation.py
Language
Python 3.8+
Dependencies
anthropic
Setup
- 1. Install:
pip install anthropic - 2. Download the .py file
- 3. Set ANTHROPIC_API_KEY
- 4. Run:
python 07_api_generation.py