30

Is there a way to generate a RESTful API in Flask using Swagger?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • 2
    There is [this](https://github.com/rantav/flask-restful-swagger) if the API already exists or [this](https://github.com/wordnik/swagger-codegen) to *generate* one. As such though, this is very much a "suggest to me a library" question, which is much more appropriate for [chat] – Sean Vieira Jan 08 '14 at 14:40

8 Answers8

17

There is Flask-RESTPlus based on Flask-Restful until now. It provides some decorators to document your API and generate a Swagger specification, a Postman collection...

Next version will be able to generate the API from Swagger specifications.

noirbizarre
  • 3,429
  • 1
  • 30
  • 21
  • This is a great choice, it works just like restify, but includes swagger, and it includes the swagger UI. – nycynik Apr 15 '16 at 03:52
  • 3
    I only wish that @api.doc(...) and other Swagger doc decorators in Flask-RESTPlus 0.12 were documented better. I am spending HUGE time shooting in the dark to get the doc like I want it. – Bob Denny Nov 24 '18 at 17:15
  • There is an entire documentation page only for the documentation decorators: https://flask-restplus.readthedocs.io/en/stable/swagger.html If something is unclear or missing, please submit pull-requests (or ask) – noirbizarre Nov 24 '18 at 18:59
  • 1
    Thing is, to PR with the bits that are unclear and or missing, you need to know what the solution is. I spent ages earlier trying to make a field a password type input. Random things like that. Oh and trying to have an API spread across more than 1 file. Don't get me wrong this is my answer of choice for this problem, but it's sometimes harder than it should be to achieve things – Craig Brett Mar 12 '19 at 15:28
  • 2
    **IMPORTANT** to note that the Flask-RESTPlus project should be considered unmaintained and been forked to another side project. – MikeL Mar 22 '20 at 07:05
15

Flask is now supported from the Swagger Editor: http://editor.swagger.io/#/

Tommy
  • 12,588
  • 14
  • 59
  • 110
12

You can also use connexion

"Connexion is a framework on top of Flask to automagically handle your REST API requests based on Swagger 2.0 Specification files in YAML."

Tuxlife
  • 519
  • 5
  • 6
  • 2
    Connexion is based on OpenAPI 2.0 Specification. This is disappointing as OpenAPI has since moved on to 3.0! – Ron Kalian Oct 22 '18 at 08:58
  • 2
    Connexion 2 was recently released - it claims to implement OpenAPI 3.0. I found it adheres to the spec more closely if `connexion.App()` is defined with `options={'uri_parsing_class': OpenAPIURIParser }` param – Ron Kalian Nov 21 '18 at 11:13
4

You can use flask-restful-swagger.

Cosmo Harrigan
  • 895
  • 1
  • 8
  • 22
3

Flasgger is an extension to make automatic UI for Flask routes http://github.com/rochacbruno/flasgger

2

If you don't use the flask-restful extension you could take a look at flask-sillywalk

Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22
1

Nowadays there is also flask-apispec. It uses marshmallow to write the schemas/annotations that end up in the swagger output.

Rovanion
  • 4,382
  • 3
  • 29
  • 49
1

For Swagger 3.0:

Use fastapi.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    This may not be a correct answer (technically it uses Starlette instead of Flask), but it is IMHO by far the best. I tried all the other options and was dismayed that they fail to take advantage of modern Python. In contrast, the serialization, typing and routing with FastAPI are beautifully implemented, and being actively improved. Starlette is very similar to Flask, so there is barely any learning curve. – Ben Mares Jun 16 '20 at 13:22