Need help with connexion?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

3.6K Stars 611 Forks Other 1.2K Commits 408 Opened issues


Swagger/OpenAPI First framework for Python on top of Flask with automatic endpoint validation & OAuth2 support

Services available


Need anything else?

Contributors list


.. image:: :alt: Join the chat at :target:

.. image:: :target: :alt: Travis CI build status

.. image:: :target: :alt: Coveralls status

.. image:: :target: :alt: Latest Version

.. image:: :target: :alt: Development Status

.. image:: :target: :alt: Python Versions

.. image:: :target: :alt: License

Connexion is a framework that automagically handles HTTP requests based on

OpenAPI Specification
_ (formerly known as Swagger Spec) of your API described in
YAML format
_. Connexion allows you to write an OpenAPI specification, then maps the endpoints to your Python functions; this makes 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.

We built Connexion this way in order to:

  • simplify the development process
  • confirm expectations about what your API will look like

Connexion Features:

  • Validates requests and endpoint parameters automatically, based on your specification
  • Provides a Web Swagger Console UI so that the users of your API can have live documentation and even call your API's endpoints through it
  • Handles OAuth 2 token-based authentication
  • Supports API versioning
  • Supports automatic serialization of payloads. If your specification defines that an endpoint returns JSON, Connexion will automatically serialize the return value for you and set the right content type in the HTTP header.

Why Connexion

With 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 that offered by frameworks such as Hug_, which generates a specification after you'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 code logic of your application.

Other Sources/Mentions

  • Zalando RESTful API guidelines with
    API First
  • Connexion listed on Swagger_'s website
  • Blog post:
    Crafting effective Microservices in Python

New in Connexion 2.0:

  • App and Api options must be provided through the "options" argument (
    have been removed).
  • You must specify a form content-type in 'consumes' in order to consume form data.
  • The
    interface has been formalized in the
  • The
    class has been renamed to
  • Array parameter deserialization now follows the Swagger 2.0 spec more closely. In situations when a query parameter is passed multiple times, and the collectionFormat is either csv or pipes, the right-most value will be used. For example,
    will result in
    q = [4, 5, 6]
    . The old behavior is available by setting the collectionFormat to
    , or by importing
    and passing
    to your Api.
  • The spec validator library has changed from
  • Errors that previously raised
    now raise the
    exception. All spec validation errors should be wrapped with
  • Support for nullable/x-nullable, readOnly and writeOnly/x-writeOnly has been added to the standard json schema validator.
  • Custom validators can now be specified on api level (instead of app level).
  • Added support for basic authentication and apikey authentication
  • If unsupported security requirements are defined or
    is missing, connexion now denies requests instead of allowing access without security-check.
  • Accessing
    is no longer supported, use

How to Use


Python 3.6+

Installing It

In your command line, type:

.. code-block:: bash

$ pip install connexion

Running It

Place your API YAML inside a folder in the root path of your application (e.g

). Then run:

.. code-block:: python

import connexion

app = connexion.App(name, specification_dir='swagger/') app.add_api('my_api.yaml')

See the

Connexion Pet Store Example Application
_ for a sample specification.

Now you're able to run and use Connexion!

OAuth 2 Authentication and Authorization

Connexion supports one of the three OAuth 2 handling methods. (See "TODO" below.) With Connexion, the API security definition must include a 'x-tokenInfoUrl' or 'x-tokenInfoFunc (or set

env var respectively). 'x-tokenInfoUrl' must contain an URL to validate and get the
token information
_ and 'x-tokenInfoFunc must contain a reference to a function used to obtain the token info. When both 'x-tokenInfoUrl' and 'x-tokenInfoFunc' are used, Connexion will prioritize the function method. Connexion expects to receive the OAuth token in the
header field in the format described in
RFC 6750 
_ section 2.1. This aspect represents a significant difference from the usual OAuth flow.

Dynamic Rendering of Your Specification

Connexion uses Jinja2_ to allow specification parameterization through the

parameter. You can define specification arguments for the application either globally (via the
constructor) or for each specific API (via the

.. code-block:: python

app = connexion.App(__name__, specification_dir='swagger/',
                    arguments={'global': 'global_value'})
app.add_api('my_api.yaml', arguments={'api_local': 'local_value'})

When a value is provided both globally and on the API, the API value will take precedence.

Endpoint Routing to Your Python Views

Connexion uses the

from each
Operation Object
_ to identify which Python function should handle each URL.

Explicit Routing:

.. code-block:: yaml

      operationId: myapp.api.hello_world

If you provide this path in your specification POST requests to

, it will be handled by the function
in the
module. Optionally, you can include
) in your operation definition, making

.. code-block:: yaml

      x-swagger-router-controller: myapp.api
      operationId: hello_world

Keep in mind that Connexion follows how

HTTP methods work in Flask
_ and therefore HEAD requests will be handled by the
specified under GET in the specification. If both methods are supported,
can be used to determine which request was made.

Automatic Routing

To customize this behavior, Connexion can use alternative

--for example,
. The
will compose an
based on the path and HTTP method of the endpoints in your specification:

.. code-block:: python

from connexion.resolver import RestyResolver

app = connexion.App(name) app.add_api('swagger.yaml', resolver=RestyResolver('api'))

.. code-block:: yaml

paths: /: get: # Implied operationId: api.get /foo: get: # Implied operationId: post: # Implied operationId:

      # Implied operationId:
      # Implied operationId:
      # Implied operationId:
      # Implied operationId:

will give precedence to any
encountered in the specification. It will also respect
. You can import and extend
to implement your own
(and function) resolution algorithm.

Automatic Parameter Handling

Connexion automatically maps the parameters defined in your endpoint specification to arguments of your Python views as named parameters, and, whenever possible, with value casting. Simply define the endpoint's parameters with the same names as your views arguments.

As an example, say you have an endpoint specified as:

.. code-block:: yaml

      operationId: api.foo_get
        - name: message
          description: Some message.
          in: query
          type: string
          required: true

And the view function:

.. code-block:: python

# file

def foo_get(message): # do something return 'You send the message: {}'.format(message), 200

In this example, Connexion automatically recognizes that your view function expects an argument named

and assigns the value of the endpoint parameter
to your view function.

.. note:: In the OpenAPI 3.x.x spec, the requestBody does not have a name. By default it will be passed in as 'body'. You can optionally provide the x-body-name parameter in your requestBody schema to override the name of the parameter that will be passed to your handler function.

.. warning:: When you define a parameter at your endpoint as not required, and this argument does not have default value in your Python view, you will get a "missing positional argument" exception whenever you call this endpoint WITHOUT the parameter. Provide a default value for a named argument or use


Type casting ^^^^^^^^^^^^

Whenever possible, Connexion will try to parse your argument values and do type casting to related Python native values. The current available type castings are:

+--------------+-------------+ | OpenAPI Type | Python Type | +==============+=============+ | integer | int | +--------------+-------------+ | string | str | +--------------+-------------+ | number | float | +--------------+-------------+ | boolean | bool | +--------------+-------------+ | array | list | +--------------+-------------+ | null | None | +--------------+-------------+ | object | dict | +--------------+-------------+

If you use the

type In the Swagger definition, you can define the
so that it won't be recognized. Connexion currently supports collection formats "pipes" and "csv". The default format is "csv".

Connexion is opinionated about how the URI is parsed for

types. The default behavior for query parameters that have been defined multiple times is to use the right-most value. For example, if you provide a URI with the the query string
, connexion will set
letters = ['d', 'e', 'f']

You can override this behavior by specifying the URI parser in the app or api options.

.. code-block:: python

from connexion.decorators.uriparsing import AlwaysMultiURIParser options = {'uriparserclass': AlwaysMultiURIParser} app = connexion.App(name, specificationdir='swagger/', options=options)

You can implement your own URI parsing behavior by inheriting from


There are a handful of URI parsers included with connection.

+----------------------+---------------------------------------------------------------------------+ | OpenAPIURIParser | This parser adheres to the OpenAPI 3.x.x spec, and uses the

| | default: OpenAPI 3.0 | parameter. Query parameters are parsed from left to right, so if a query | | | parameter is defined twice, then the right-most definition will take | | | precedence. For example, if you provided a URI with the query string | | |
, and
style: simple
, then connexion | | | will set
letters = ['d', 'e', 'f']
. For additional information see | | |
OpenAPI 3.0 Style Values
_. | +----------------------+---------------------------------------------------------------------------+ | Swagger2URIParser | This parser adheres to the Swagger 2.0 spec, and will only join together | | default: OpenAPI 2.0 | multiple instance of the same query parameter if the
| | | is set to
. Query parameters are parsed from left to right, so | | | if a query parameter is defined twice, then the right-most definition | | | wins. For example, if you provided a URI with the query string | | |
, and
collectionFormat: csv
, then | | | connexion will set
letters = ['d', 'e', 'f']
| +----------------------+---------------------------------------------------------------------------+ | FirstValueURIParser | This parser behaves like the Swagger2URIParser, except that it prefers | | | the first defined value. For example, if you provided a URI with the query| | | string
collectionFormat: csv
| | | hen connexion will set
letters = ['a', 'b', 'c']
| +----------------------+---------------------------------------------------------------------------+ | AlwaysMultiURIParser | This parser is backwards compatible with Connexion 1.x. It joins together | | | multiple instances of the same query parameter. | +----------------------+---------------------------------------------------------------------------+

Parameter validation ^^^^^^^^^^^^^^^^^^^^

Connexion can apply strict parameter validation for query and form data parameters. When this is enabled, requests that include parameters not defined in the swagger spec return a 400 error. You can enable it when adding the API to your application:

.. code-block:: python

app.add_api('my_apy.yaml', strict_validation=True)

API Versioning and basePath

Setting a base path is useful for versioned APIs. An example of a base path would be the


If you are using OpenAPI 3.x.x, you set your base URL path in the servers block of the specification. You can either specify a full URL, or just a relative path.

.. code-block:: yaml

  - url: https://MYHOST/1.0
    description: full url example
  - url: /1.0
    description: relative path example

paths: ...

If you are using OpenAPI 2.0, you can define a

on the top level of your OpenAPI 2.0 specification.

.. code-block:: yaml

basePath: /1.0

paths: ...

If you don't want to include the base path in your specification, you can provide it when adding the API to your application:

.. code-block:: python

app.add_api('my_api.yaml', base_path='/1.0')

Swagger JSON

Connexion makes the OpenAPI/Swagger specification in JSON format available from either

(for OpenAPI 2.0) or
(for OpenAPI 3.x.x) at the base path of the API. For example, if your base path was
, then your spec would be available at

You can disable serving the spec JSON at the application level:

.. code-block:: python

options = {"serve_spec": False}
app = connexion.App(__name__, specification_dir='openapi/',

You can also disable it at the API level:

.. code-block:: python

options = {"serve_spec": False}
app = connexion.App(__name__, specification_dir='openapi/')
app.add_api('my_api.yaml', options=options)

HTTPS Support

When specifying HTTPS as the scheme in the API YAML file, all the URIs in the served Swagger UI are HTTPS endpoints. The problem: The default server that runs is a "normal" HTTP server. This means that the Swagger UI cannot be used to play with the API. What is the correct way to start a HTTPS server when using Connexion?

One way,

described by Flask
_, looks like this:

.. code-block:: python

from OpenSSL import SSL context = SSL.Context(SSL.SSLv23METHOD) context.useprivatekeyfile('yourserver.key') context.usecertificate_file('yourserver.crt')'', port='12344', debug=False/True, ssl_context=context)

However, Connexion doesn't provide an sslcontext parameter. This is because Flask doesn't, either--but it uses

to send the parameters to the underlying

The Swagger UI Console

The Swagger UI for an API is available through pip extras. You can install it with

pip install connexion[swagger-ui]
. It will be served up at
is the base path of the API.

You can disable the Swagger UI at the application level:

.. code-block:: python

app = connexion.App(__name__, specification_dir='openapi/',
                    options={"swagger_ui": False})

You can also disable it at the API level:

.. code-block:: python

app = connexion.App(__name__, specification_dir='openapi/')
app.add_api('my_api.yaml', options={"swagger_ui": False})

If necessary, you can explicitly specify the path to the directory with swagger-ui to not use the connexion[swagger-ui] distro. In order to do this, you should specify the following option:

.. code-block:: python

options = {'swaggerpath': '/path/to/swaggerui/'} app = connexion.App(name, specification_dir='openapi/', options=options)

If you wish to provide your own swagger-ui distro, note that connexion expects a jinja2 file called

in order to load the correct
by default. Your
file can use the
jinja variable for this purpose:

.. code-block::

const ui = SwaggerUIBundle({ url: "{{ openapi_spec_url }}"})

Additionally, if you wish to use swagger-ui-3.x.x, it is also provided by installing connexion[swagger-ui], and can be enabled like this:

.. code-block:: python

from swaggeruibundle import swaggerui3path options = {'swaggerpath': swaggerui3path} app = connexion.App(name, specificationdir='swagger/', options=options)

Server Backend

By default Connexion uses the Flask_ server. For asynchronous applications, you can also use Tornado_ as the HTTP server. To do this, set your server to


.. code-block:: python

import connexion

app = connexion.App(name, specification_dir='swagger/')'tornado', port=8080)

You can use the Flask WSGI app with any WSGI container, e.g.

Flask with uWSGI
_ (this is common):

.. code-block:: python

app = connexion.App(__name__, specification_dir='swagger/')
application = # expose global WSGI application object

You can use the

framework as server backend as well:

.. code-block:: python

import connexion

app = connexion.AioHttpApp(name, specification_dir='swagger/')

.. note:: Also check aiohttp handler examples_.

Set up and run the installation code:

.. code-block:: bash

$ sudo pip3 install uwsgi
$ uwsgi --http :8080 -w app -p 16  # use 16 worker processes

See the

uWSGI documentation
_ for more information.

.. _using Flask with uWSGI: .. _uWSGI documentation: .. _examples:


Additional information is available at

Connexion's Documentation Page


A full changelog is maintained on the

GitHub releases page

.. _GitHub releases page:

Contributing to Connexion/TODOs

We welcome your ideas, issues, and pull requests. Just follow the usual/standard GitHub practices.

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 (Zalando SE, Berlin) shall be under the terms and conditions of Apache License 2.0 written below, without any additional copyright information, terms or conditions.


If you'd like to become a more consistent contributor to Connexion, we'd love your help working on these we have a list of

issues where we are looking for contributions


We'd like to thank all of Connexion's contributors for working on this project, and to Swagger/OpenAPI for their support.


Copyright 2015 Zalando SE

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

.. Flask: .. _issues waffle board: .. _API First: .. _Hug: .. _Swagger: .. _Jinja2: .. _rfc6750: .. _OpenAPI Specification: .. _OpenAPI 3.0 Style Values: .. _Operation Object: .. _swager.spec.securitydefinition: .. swager.spec.securityrequirement: .. _YAML format: .. _token information: .. _Tornado: .. _Connexion Pet Store Example Application: .. _described by Flask: .. _werkzeug: .. _Connexion's Documentation Page: .. _Crafting effective Microservices in Python: .. _issues where we are looking for contributions: .. _HTTP Methods work in Flask:

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.