# Generate invoice Generate an invoice for a specific organization and billing period. This endpoint is restricted to staff users only. #### Request Body **`organization_id`** string required Organization ID (format: `org_`). **`year`** integer required Year for billing period (2024-2100). **`month`** integer required Month for billing period (1-12). **`currency`** string optional Currency code (CHF, USD, EUR, GBP). Defaults to CHF. **`regenerate`** boolean optional Regenerate if invoice already exists. Defaults to false. ## Returns Returns the generated invoice object with all line items, tax calculations, and metadata. ```bash curl -X POST "https://api.aitronos.com/v1/invoices/generate" \ -H "X-API-Key: $FREDDY_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "organization_id": "org_123e4567e89b12d3a456426614174000", "year": 2024, "month": 12, "currency": "CHF", "regenerate": false }' ``` ```python import os import requests api_key = os.environ["FREDDY_API_KEY"] response = requests.post( "https://api.aitronos.com/v1/invoices/generate", headers={"X-API-Key": api_key}, json={ "organization_id": "org_123e4567e89b12d3a456426614174000", "year": 2024, "month": 12, "currency": "CHF", "regenerate": False } ) invoice = response.json() print(f"Invoice generated: {invoice['invoice_number']}") ``` ```javascript const response = await fetch( "https://api.aitronos.com/v1/invoices/generate", { method: "POST", headers: { "X-API-Key": process.env.FREDDY_API_KEY, "Content-Type": "application/json" }, body: JSON.stringify({ organization_id: "org_123e4567e89b12d3a456426614174000", year: 2024, month: 12, currency: "CHF", regenerate: false }) } ); const invoice = await response.json(); console.log(`Invoice generated: ${invoice.invoice_number}`); ``` **Response:** 201 Created ```json { "id": "inv_123e4567e89b12d3a456426614174000", "invoice_number": "INV-2024-12-AIT-001", "organization_id": "org_123e4567e89b12d3a456426614174000", "organization_name": "Aitronos AG", "billing_period_start": "2024-12-01T00:00:00Z", "billing_period_end": "2025-01-01T00:00:00Z", "issue_date": "2024-12-31T23:59:59Z", "due_date": "2025-01-30T23:59:59Z", "payment_date": null, "currency": "CHF", "exchange_rate": 1.0, "subtotal": 1250.50, "tax_rate": 0.081, "tax_amount": 101.29, "total_amount": 1351.79, "status": "issued", "items": [ { "id": "invitem_123e4567e89b12d3a456426614174000", "description": "GPT-4 Turbo - Input Tokens", "model_name": "gpt-4-turbo", "provider": "openai", "quantity": 1250.5, "unit": "1K tokens", "unit_price": 0.01, "amount": 12.51, "input_tokens": 1250500, "output_tokens": null, "total_requests": 1523 }, { "id": "invitem_456e4567e89b12d3a456426614174000", "description": "GPT-4 Turbo - Output Tokens", "model_name": "gpt-4-turbo", "provider": "openai", "quantity": 3420.75, "unit": "1K tokens", "unit_price": 0.03, "amount": 102.62, "input_tokens": null, "output_tokens": 3420750, "total_requests": 1523 } ], "created_at": "2024-12-31T23:59:59Z", "updated_at": "2024-12-31T23:59:59Z" } ``` 401 Unauthorized ```json { "success": false, "error": { "code": "AUTHENTICATION_REQUIRED", "message": "Authentication required", "system_message": "Missing or invalid authentication token", "type": "client_error", "status": 401, "details": {}, "trace_id": "2fbbf3b6-51a1-4f1b-88e2-c00e8b52fbb8", "timestamp": "2025-11-26T08:21:45Z" } } ``` 403 Forbidden ```json { "success": false, "error": { "code": "INSUFFICIENT_PERMISSIONS", "message": "You don't have permission to perform this action.", "system_message": "Only staff can create invoices", "type": "client_error", "status": 403, "details": { "required_role": "aitronos_staff" }, "trace_id": "2fbbf3b6-51a1-4f1b-88e2-c00e8b52fbb8", "timestamp": "2025-11-26T08:21:45Z" } } ``` 404 Not Found ```json { "success": false, "error": { "code": "ORGANIZATION_NOT_FOUND", "message": "Organization not found", "system_message": "Organization does not exist", "type": "client_error", "status": 404, "details": { "organization_id": "org_123e4567e89b12d3a456426614174000" }, "trace_id": "2fbbf3b6-51a1-4f1b-88e2-c00e8b52fbb8", "timestamp": "2025-11-26T08:21:45Z" } } ``` 409 Conflict ```json { "success": false, "error": { "code": "RESOURCE_ALREADY_EXISTS", "message": "Invoice already exists for this period", "system_message": "Use regenerate=true to recreate", "type": "client_error", "status": 409, "details": { "organization_id": "org_123e4567e89b12d3a456426614174000", "year": 2024, "month": 12 }, "trace_id": "2fbbf3b6-51a1-4f1b-88e2-c00e8b52fbb8", "timestamp": "2025-11-26T08:21:45Z" } } ``` 422 Unprocessable Entity ```json { "success": false, "error": { "code": "INVALID_INPUT", "message": "No usage data found for period", "system_message": "Cannot generate invoice without usage data", "type": "client_error", "status": 422, "details": { "organization_id": "org_123e4567e89b12d3a456426614174000", "year": 2024, "month": 12 }, "trace_id": "2fbbf3b6-51a1-4f1b-88e2-c00e8b52fbb8", "timestamp": "2025-11-26T08:21:45Z" } } ```