Package: connexion
Explore the docs »Connexion is a modern Python web framework that makes spec-first and api-first development easy.
You describe your API in anOpenAPI(orSwagger) specification with as much
detail as you want and Connexion will guarantee that it works as you specified.It works either standalone, or in combination with any ASGI or WSGI-compatible framework!📢 Connexion 3 was recently released! Read about the changes here »✨ FeaturesConnexion provides the following functionalitybased on your specification:🚏Automatic route registration, no@routedecorators needed🔒Authentication, split from your application logic🔎Request and response validationof headers, parameters, and body📬Parameter parsing and injection, no request object needed📨Response serialization, you can return regular Python objects📺A Swagger UI consolewith live documentation and ‘try it out’ feature🧩Pluggability, in all dimensionsConnexion alsohelps you write your OpenAPI specificationand develop against it by providing a command line interface which lets you test and mock your specification.connexionrunopenapi.yaml(back to top)🫶 SponsorsSponsors help us dedicate time to maintain Connexion. Want to help?Explore the options »(back to top)🪤 Why ConnexionWith Connexion, you write the spec first. Connexion then calls your Python
code, handling the mapping from the specification to the code. This
incentivizes you to write the specification so that all of your
developers can understand what your API does, even before you write a
single line of code.If multiple teams depend on your APIs, you can use Connexion to easily
send them the documentation of your API. This guarantees that your API will
follow the specification that you wrote. This is a different process from
the one offered by most frameworks, which generate a specificationafteryou've written the code.
Some disadvantages of generating specifications based on code is that
they often end up lacking details or mix your documentation with the implementation
logic of your application.(back to top)⚒️ How to UseInstallationYou can install connexion using pip:$pipinstallconnexionConnexion provides 'extras' with optional dependencies to unlock additional features:swagger-ui: Enables a Swagger UI console for your application.uvicorn: Enables to run the your application usingapp.run()for
development instead of using an external ASGI server.flask: Enables theFlaskAppto build applications compatible with the Flask
ecosystem.You can install them as follows:$pipinstallconnexion[swagger-ui]$pipinstallconnexion[swagger-ui,uvicorn].(back to top)Creating your applicationConnexion can be used either as a standalone application or as a middleware wrapping an existing
ASGI (or WSGI) application written using a different framework. The standalone application can be
built using either theAsyncApporFlaskApp.TheAsyncAppis a lightweight application with native asynchronous support. Use it if you
are starting a new project and have no specific reason to use one of the other options.fromconnexionimportAsyncAppapp=AsyncApp(__name__)TheFlaskAppleverages theFlaskframework, which is useful if you're migrating from
connexion 2.X or you want to leverage theFlaskecosystem.fromconnexionimportFlaskAppapp=FlaskApp(__name__)TheConnexionMiddlewarecan be wrapped around any existing ASGI or WSGI application.
Use it if you already have an application written in a different framework and want to add
functionality provided by connexionfromasgi_frameworkimportAppfromconnexionimportConnexionMiddlewareapp=App(__name__)app=ConnexionMiddleware(app)(back to top)Registering an APIWhile you can register individual routes on your application, Connexion really shines when you
register an API defined by an OpenAPI (or Swagger) specification.
The operation described in your specification is automatically linked to your Python view function via theoperationIdrun.pydefpost_greeting(name:str,greeting:str):# Paramaeters are automatically unpackedreturnf"{greeting}{name}",200# Responses are automatically serializedapp.add_api("openapi.yaml")openapi.yaml...paths:/greeting/{name}:post:operationId:run.post_greetingresponses:200:content:text/plain:schema:type:stringparameters:-name:namein:pathrequired:trueschema:type:string-name:greetingin:queryrequired:trueschema:type:string(back to top)Running your applicationIf you installed connexion usingconnexion[uvicorn], you can run it using therunmethod. This is only recommended for development:app.run()In production, run your application using an ASGI server such asuvicorn. If you defined yourappin a python module calledrun.py, you can run it as follows:$uvicornrun:appOr with gunicorn:$gunicorn-kuvicorn.workers.UvicornWorkerrun:appNow you're able to run and use Connexion!See theexamplesfolder for more examples.(back to top)📜 ChangesA full changelog is maintained on theGitHub releases page.(back to top)🤲 ContributingWe welcome your ideas, issues, and pull requests. Just follow the
usual/standard GitHub practices.For easy development, install connexion using poetry with all extras, and
install the pre-commit hooks to automatically run black formatting and static analysis checks.pipinstallpoetrypoetryinstall--all-extraspre-commitinstallYou can find out more about how Connexion works and where to apply your changes by having a look
at ourarchitecture.Unless you explicitly state otherwise in advance, any non trivial
contribution intentionally submitted for inclusion in this project by you
to the steward of this repository shall be under the
terms and conditions of Apache License 2.0 written below, without any
additional copyright information, terms or conditions.(back to top)🙏 ThanksWe'd like to thank all of Connexion's contributors for working on this
project, Swagger/OpenAPI for their support, and Zalando for originally developing and releasing Connexion.📚 Recommended ResourcesAbout the advantages of working spec-first:Blog AtlassianAPI guidelines ZalandoBlog ML6Blog ZalandoTools to help you work spec-first:Online swagger editorVS Code pluginPycharm plugin