Connexion
Connexion is a modern Python web framework that facilitates API-first development using OpenAPI (formerly Swagger) specifications. It automatically handles routing, request validation, authentication, parameter parsing, and response serialization based on your specification. Version 3.3.0 is the latest stable release, offering a modular, ASGI-compatible architecture with support for both Flask (WSGI) and Starlette (ASGI) backends. The library maintains an active release cadence, frequently publishing updates and new features.
Warnings
- breaking Connexion 3.x introduced fundamental changes by adopting the ASGI interface, dropping Aiohttp support entirely. This significantly changes how applications are created and run, favoring `AsyncApp` (Starlette-based) for new asynchronous projects and `FlaskApp` (Flask-based) for WSGI compatibility or migration.
- breaking The global `connexion.request` object now represents a Starlette `Request` when using `AsyncApp` in Connexion 3.x, instead of a Flask `Request` as in 2.x. This impacts direct access to request attributes and methods.
- breaking Python 3.6 support was dropped in Connexion 3.x, and the minimum required Python version is now 3.9.
- gotcha Connexion 3.x changed how `uri_parser_class` and `jsonifier` are passed. They are now arguments directly to the `App` constructor or `add_api()` method, rather than through an `options` dictionary or by setting attributes on the `Api` object.
- gotcha Connexion 3.x no longer attempts to guess a content type for response serialization if multiple content types are defined in the OpenAPI specification for a given response.
Install
-
pip install connexion -
pip install connexion[flask] -
pip install connexion[starlette] -
pip install connexion[swagger-ui] -
pip install connexion[uvicorn]
Imports
- AsyncApp
from connexion import AsyncApp
- FlaskApp
from connexion import FlaskApp
- ConnexionMiddleware
from connexion import ConnexionMiddleware
- request
from connexion import request
Quickstart
import connexion
from pathlib import Path
# app.py
def get_hello():
return {"message": "Hello from Connexion!"}, 200
# Create the Connexion app using AsyncApp for ASGI compatibility
# and look for the OpenAPI spec in the current directory.
app = connexion.AsyncApp(__name__, specification_dir='.')
# Add the API defined in openapi.yaml
app.add_api('openapi.yaml')
# To run the application (requires 'pip install connexion[uvicorn]')
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=8080)
# For external running (e.g., via command line: uvicorn app:app --reload)
# Define your OpenAPI spec in openapi.yaml in the same directory:
# openapi: 3.0.0
# info:
# title: Simple Hello API
# version: 1.0.0
# paths:
# /hello:
# get:
# operationId: app.get_hello # Links to the get_hello function in app.py
# responses:
# '200':
# description: A greeting
# content:
# application/json:
# schema:
# type: object
# properties:
# message:
# type: string