Spaces:
Runtime error
Runtime error
File size: 12,532 Bytes
5a65404 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | # ERP to CDF Transformation API Documentation
## Overview
The ERP to CDF Transformation API v2.0 provides high-performance data transformation capabilities using Polars and DuckDB. It converts data from various ERP systems into a standardized Common Data Frame (CDF) format.
**Base URL**: `http://localhost:8000`
**API Version**: 2.0.0
**Engine**: Polars + DuckDB
## Features
- ✅ **11 ERP Systems**: Oracle Fusion, SAP ERP, Microsoft Dynamics, NetSuite, Workday, Infor M3/LN, Epicor Kinetic, QAD ERP, IFS Cloud, Sage Intacct
- ✅ **5 Entity Types**: Sales Orders, Customers, Invoices, Purchase Orders, Inventory Items
- ✅ **Multiple File Formats**: CSV, Excel, JSON, SQL, Parquet
- ✅ **Auto-Detection**: Intelligent ERP system and entity type recognition
- ✅ **Data Format Consistency**: ISO dates, DECIMAL(18,2) precision, proper boolean/text handling
- ✅ **High Performance**: Lightning-fast processing with Polars + DuckDB
## Authentication
Currently, the API does not require authentication. For production deployments, implement appropriate authentication mechanisms.
## Interactive Documentation
- **Swagger UI**: `http://localhost:8000/docs`
- **ReDoc**: `http://localhost:8000/redoc`
## Endpoints
### Health & Information
#### GET /health
Check API health status.
**Response:**
```json
{
"status": "healthy",
"version": "2.0.0",
"engine": "Polars + DuckDB"
}
```
#### GET /entities
Get list of supported entity types.
**Response:**
```json
[
"sales_orders",
"customers",
"invoices",
"purchase_orders",
"inventory_items"
]
```
#### GET /erps
Get list of supported ERP systems.
**Response:**
```json
[
"Oracle Fusion",
"SAP ERP",
"Microsoft Dynamics",
"NetSuite",
"Workday",
"Infor M3",
"Infor LN",
"Epicor Kinetic",
"QAD ERP",
"IFS Cloud",
"Sage Intacct"
]
```
#### GET /formats
Get list of supported file formats.
**Response:**
```json
[
"csv",
"excel",
"json",
"sql",
"parquet"
]
```
### Core Transformation
#### POST /transform
Transform JSON data to CDF format.
**Request Body:**
```json
{
"auto_select_erp": true,
"auto_select_entity": true,
"entity": "sales_orders",
"erp": "NetSuite",
"data": [
{
"tranId": "SO1001",
"trandate": "2025-08-04",
"entity": "CUST01",
"total": 1249.0
}
]
}
```
**Response:**
```json
{
"success": true,
"data": [
{
"order_id": "SO1001",
"order_date": "2025-08-04",
"customer_id": "CUST01",
"total_amount": 1249.0,
"status": null,
"currency": null,
"notes": null,
"item_id": null,
"description": null,
"quantity": null,
"unit_price": null,
"line_total": null,
"tax_code": null
}
],
"detected_erp": "NetSuite",
"detected_entity": "sales_orders",
"transformation_mode": "template",
"confidence_score": 1.0,
"message": "Successfully transformed using template mapping",
"column_mappings": {
"tranId": "order_id",
"trandate": "order_date",
"entity": "customer_id",
"total": "total_amount"
},
"row_count": 1,
"processing_time_ms": 45.2
}
```
#### POST /transform/file
Transform uploaded file to CDF format.
**Parameters:**
- `file` (form-data): File to transform
- `auto_select_erp` (form-data): Auto-detect ERP system (default: true)
- `auto_select_entity` (form-data): Auto-detect entity type (default: true)
- `entity` (form-data): Entity type if known
- `erp` (form-data): ERP system if known
- `output_format` (form-data): Output format (default: json)
**Response:** Same as `/transform` endpoint
**cURL Example:**
```bash
curl -X POST "http://localhost:8000/transform/file" \
-F "file=@sample.csv" \
-F "auto_select_erp=true" \
-F "auto_select_entity=true"
```
#### POST /transform/file/download
Transform uploaded file and return as downloadable file.
**Parameters:** Same as `/transform/file`
**Response:** File download with transformed data
**cURL Example:**
```bash
curl -X POST "http://localhost:8000/transform/file/download" \
-F "file=@sample.csv" \
-F "output_format=csv" \
-o transformed_data.csv
```
### Analysis & Validation
#### POST /analyze
Analyze uploaded file and provide insights.
**Parameters:**
- `file` (form-data): File to analyze
**Response:**
```json
{
"column_info": {
"column_count": 4,
"row_count": 100,
"columns": ["tranId", "trandate", "entity", "total"],
"dtypes": {
"tranId": "String",
"trandate": "String",
"entity": "String",
"total": "Float64"
},
"null_counts": {
"tranId": 0,
"trandate": 0,
"entity": 0,
"total": 2
},
"sample_data": {
"tranId": ["SO1001", "SO1002", "SO1003"],
"trandate": ["2025-08-04", "2025-08-04", "2025-08-05"],
"entity": ["CUST01", "CUST02", "CUST01"],
"total": [1249.0, 850.5, 2100.0]
}
},
"erp_entity_suggestions": [
{
"entity": "sales_orders",
"erp": "NetSuite",
"confidence": 0.85,
"above_threshold": true
},
{
"entity": "sales_orders",
"erp": "Oracle Fusion",
"confidence": 0.65,
"above_threshold": true
}
],
"available_entities": ["sales_orders", "customers", "invoices", "purchase_orders", "inventory_items"],
"available_erps": ["Oracle Fusion", "SAP ERP", "Microsoft Dynamics", "NetSuite", "Workday", "Infor M3", "Infor LN", "Epicor Kinetic", "QAD ERP", "IFS Cloud", "Sage Intacct"],
"processing_time_ms": 23.1
}
```
#### POST /validate
Validate file structure and content.
**Parameters:**
- `file` (form-data): File to validate
**Response:**
```json
{
"valid": true,
"file_format": "csv",
"column_info": {
"column_count": 4,
"row_count": 100,
"columns": ["tranId", "trandate", "entity", "total"],
"dtypes": {
"tranId": "String",
"trandate": "String",
"entity": "String",
"total": "Float64"
},
"null_counts": {
"tranId": 0,
"trandate": 0,
"entity": 0,
"total": 2
}
},
"message": "File is valid and can be processed"
}
```
### Template Information
#### GET /template/erp/{erp}/entity/{entity}
Get template mapping for specific ERP and entity.
**Parameters:**
- `erp`: ERP system name
- `entity`: Entity type
**Example:** `GET /template/erp/NetSuite/entity/sales_orders`
**Response:**
```json
{
"erp": "NetSuite",
"entity": "sales_orders",
"mapping": {
"tranId": "order_id",
"trandate": "order_date",
"entity": "customer_id",
"total": "total_amount",
"status": "status",
"currency": "currency",
"memo": "notes"
},
"cdf_schema": [
"order_id",
"order_date",
"customer_id",
"status",
"currency",
"total_amount",
"notes",
"item_id",
"description",
"quantity",
"unit_price",
"line_total",
"tax_code"
]
}
```
#### GET /template/entity/{entity}/schema
Get CDF schema for specific entity.
**Parameters:**
- `entity`: Entity type
**Example:** `GET /template/entity/customers/schema`
**Response:**
```json
{
"entity": "customers",
"cdf_schema": [
"customer_id",
"customer_name",
"contact_email",
"phone_number",
"alt_phone",
"is_company",
"status",
"currency",
"created_date",
"last_modified",
"address_type",
"address_line1",
"city",
"state",
"postal_code",
"country"
],
"field_count": 16
}
```
## Data Models
### Entity Types
| Entity | Description | CDF Fields |
|--------|-------------|------------|
| `sales_orders` | Sales transactions with line items | 13 fields |
| `customers` | Customer master data with full address support | 16 fields |
| `invoices` | Invoice documents | 14 fields |
| `purchase_orders` | Purchase orders | 14 fields |
| `inventory_items` | Product catalog | 12 fields |
### CDF Schema Details
#### Sales Orders (13 fields)
- `order_id`, `order_date`, `customer_id`, `status`, `currency`
- `total_amount`, `notes`, `item_id`, `description`, `quantity`
- `unit_price`, `line_total`, `tax_code`
#### Customers (16 fields)
- **Core**: `customer_id`, `customer_name`, `contact_email`, `phone_number`, `alt_phone`
- **Business**: `is_company`, `status`, `currency`, `created_date`, `last_modified`
- **Address**: `address_type`, `address_line1`, `city`, `state`, `postal_code`, `country`
#### Invoices (14 fields)
- `invoice_id`, `invoice_date`, `customer_id`, `status`, `currency`
- `total_amount`, `due_date`, `notes`, `item_id`, `description`
- `quantity`, `unit_price`, `line_total`, `tax_code`
#### Purchase Orders (14 fields)
- `po_id`, `po_date`, `vendor_id`, `status`, `currency`
- `total_amount`, `expected_receipt_date`, `notes`, `item_id`, `description`
- `quantity`, `unit_price`, `line_total`, `tax_code`
#### Inventory Items (12 fields)
- `item_id`, `item_name`, `description`, `purchase_description`, `sales_description`
- `base_price`, `cost_price`, `unit_type`, `is_active`, `quantity_on_hand`
- `reorder_point`, `preferred_vendor_id`
## Data Format Consistency
### Date Fields
- **APIs/Database**: ISO `yyyy-MM-dd` format
- **Human-readable files**: `dd-MMMM-yyyy` format (e.g., `04-August-2025`)
### Decimal Fields
- **Precision**: DECIMAL(18,2) for all currency and pricing fields
- **Format**: Always 2 decimal places (e.g., `1249.00`)
### Boolean Fields
- **JSON/API**: `true`/`false`
- **Database**: `1`/`0`
### Text Fields
- **Encoding**: UTF-8 with proper CSV/JSON escaping
- **Processing**: Trimmed and normalized
## Error Handling
### HTTP Status Codes
| Code | Description |
|------|-------------|
| 200 | Success |
| 400 | Bad Request (invalid input, unsupported format) |
| 404 | Not Found (template mapping not found) |
| 422 | Validation Error (invalid request body) |
| 500 | Internal Server Error |
### Error Response Format
```json
{
"detail": "Error message describing what went wrong"
}
```
### Common Errors
#### 400 Bad Request
```json
{
"detail": "Unsupported file format: .txt"
}
```
#### 404 Not Found
```json
{
"detail": "No mapping found for CustomERP -> sales_orders"
}
```
#### 422 Validation Error
```json
{
"detail": [
{
"loc": ["body", "entity"],
"msg": "field required",
"type": "value_error.missing"
}
]
}
```
## Rate Limiting
Currently, no rate limiting is implemented. For production deployments, consider implementing rate limiting based on your requirements.
## Performance
### Benchmarks
- **Small files** (< 1MB): < 100ms processing time
- **Medium files** (1-10MB): < 500ms processing time
- **Large files** (10-100MB): < 2s processing time
### Optimization Tips
1. Use Parquet format for large datasets
2. Enable auto-detection for unknown data sources
3. Use the `/analyze` endpoint to understand data structure before transformation
4. Batch process multiple files using the download endpoint
## SDK Examples
### Python
```python
import requests
# Transform JSON data
response = requests.post('http://localhost:8000/transform', json={
"auto_select_erp": True,
"auto_select_entity": True,
"data": [{"tranId": "SO1001", "total": 1249.0}]
})
result = response.json()
print(f"Transformed {result['row_count']} records")
```
### JavaScript
```javascript
// Transform file upload
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('auto_select_erp', 'true');
fetch('http://localhost:8000/transform/file', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log('Success:', data));
```
### cURL
```bash
# Analyze file
curl -X POST "http://localhost:8000/analyze" \
-F "file=@data.csv"
# Transform with specific ERP
curl -X POST "http://localhost:8000/transform/file" \
-F "file=@data.csv" \
-F "erp=NetSuite" \
-F "entity=sales_orders"
```
## Deployment
### Docker
```dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]
```
### Environment Variables
```bash
# Optional: OpenAI API key for LLM fallback
OPENAI_API_KEY=your-api-key-here
# Optional: Custom template file path
TEMPLATE_PATH=./custom-template.json
```
## Support
For questions, issues, or feature requests:
1. Check this documentation
2. Review the interactive API docs at `/docs`
3. Search existing issues in the repository
4. Create a new issue with detailed information
---
**API Version**: 2.0.0
**Last Updated**: August 5, 2025
**Engine**: Polars + DuckDB
|