How to indicate base url in Flask Restplus documentation

Alexis.Rolland picture Alexis.Rolland · Apr 26, 2017 · Viewed 9.3k times · Source

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='[email protected]',
    base_url='/test')

enter image description here

Answer

noirbizarre picture noirbizarre · May 30, 2017

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.