API Reference¶
This page documents the public runtime API exposed by azure-functions-openapi.
Import from package root
All symbols below are exported from azure_functions_openapi.__init__, so you can import from azure_functions_openapi directly.
from azure_functions_openapi import (
OPENAPI_VERSION_3_0,
OPENAPI_VERSION_3_1,
generate_openapi_spec,
get_openapi_json,
get_openapi_yaml,
openapi,
render_swagger_ui,
)
Public API surface¶
| Symbol | Kind | Purpose |
|---|---|---|
openapi |
decorator | Attach operation metadata to function handlers |
generate_openapi_spec |
function | Build OpenAPI dictionary from decorator registry |
get_openapi_json |
function | Build OpenAPI and serialize to JSON string |
get_openapi_yaml |
function | Build OpenAPI and serialize to YAML string |
render_swagger_ui |
function | Return Swagger UI HttpResponse |
OPENAPI_VERSION_3_0 |
constant | OpenAPI version string "3.0.0" |
OPENAPI_VERSION_3_1 |
constant | OpenAPI version string "3.1.0" |
Decorator behavior model¶
@openapi stores metadata in a thread-safe registry and the spec functions read from that registry to generate output.
@openapi metadata -> internal registry -> generate_openapi_spec -> JSON/YAML endpoint
-> render_swagger_ui (browser docs)
Note
get_openapi_json() and get_openapi_yaml() return strings, not HttpResponse. Wrap the returned value in func.HttpResponse in your Azure Function route.
Common usage patterns¶
Minimal endpoint¶
@app.route(route="ping", methods=["GET"])
@openapi(summary="Ping", description="Health check endpoint")
def ping(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse("ok", status_code=200)
With Pydantic request and response¶
class CreateItemRequest(BaseModel):
name: str
class ItemResponse(BaseModel):
id: int
name: str
@app.route(route="items", methods=["POST"])
@openapi(
summary="Create item",
method="post",
route="/api/items",
request_model=CreateItemRequest,
response_model=ItemResponse,
response={201: {"description": "Created"}},
)
def create_item(req: func.HttpRequest) -> func.HttpResponse:
...
With raw schema dictionaries¶
@app.route(route="raw", methods=["POST"])
@openapi(
summary="Raw schema example",
method="post",
request_body={
"type": "object",
"properties": {"value": {"type": "string"}},
"required": ["value"],
},
response={
200: {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {"accepted": {"type": "boolean"}},
}
}
},
}
},
)
def raw(req: func.HttpRequest) -> func.HttpResponse:
...
Expose OpenAPI + Swagger routes¶
@app.route(route="openapi.json", methods=["GET"])
def openapi_json(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse(get_openapi_json(title="My API", version="1.0.0"), mimetype="application/json")
@app.route(route="openapi.yaml", methods=["GET"])
def openapi_yaml(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse(get_openapi_yaml(title="My API", version="1.0.0"), mimetype="application/x-yaml")
@app.route(route="docs", methods=["GET"])
def docs(req: func.HttpRequest) -> func.HttpResponse:
return render_swagger_ui(title="My API Docs", openapi_url="/api/openapi.json")
mkdocstrings reference¶
The sections below are generated directly from source docstrings.
openapi¶
generate_openapi_spec(title='API', version='1.0.0', openapi_version=OPENAPI_VERSION_3_0, description=DEFAULT_OPENAPI_INFO_DESCRIPTION, security_schemes=None)
¶
Compile an OpenAPI specification from the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
API title |
'API'
|
version
|
str
|
API version |
'1.0.0'
|
openapi_version
|
str
|
OpenAPI specification version ("3.0.0" or "3.1.0") |
OPENAPI_VERSION_3_0
|
description
|
str
|
Description for the OpenAPI info object |
DEFAULT_OPENAPI_INFO_DESCRIPTION
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions for components.securitySchemes. Example: {"BearerAuth": {"type": "http", "scheme": "bearer"}} |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
OpenAPI specification dictionary |
Source code in src/azure_functions_openapi/openapi.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
get_openapi_json(title='API', version='1.0.0', openapi_version=OPENAPI_VERSION_3_0, description=DEFAULT_OPENAPI_INFO_DESCRIPTION, security_schemes=None)
¶
Return the spec as pretty-printed JSON (UTF-8).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
API title |
'API'
|
version
|
str
|
API version |
'1.0.0'
|
openapi_version
|
str
|
OpenAPI specification version ("3.0.0" or "3.1.0") |
OPENAPI_VERSION_3_0
|
description
|
str
|
Description for the OpenAPI info object |
DEFAULT_OPENAPI_INFO_DESCRIPTION
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions for components.securitySchemes. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
OpenAPI spec in JSON format. |
Source code in src/azure_functions_openapi/openapi.py
get_openapi_yaml(title='API', version='1.0.0', openapi_version=OPENAPI_VERSION_3_0, description=DEFAULT_OPENAPI_INFO_DESCRIPTION, security_schemes=None)
¶
Return the spec as YAML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
API title |
'API'
|
version
|
str
|
API version |
'1.0.0'
|
openapi_version
|
str
|
OpenAPI specification version ("3.0.0" or "3.1.0") |
OPENAPI_VERSION_3_0
|
description
|
str
|
Description for the OpenAPI info object |
DEFAULT_OPENAPI_INFO_DESCRIPTION
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions for components.securitySchemes. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
OpenAPI spec in YAML format. |
Source code in src/azure_functions_openapi/openapi.py
generate_openapi_spec¶
Compile an OpenAPI specification from the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
API title |
'API'
|
version
|
str
|
API version |
'1.0.0'
|
openapi_version
|
str
|
OpenAPI specification version ("3.0.0" or "3.1.0") |
OPENAPI_VERSION_3_0
|
description
|
str
|
Description for the OpenAPI info object |
DEFAULT_OPENAPI_INFO_DESCRIPTION
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions for components.securitySchemes. Example: {"BearerAuth": {"type": "http", "scheme": "bearer"}} |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
OpenAPI specification dictionary |
Source code in src/azure_functions_openapi/openapi.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
get_openapi_json¶
Return the spec as pretty-printed JSON (UTF-8).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
API title |
'API'
|
version
|
str
|
API version |
'1.0.0'
|
openapi_version
|
str
|
OpenAPI specification version ("3.0.0" or "3.1.0") |
OPENAPI_VERSION_3_0
|
description
|
str
|
Description for the OpenAPI info object |
DEFAULT_OPENAPI_INFO_DESCRIPTION
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions for components.securitySchemes. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
OpenAPI spec in JSON format. |
Source code in src/azure_functions_openapi/openapi.py
get_openapi_yaml¶
Return the spec as YAML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
API title |
'API'
|
version
|
str
|
API version |
'1.0.0'
|
openapi_version
|
str
|
OpenAPI specification version ("3.0.0" or "3.1.0") |
OPENAPI_VERSION_3_0
|
description
|
str
|
Description for the OpenAPI info object |
DEFAULT_OPENAPI_INFO_DESCRIPTION
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions for components.securitySchemes. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
OpenAPI spec in YAML format. |
Source code in src/azure_functions_openapi/openapi.py
render_swagger_ui¶
Render Swagger UI with enhanced security headers and CSP protection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Page title for the Swagger UI |
'API Documentation'
|
openapi_url
|
str
|
URL to the OpenAPI specification |
'/api/openapi.json'
|
custom_csp
|
str | None
|
Custom Content Security Policy (optional) |
None
|
enable_client_logging
|
bool
|
Whether to enable browser-side response logging |
False
|
Returns:
| Type | Description |
|---|---|
HttpResponse
|
HttpResponse with Swagger UI HTML and security headers |
Source code in src/azure_functions_openapi/swagger_ui.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
Related internals¶
While not part of the top-level runtime import list for app code, these internals are useful when debugging:
- Registry accessor:
azure_functions_openapi.decorator.get_openapi_registry - Route sanitizer:
azure_functions_openapi.utils.validate_route_path - Operation ID sanitizer:
azure_functions_openapi.utils.sanitize_operation_id
Version constants¶
Use these constants for explicit version selection:
from azure_functions_openapi import OPENAPI_VERSION_3_0, OPENAPI_VERSION_3_1
spec_30 = get_openapi_json(openapi_version=OPENAPI_VERSION_3_0)
spec_31 = get_openapi_json(openapi_version=OPENAPI_VERSION_3_1)
Tip
Prefer constants over hardcoded strings to avoid typos and keep version intent explicit in code review.