chatbot-app
View on GitHub ↗
API & Agent

REST API Reference

A FastAPI app in api/ exposes the same chat/RAG functionality over HTTP, as an additional entry point alongside the CLI. The CLI is unaffected by it.

Starting the API

Start the API with auto-reload
uvicorn api.main:app --reload --app-dir src

GET /docs serves the automatic Swagger UI.

Endpoints

EndpointAuthDescription
POST /auth/tokenAPI_SECRET in bodyExchanges API_SECRET for a Bearer JWT valid for one hour. Rate limited to 5/min to slow brute-forcing.
POST /chatBearer JWTRuns a standard conversation turn. 20/min per token.
POST /chat/ragBearer JWTRuns a grounded turn and returns retrieved source excerpts. 20/min. Returns 503 if the RAG vector store never initialized.
POST /agentBearer JWTStarts or continues an agent turn until completion or approval is required. 20/min.
POST /agent/{pending_id}/approveBearer JWTExecutes all pending tool calls and resumes the agent.
POST /agent/{pending_id}/rejectBearer JWTRejects the whole pending batch instead of executing it, and resumes without them.
GET /healthNoneReports vector_store: "ready" | "not_initialized". Exempt from rate limiting — it's a monitoring endpoint.

Request validation

session_id is constrained to ^[a-zA-Z0-9_-]{1,64}$. It becomes part of a file path in session_store.py, so this is a mandatory path-traversal guard, not just input hygiene. Messages must contain 1–2,000 characters and cannot be blank. Body shape: {"message": str, "session_id": str}.

Multi-session history

Unlike the CLI's single hardcoded session_id="1", the API keeps one InMemoryChatMessageHistory per session_id per process, lazily loaded from sessions/<session_id>.json on first use and saved back after every turn. /chat and /chat/rag share the same history store keyed by session_id — a session is one continuous conversation regardless of which endpoint served a given turn.

Rate limiting design

/chat, /chat/rag, and the agent routes have no per-route @limiter.limit(...) decorator — deliberately. slowapi's SlowAPIMiddleware (api/rate_limit.py) enforces default_limits at the ASGI layer, before FastAPI resolves Depends(verify_token). A per-route decorator's check only runs inside the endpoint body, so a request with no or invalid token would never reach it and would go completely unrate-limited. /auth/token keeps its own decorator since it has no auth dependency that can fail first.

Example

Authenticate once, then send a chat message
# Exchange the pre-shared secret for a JWT
TOKEN=$(curl --silent --request POST http://127.0.0.1:8000/auth/token \
  --header "Content-Type: application/json" \
  --data '{"api_secret":"replace-with-api-secret"}' \
  | python -c "import json,sys; print(json.load(sys.stdin)['access_token'])")

# Send a chat message
curl --request POST http://127.0.0.1:8000/chat \
  --header "Authorization: Bearer $TOKEN" \
  --header "Content-Type: application/json" \
  --data '{"message":"Hello","session_id":"demo"}'

This authentication scheme is intended for service-to-service use. It identifies one shared service client, not individual users — any valid token holder can address any valid session_id or pending_id.

Next steps