|
|
API Documentation Agent
Author: Venkata Sudhakar
API documentation is often written manually and quickly becomes outdated as code evolves. An API documentation agent reads source code directly, understands the intent of each endpoint, and generates accurate, well-structured documentation that stays in sync with the codebase.
In this tutorial, we build a documentation agent for ShopMax India's internal order management API. The agent reads FastAPI route definitions and produces markdown documentation with endpoint descriptions, parameters, request bodies, response schemas, and curl examples.
The below example shows the agent processing a FastAPI orders endpoint and generating its full documentation.
It gives the following output,
## POST /orders
**Create a new order**
Creates a new order for the authenticated customer, validates stock,
applies loyalty tier discounts, and initiates payment via Razorpay.
**Authentication:** Bearer token required
### Request Body
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| items | array[OrderItem] | Yes | List of products and quantities |
| shipping_address_id | integer | Yes | ID of saved delivery address |
| payment_method | string | Yes | One of: card, upi, cod, emi |
| use_wallet_balance | boolean | No | Apply wallet credits (default: false) |
### Response - 201 Created
Returns OrderResponse with order ID, final amount, and Razorpay payment intent ID.
### Error Codes
| Code | Reason |
|------|--------|
| 400 | One or more items out of stock |
| 422 | Order total exceeds customer credit limit |
| 401 | Missing or invalid authentication token |
### Example
curl -X POST https://api.shopmax.in/orders \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d "{"items": [{"product_id": "LP001", "qty": 2}], "shipping_address_id": 42, "payment_method": "upi"}"
Run this agent over all route files in your FastAPI project and aggregate the output into a single docs/api.md file. Pipe it into tools like Docusaurus or MkDocs for a hosted developer portal. For OpenAPI JSON generation instead of markdown, add 'Output valid OpenAPI 3.1 JSON only' to the system prompt.
|
|