APISpec is a pluggable Python library designed for generating API specifications. It primarily supports the OpenAPI Specification (formerly known as the Swagger specification), enabling developers to programmatically define their API's structure, endpoints, and data models. It is framework-agnostic and offers built-in integration capabilities, notably with Marshmallow. The library maintains an active development status, with frequent patch and minor releases, and new major versions typically released on an annual or bi-annual cadence.
pip install apispecVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize an `APISpec` object, define a Marshmallow schema, register it as an OpenAPI component, and then add a path with an operation that references the defined schema. Finally, it prints the generated OpenAPI specification in JSON format. This illustrates the basic programmatic API definition workflow using `apispec` and its Marshmallow plugin.
Always pass `openapi_version='2.0'` or `openapi_version='3.0.2'` (or another desired version) when initializing `APISpec`.
Install `apispec-webframeworks` (`pip install apispec-webframeworks`) and update your import statements (e.g., `from apispec_webframeworks.flask import FlaskPlugin`).
Ensure all internal references to components within your spec are by their short ID. `apispec` will generate the correct full path based on the OpenAPI version.
Upgrade your Marshmallow installation to a compatible version (e.g., `pip install -U 'marshmallow>=3.13.0'`). Consider using `apispec[marshmallow]` to ensure compatible versions are installed. Additionally, update your Marshmallow schemas to use syntax compatible with Marshmallow 3.x/4.x (e.g., replace `fields.Str(description=...)` with `fields.Str(metadata={'description': '...'})` and similar adjustments for other removed arguments like `allow_none`).Modify custom plugin helper method signatures to include `**kwargs` (e.g., `def my_helper(self, obj, **kwargs):`).
Instead of `spec.components.schema('MySchema', schema=MySchema, extra_fields={'new_field': {'type': 'string'}}),` directly include all fields in your Marshmallow Schema or the component dictionary passed to `spec.components.schema`.Update your Marshmallow schema definitions to be compatible with Marshmallow 4.x. For example, change `fields.Str(description='...')` to `fields.Str(metadata={'description': '...'})` and `fields.Str(example='...')` to `fields.Str(metadata={'example': '...'})`. Alternatively, pin your Marshmallow version to `<4.0.0` (e.g., `marshmallow>=3.18.0,<4.0.0`) if you are unable to update your schema definitions.Ensure that each operation in your OpenAPI specification includes a 'responses' field with appropriate response definitions.
Use unique names for each component when registering them in the specification.
Ensure that all parameter definitions include the 'name', 'in', and 'schema' keys with appropriate values.
Implement the required method in your plugin or avoid calling unimplemented methods.
Validate your OpenAPI specification against the OpenAPI standard and correct any validation errors.