Introduction
Torn is built on FastAPI, a modern, high-performance Python web framework. The API follows REST principles and provides comprehensive endpoints for managing electronic invoicing operations for SII Chile.Base Information
Torn - Facturador Electrónico
Sistema de facturación electrónica para el SII de Chile
0.1.0
https://api.torn.cl (production) or http://localhost:8000 (development)API Architecture
Multi-Tenant Design
Torn uses a multi-tenant SaaS architecture where:- Global Level: User authentication and tenant management
- Tenant Level: Business operations (customers, products, sales, etc.)
- Schema Isolation: Each tenant has a dedicated PostgreSQL schema for data isolation
Router Organization
The API is organized into functional routers defined inapp/main.py:42:
Common Patterns
Request Headers
All tenant-scoped endpoints require these headers:Global endpoints (like
/auth/login) only require the Authorization header after initial login.Response Format
All endpoints return JSON responses. Successful responses return the requested data directly:Pagination
While not universally implemented, endpoints that support pagination typically use query parameters:Most list endpoints currently return all results. Pagination will be added to high-volume endpoints as needed.
Filtering
Search and filter endpoints use query parameters. Example fromapp/routers/customers.py:55:
- Text search:
?q=search_term - Date range:
?from_date=2026-01-01&to_date=2026-03-08 - Status filter:
?is_active=true
Sorting
Results are typically sorted by a logical default field:- Customers: Alphabetically by
razon_social - Products: By ID descending (newest first)
- Sales: By date descending (newest first)
Standard HTTP Methods
The API follows REST conventions:| Method | Purpose | Example |
|---|---|---|
| GET | Retrieve resource(s) | GET /customers |
| POST | Create new resource | POST /customers |
| PUT | Update existing resource | PUT /customers/{id} |
| DELETE | Delete resource | DELETE /customers/{id} |
Error Handling
The API uses standard HTTP status codes and returns detailed error messages.Success Codes
Request successful, resource returned
Resource successfully created
Resource successfully deleted
Error Codes
Invalid request data or parameters
Missing or invalid authentication token
Authenticated but insufficient permissions
Resource does not exist
Resource conflict (e.g., duplicate RUT)
Validation error in request body
Server error
Error Response Format
Errors return a consistent structure:Example Error Handling
Fromapp/routers/customers.py:32:
app/routers/customers.py:88:
CORS Configuration
The API is configured to accept requests from local development origins defined inapp/main.py:19:
Resource Patterns
List Resources
Retrieve all resources of a type:Get Single Resource
Retrieve a specific resource by identifier:Create Resource
Create a new resource:Update Resource
Update an existing resource (partial update):Updates are partial - only include fields you want to change.
Delete Resource
Delete a resource:Auto-Generation Features
Several resources support automatic ID/code generation:Product SKU
Fromapp/routers/products.py:24:
codigo_interno when creating a product, one is generated automatically.
EAN-13 Barcodes
Fromapp/routers/products.py:49:
Interactive Documentation
FastAPI provides automatic interactive API documentation:- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - OpenAPI Schema:
http://localhost:8000/openapi.json
- Explore all available endpoints
- View request/response schemas
- Test endpoints directly from the browser
- See detailed validation rules
Health Check
Check API availability:Root Endpoint
Basic API info fromapp/main.py:68:
Next Steps
Authentication
Learn about JWT authentication and tenant access
Customers API
Manage customer records and contacts
Products API
Create and manage product catalog
Sales API
Generate invoices and sales documents