Skip to content
This repository was archived by the owner on May 1, 2026. It is now read-only.

Commit b611e92

Browse files
committed
docs: Expand README with comprehensive API design details, including endpoints, request/response models, and security features
1 parent 551cdc3 commit b611e92

1 file changed

Lines changed: 174 additions & 2 deletions

File tree

README.md

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,167 @@ Query Results (JSON):
290290
]
291291
```
292292

293+
## API Design
294+
295+
### Overview
296+
297+
The Text2SQL Analytics System exposes a RESTful API built with **FastAPI** that converts natural language queries into SQL and executes them against a PostgreSQL database. The API includes built-in security features, rate limiting, and comprehensive error handling.
298+
299+
### Base URL
300+
301+
```
302+
http://localhost:8000
303+
```
304+
305+
### Authentication & Security
306+
307+
- **Rate Limiting**: 5 requests per 10 seconds per IP address
308+
- **Request Timeout**: Monitored via `X-Process-Time` header
309+
- **SQL Injection Protection**: Built-in query validation and sanitization
310+
- **Error Handling**: Structured error responses with appropriate HTTP status codes
311+
312+
### Endpoints
313+
314+
#### 1. Health Check
315+
316+
**GET** `/`
317+
318+
Returns the API health status.
319+
320+
**Response:**
321+
322+
```json
323+
{
324+
"status": "ok",
325+
"message": "Text2SQL API running."
326+
}
327+
```
328+
329+
#### 2. Generate and Execute SQL
330+
331+
**POST** `/generate-sql`
332+
333+
Converts natural language to SQL, validates the query, and executes it against the database.
334+
335+
**Request Body:**
336+
337+
```json
338+
{
339+
"question": "Show all orders shipped in 1997"
340+
}
341+
```
342+
343+
**Response Schema:**
344+
345+
```json
346+
{
347+
"sql_query": "string", // Raw SQL generated by Gemini
348+
"sanitized_query": "string", // SQL after sanitization
349+
"validate_query": "string", // Final validated SQL
350+
"result_json": "string" // Query results as JSON string
351+
}
352+
```
353+
354+
**Example Request:**
355+
356+
```bash
357+
curl -X POST "http://localhost:8000/generate-sql" \
358+
-H "Content-Type: application/json" \
359+
-d '{"question": "Find all customers from Germany"}'
360+
```
361+
362+
**Example Response:**
363+
364+
```json
365+
{
366+
"sql_query": "SELECT * FROM customers WHERE country = 'Germany';",
367+
"sanitized_query": "SELECT * FROM customers WHERE country = 'Germany'",
368+
"validate_query": "SELECT * FROM customers WHERE country = 'Germany'",
369+
"result_json": "[{\"customer_id\":1,\"company_name\":\"Alfreds Futterkiste\",\"country\":\"Germany\"}]"
370+
}
371+
```
372+
373+
### Error Responses
374+
375+
#### 400 Bad Request
376+
377+
```json
378+
{
379+
"detail": "Validation error: Invalid SQL syntax"
380+
}
381+
```
382+
383+
#### 429 Too Many Requests
384+
385+
```json
386+
{
387+
"message": "Too many requests"
388+
}
389+
```
390+
391+
#### 500 Internal Server Error
392+
393+
```json
394+
{
395+
"detail": "Internal error: Database connection failed"
396+
}
397+
```
398+
399+
### Request/Response Models
400+
401+
**Text2SQLRequest:**
402+
403+
- `question` (string, required): Natural language query
404+
- Default: "Show all orders shipped in 1997"
405+
406+
**SQLResponseModel:**
407+
408+
- `sql_query` (string): Original SQL generated by Gemini API
409+
- `sanitized_query` (string): SQL after sanitization process
410+
- `validate_query` (string): Final validated SQL ready for execution
411+
- `result_json` (string): Query results serialized as JSON
412+
413+
### API Features
414+
415+
#### Middleware Stack
416+
417+
1. **Process Time Tracking**: Adds `X-Process-Time` header to all responses
418+
2. **Rate Limiting**: IP-based request limiting (5 req/10sec)
419+
3. **CORS**: Cross-origin resource sharing support
420+
421+
#### Query Processing Pipeline
422+
423+
1. **Natural Language Input**: User provides English question
424+
2. **Prompt Engineering**: Question converted to optimized prompt
425+
3. **SQL Generation**: Gemini API generates SQL query
426+
4. **Sanitization**: Remove dangerous SQL constructs
427+
5. **Validation**: Ensure SQL syntax and safety
428+
6. **Execution**: Run validated query against PostgreSQL
429+
7. **Serialization**: Convert results to JSON format
430+
431+
#### Supported Query Types
432+
433+
- **SELECT queries**: Data retrieval operations
434+
- **Aggregations**: COUNT, SUM, AVG, MIN, MAX
435+
- **Joins**: Inner, left, right joins across tables
436+
- **Filtering**: WHERE clauses with various conditions
437+
- **Grouping**: GROUP BY with HAVING clauses
438+
- **Sorting**: ORDER BY operations
439+
440+
#### Blocked Operations
441+
442+
- INSERT, UPDATE, DELETE statements
443+
- DROP, ALTER, CREATE statements
444+
- System function calls
445+
- Subqueries with potential security risks
446+
447+
### Interactive Documentation
448+
449+
FastAPI automatically generates interactive API documentation:
450+
451+
- **Swagger UI**: [http://localhost:8000/docs](http://localhost:8000/docs)
452+
- **ReDoc**: [http://localhost:8000/redoc](http://localhost:8000/redoc)
453+
293454
## Running the FastAPI Application
294455

295456
To start the development server with hot-reload, run:
@@ -298,8 +459,19 @@ To start the development server with hot-reload, run:
298459
uvicorn src.main:app --reload
299460
```
300461

301-
The API will be available at [http://localhost:8000](http://localhost:8000).
302-
Access the automatically generated interactive documentation at [http://localhost:8000/docs](http://localhost:8000/docs) or the alternative ReDoc at [http://localhost:8000/redoc](http://localhost:8000/redoc).
462+
The API will be available at [http://localhost:8000](http://localhost:8000).
463+
464+
### Production Deployment
465+
466+
For production deployment, use:
467+
468+
```bash
469+
# With specific host and port
470+
uvicorn src.main:app --host 0.0.0.0 --port 8000
471+
472+
# With multiple workers
473+
uvicorn src.main:app --host 0.0.0.0 --port 8000 --workers 4
474+
```
303475

304476
## Testing
305477

0 commit comments

Comments
 (0)