Connexion

3.3.0 · active · verified Mon Apr 06

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

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple 'Hello World' API using Connexion's `AsyncApp` (ASGI backend). It defines an API using an `openapi.yaml` specification file and links an operation to a Python function. The application can be run using `uvicorn` (e.g., `uvicorn app:app --reload` from the command line, assuming the Python file is named `app.py`). Ensure `openapi.yaml` is in the same directory as `app.py`.

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

view raw JSON →