Skip to main content

Pyramid plugin for openapi spec generation

Project description


pyramid_apispec allows you to create an OpenAPI specification file using apispec and an online OpenAPI explorer using the Swagger UI project for your Pyramid application and its marshmallow schemas.


pip install pyramid_apispec

Basic usage

Check out the demo folder and minimal application example by running:

pip install -e '.[demo]'
python demo/

You can then visit your API explorer page at

Or visit generated documentation here for an example of the demo site. (please note that actual REST API is not working in GitHub pages)

Note: The demo site is using OpenAPI/Swagger v2.0. OpenAPI 3.0 is supported as well, it just uses a different YAML schema so pay attention to examples online.


This example is based on apispec, adapted for Pyramid and pyramid_apispec (updated as of apispec v5.1.0).

This example is using OpenAPI 3.0.2

Hinting a route and its view:

Add the OpenAPI YAML to the view docstring. Optionally use Marshmallow schemas to define the inputs and outputs.

import uuid

from marshmallow import Schema, fields
from pyramid.view import view_config

# Optional marshmallow support
class CategorySchema(Schema):
    id = fields.Int()
    name = fields.Str(required=True)

class PetSchema(Schema):
    categories = fields.List(fields.Nested(CategorySchema))
    name = fields.Str()

@view_config(route_name="random_pet", renderer="json")
def random_pet(request):
    """A cute furry animal endpoint.
      description: Get a random pet
        - ApiKeyAuth: []
          description: Return a pet
              schema: PetSchema
    # Hardcoded example data
    pet_data = {
        "name": "sample_pet_" + str(uuid.uuid1()),
        "categories": [{"id": 1, "name": "sample_category"}],
    return PetSchema().dump(pet_data)

For more details on how to document the API, see the OpenAPI specification.

Rendering the spec as JSON response:

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from pyramid.view import view_config
from pyramid_apispec.helpers import add_pyramid_paths

@view_config(route_name="openapi_spec", renderer="json")
def api_spec(request):
    """View to generate the OpenAPI JSON output."""
    spec = APISpec(
        title="Some API",

    # Optional security scheme support
    api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"}
    spec.components.security_scheme("ApiKeyAuth", api_key_scheme)

    # Optionally register Marshmallow schema for more flexibility
    spec.components.schema("Pet", schema=PetSchema)

    # inspect the `random_pet` route and generate operations from docstring
    add_pyramid_paths(spec, 'random_pet', request=request)

    return spec.to_dict()

Adding the API Explorer View

To complement the specification file generation, this package can also provide an API explorer for your application's API via the Swagger UI project:

config.add_route("openapi_spec", "/openapi.json")

By default you need to pass the route name of the view that serves the OpenAPI specification in your application. If needed you can specify a Pyramid permission or custom callable (script_generator argument) to override the default JavaScript configuration of Swagger UI.

The default URL for the explorer is /api-explorer. This setting is controlled via the explorer_route_path argument - the route is registered as pyramid_apispec.api_explorer_path.

Running tests

pip install -e '.[dev]'

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyramid_apispec-0.5.0.tar.gz (11.6 kB view hashes)

Uploaded source

Built Distribution

pyramid_apispec-0.5.0-py2.py3-none-any.whl (10.9 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page