Skip to content

Instantly share code, notes, and snippets.

@goodki-d
Last active January 9, 2025 08:12
Show Gist options
  • Save goodki-d/fa4d1f7ad14303fa83faf7c1e6eaf77f to your computer and use it in GitHub Desktop.
Save goodki-d/fa4d1f7ad14303fa83faf7c1e6eaf77f to your computer and use it in GitHub Desktop.
Build OpenAPI spec for a sanic app without starting the server
import json
from sanic import Sanic
from sanic_ext.extensions.openapi.builders import SpecificationBuilder
app = Sanic(__name__)
def get_spec(a: Sanic):
return SpecificationBuilder().build(a).serialize()
@app.command
def openapi():
with open("./openapi.json", "w") as f:
f.write(json.dumps(get_spec(app)))
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment