| 1 |
Quickstart |
| 2 |
========== |
| 3 |
|
| 4 |
Basic Usage |
| 5 |
----------- |
| 6 |
|
| 7 |
First, create an `APISpec <apispec.APISpec>` object, passing basic information about your API. |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
from apispec import APISpec |
| 12 |
|
| 13 |
spec = APISpec( |
| 14 |
title="Gisty", |
| 15 |
version="1.0.0", |
| 16 |
openapi_version="3.0.2", |
| 17 |
info=dict(description="A minimal gist API"), |
| 18 |
) |
| 19 |
|
| 20 |
Add schemas to your spec using `spec.components.schema <apispec.core.Components.schema>`. |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
spec.components.schema( |
| 25 |
"Gist", |
| 26 |
{ |
| 27 |
"properties": { |
| 28 |
"id": {"type": "integer", "format": "int64"}, |
| 29 |
"name": {"type": "string"}, |
| 30 |
} |
| 31 |
}, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
Add paths to your spec using `path <apispec.APISpec.path>`. |
| 36 |
|
| 37 |
.. code-block:: python |
| 38 |
|
| 39 |
|
| 40 |
spec.path( |
| 41 |
path="/gist/{gist_id}", |
| 42 |
operations=dict( |
| 43 |
get=dict( |
| 44 |
responses={"200": {"content": {"application/json": {"schema": "Gist"}}}} |
| 45 |
) |
| 46 |
), |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
The API is chainable, allowing you to combine multiple method calls in |
| 51 |
one statement: |
| 52 |
|
| 53 |
.. code-block:: python |
| 54 |
|
| 55 |
spec.path(...).path(...).tag(...) |
| 56 |
|
| 57 |
spec.components.schema(...).parameter(...) |
| 58 |
|
| 59 |
To output your OpenAPI spec, invoke the `to_dict <apispec.APISpec.to_dict>` method. |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
from pprint import pprint |
| 64 |
|
| 65 |
pprint(spec.to_dict()) |
| 66 |
# {'components': {'parameters': {}, |
| 67 |
# 'responses': {}, |
| 68 |
# 'schemas': {'Gist': {'properties': {'id': {'format': 'int64', |
| 69 |
# 'type': 'integer'}, |
| 70 |
# 'name': {'type': 'string'}}}}}, |
| 71 |
# 'info': {'description': 'A minimal gist API', |
| 72 |
# 'title': 'Gisty', |
| 73 |
# 'version': '1.0.0'}, |
| 74 |
# 'openapi': '3.0.2', |
| 75 |
# 'paths': {'/gist/{gist_id}': |
| 76 |
# {'get': {'responses': {'200': {'content': {'application/json': {'schema': {'$ref': '#/definitions/Gist'}}}}}}}}, |
| 77 |
# 'tags': []} |
| 78 |
|
| 79 |
Use `to_yaml <apispec.APISpec.to_yaml>` to export your spec to YAML. |
| 80 |
|
| 81 |
.. code-block:: python |
| 82 |
|
| 83 |
print(spec.to_yaml()) |
| 84 |
# components: |
| 85 |
# parameters: {} |
| 86 |
# responses: {} |
| 87 |
# schemas: |
| 88 |
# Gist: |
| 89 |
# properties: |
| 90 |
# id: {format: int64, type: integer} |
| 91 |
# name: {type: string} |
| 92 |
# info: {description: A minimal gist API, title: Gisty, version: 1.0.0} |
| 93 |
# openapi: 3.0.2 |
| 94 |
# paths: |
| 95 |
# /gist/{gist_id}: |
| 96 |
# get: |
| 97 |
# responses: |
| 98 |
# '200': |
| 99 |
# content: |
| 100 |
# application/json: |
| 101 |
# schema: {$ref: '#/definitions/Gist'} |
| 102 |
# tags: [] |
| 103 |
|
| 104 |
.. seealso:: |
| 105 |
For a full reference of the `APISpec <apispec.APISpec>` class, see the :doc:`Core API Reference <api_core>`. |
| 106 |
|
| 107 |
|
| 108 |
Next Steps |
| 109 |
---------- |
| 110 |
|
| 111 |
We've learned how to programmatically construct an OpenAPI spec, but defining our entities was verbose. |
| 112 |
|
| 113 |
In the next section, we'll learn how to let plugins do the dirty work: :doc:`Using Plugins <using_plugins>`. |