File size: 2,391 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
OpenAPI/Swagger configuration for FastAPI documentation.

Configures authentication methods (API Key and Bearer Token) and API documentation.
"""

from fastapi import FastAPI
from fastapi.security import APIKeyHeader
from fastapi.openapi.utils import get_openapi


# Security scheme for API Key header
api_key_header = APIKeyHeader(
    name="X-API-Key",
    auto_error=False,
    description="API Key in format: pk_xxx:sk_xxx"
)


def configure_openapi(app: FastAPI) -> None:
    """
    Configure custom OpenAPI schema with both API Key and Bearer token authentication.
    
    Args:
        app: The FastAPI application instance
    """
    def custom_openapi():
        if app.openapi_schema:
            return app.openapi_schema
        
        openapi_schema = get_openapi(
            title="Kortix API",
            version="1.0.0",
            description="""
## Authentication

This API supports two authentication methods:

### 1. API Key (Recommended for programmatic access)
Use the `X-API-Key` header with your API key in the format: `pk_xxx:sk_xxx`

```bash
curl -H "X-API-Key: pk_abc123:sk_def456" https://api.kortix.com/v1/threads
```

**Get your API key:** [https://www.kortix.com/settings/api-keys](https://www.kortix.com/settings/api-keys)

### 2. Bearer Token (JWT)
Use the `Authorization` header with a Supabase JWT token:

```bash
curl -H "Authorization: Bearer eyJhbG..." https://api.kortix.com/v1/threads
```
            """,
            routes=app.routes,
        )
        
        # Add both security schemes
        openapi_schema["components"]["securitySchemes"] = {
            "APIKeyHeader": {
                "type": "apiKey",
                "in": "header",
                "name": "X-API-Key",
                "description": "API Key in format pk_xxx:sk_xxx — Generate at https://www.kortix.com/settings/api-keys"
            },
            "BearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "JWT",
                "description": "Supabase JWT token"
            }
        }
        
        # Apply security globally (both methods accepted)
        openapi_schema["security"] = [
            {"APIKeyHeader": []},
            {"BearerAuth": []}
        ]
        
        app.openapi_schema = openapi_schema
        return app.openapi_schema
    
    app.openapi = custom_openapi