OpenAPI/Swagger spec-first web framework for Python with automatic request validation and response serialization
npx @tessl/cli install tessl/pypi-connexion@3.2.0Connexion is a Python framework that automagically handles HTTP requests based on OpenAPI Specification (formerly known as Swagger Spec) of your API described in YAML/JSON format. Connexion allows you to write an OpenAPI specification first, then maps the endpoints to your Python functions, making it unique as many tools generate the specification based on your Python code. You can describe your REST API in as much detail as you want; then Connexion guarantees that it will work as you specified.
pip install connexionpip install connexion[flask] for Flask support, pip install connexion[swagger-ui] for Swagger UI, pip install connexion[uvicorn] for production ASGI serverimport connexionFor AsyncApp (recommended for new projects):
from connexion import AsyncAppFor Flask compatibility:
from connexion import FlaskAppFor middleware integration:
from connexion import ConnexionMiddlewarefrom connexion import AsyncApp
# Create async application
app = AsyncApp(__name__)
# Add OpenAPI specification
app.add_api('openapi.yaml')
# Run with uvicorn
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)from connexion import FlaskApp
# Create Flask-based application
app = FlaskApp(__name__)
# Add OpenAPI specification
app.add_api('openapi.yaml')
# Access underlying Flask app if needed
flask_app = app.app
# Run development server
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)from connexion import ConnexionMiddleware
from starlette.applications import Starlette
# Add Connexion to existing ASGI app
asgi_app = Starlette()
app = ConnexionMiddleware(asgi_app)
app.add_api('openapi.yaml')Connexion implements a layered architecture that separates OpenAPI specification handling from the underlying web framework:
AsyncApp, FlaskApp provide framework-specific implementationsThis design enables spec-first development where the OpenAPI specification serves as the single source of truth for API behavior, with automatic generation of route handlers, parameter validation, response serialization, and interactive documentation.
Core application classes for creating Connexion-based web services with different underlying frameworks and deployment models.
class AsyncApp:
def __init__(self, import_name: str, **kwargs): ...
def add_api(self, specification: str, **kwargs): ...
def run(self, host: str = None, port: int = None, **options): ...
class FlaskApp:
def __init__(self, import_name: str, **kwargs): ...
def add_api(self, specification: str, **kwargs): ...
def run(self, host: str = None, port: int = None, **options): ...
class ConnexionMiddleware:
def __init__(self, app, **kwargs): ...
def add_api(self, specification: str, **kwargs): ...Comprehensive middleware system for processing HTTP requests and responses with automatic validation, serialization, and error handling based on OpenAPI specifications.
class ConnexionRequest:
async def get_body(self): ...
async def json(self): ...
async def form(self) -> dict: ...
async def files(self) -> dict: ...
@property
def query_params(self) -> dict: ...
@property
def path_params(self) -> dict: ...
class ConnexionResponse:
def __init__(self, body, status_code: int = 200, headers: dict = None): ...
def problem(status: int, title: str, detail: str = None, **kwargs): ...System for mapping OpenAPI operations to Python functions using various resolution strategies for flexible endpoint handling.
class Resolver:
def __init__(self, function_resolver=None): ...
def resolve(self, operation): ...
class RestyResolver:
def __init__(self, default_module_name: str, **kwargs): ...
class MethodResolver:
def __init__(self, api_name: str, **kwargs): ...Automatic request and response validation based on OpenAPI schema definitions with support for various content types and validation strategies.
VALIDATOR_MAP: dict
class AbstractRequestBodyValidator: ...
class AbstractParameterValidator: ...
class AbstractResponseBodyValidator: ...Built-in security handlers for common authentication methods with support for Bearer tokens, Basic auth, API keys, and OAuth2.
def bearer_auth(token: str): ...
def basic_auth(username: str, password: str): ...
def api_key_auth(api_key: str, required_scopes: list): ...Comprehensive exception system following RFC 7807 Problem Details standard for consistent error responses and debugging.
class ProblemException(Exception):
def __init__(self, status: int, title: str, detail: str = None, **kwargs): ...
class BadRequestProblem(ProblemException): ...
class UnauthorizedProblem(ProblemException): ...
class ForbiddenProblem(ProblemException): ...Command-line interface for running development servers and managing Connexion applications.
# Command: connexion run <spec_file>
def main(): ...# Request/Response Types
MaybeAwaitable = Union[T, Awaitable[T]]
WSGIApp = Callable[[dict, Callable], Iterable[bytes]]
ASGIApp = Callable[[dict, Callable, Callable], Awaitable[None]]
# Data Structures
class NoContent:
"""Represents empty HTTP response body"""
pass
class MediaTypeDict(dict):
"""Media type mapping utilities"""
pass