7

Could you please describe how to indicate the base URL in the documentation automatically generated by Flask Restplus?

I am running the following code but nothing shows up in the swagger UI:

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app,
    title='Data Quality Framework API',
    version='v0.1',
    doc='/data_quality_framework/api/documentation',
    contact='me@xxx.com',
    base_url='/test')

enter image description here

Alexis.Rolland
  • 4,378
  • 3
  • 32
  • 59
  • See also: [Why do I get a 404 despite having @app.route('/') when I have Flask-Restplus?](https://stackoverflow.com/q/56539574/562769) – Martin Thoma Jun 11 '19 at 14:24

3 Answers3

23

By default, when using this pattern (ie. with app as constructor argument), the API is on the root endpoint (ie. /) and the swagger documentation is on the API root (ie. still /).

You have multiple possibilities:

Use a blueprint to change the API root

If you want to keep the documentation on the API root but change the API root, use a blueprint to register you API where you want.

from flask import Flask, Blueprint
from flask_restplus import Api

app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/test')
api = Api(blueprint)
app.register_blueprint(blueprint)

assert url_for('api.doc') == '/test/'

Only change the documentation location

If you want to keep the API root at the same place but only move the documentation location, Flask-restplus allows you to specify it with the doc parameter.

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app, doc='/test/')

assert url_for('doc') == '/test/'

You can also combine both. See http://flask-restplus.readthedocs.io/en/stable/swagger.html#customization for more details on advanced documentation customization.

noirbizarre
  • 3,199
  • 1
  • 27
  • 21
  • Thank you, as discussed [here](https://github.com/noirbizarre/flask-restplus/issues/286) I was able to achieve this by removing the url_for itself and putting the documentation url directly in `api = Api(..., doc='/documentation')` – Alexis.Rolland May 30 '17 at 14:22
  • 1
    Hello Alexis and Axel, your conversation is little bit misleading because its incomplete, even in the conversation on github link. in simple words all that is needed is to have something like `url_prefix="/api"` in the creation of blueprint object and say `/doc` in doc parmater of api object creation time. so the final url becomes `/api/doc` and `Base URL` will be `/api` totally make sense. the documentation link added by Axel explains much better. – Ciasto piekarz Feb 23 '18 at 08:43
  • 1
    Yeah for me the assert line wasn't necessary: api = Api(app, doc='/api/v1', prefix="/api/v1") –  Nov 10 '18 at 06:50
3

you could define it with prefix parameter. I've tried it on version 0.13.0.

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app, prefix='/api')
Agung Wiyono
  • 114
  • 1
  • 8
-3

add it line

from flask import url_for
league3236
  • 15
  • 4