title: Flask RestX with nested collections and items
body: <p>I'm looking for advice on a design pattern for creating a REST API using Flask (with flask restx) handling multiple collections (&quot;hub&quot;) which each have items (&quot;nodes&quot;).</p>
  <p>Example:
  HubA will have N nodes it manages.
  HubB will have M nodes it manages.
  ... so on and so forth.</p>
  <p>I want to create an API that allows someone to configure a hub and populate it with nodes and then configure and manage each node on that hub. With multiple hubs being in the system.</p>
  <p>My thoughts were to try something like this but I've been unsuccessful at breaking this up into anything other than one large python file with all the routes defined. Ideally I'd have a &quot;hubs&quot; routes file and a &quot;nodes&quot; route file. I've attempted using namespaces and nested blueprints but I seem to be flailing.</p>
  <pre><code>GET /api/hubs/    -    Retrieve a list of hubs
  POST /api/hubs/    -    Create a new hub
  
  GET   /api/hubs/{idh}    -    Retrieve hub with ID of {idh}
  PUT   /api/hubs/{idh}    -    Update hub with ID of {idh}
  DELETE   /api/hubs/{idh}    -    Delete hub with ID of {idh}
  
  GET /api/hubs/{idh}/nodes    -    Retrieve a list of nodes on hub {idh} with ID of {idn}
  POST /api/hubs/{idh}/nodes   -    Create a new node on hub {idh} with ID of {idn}
  
  GET   /api/hubs/{idh}/nodes/{idn}    -    Retrieve node with ID of {idn} on hub {idh}
  PUT   /api/hubs/{idh}/nodes/{idn}    -    Update node with ID of {idn} on hub {idh}
  DELETE   /api/hubs/{idh}/nodes/{idn}    -    Delete node with ID of {idn} on hub {idh}
  </code></pre>
  <p>Thanks to anyone who helps my comprehension/confusion. I'm not married to any idea currently. Here's what I've written and the swagger UI does not render at /hubs (TypeError)</p>
  <pre><code>project/
  ├── app.py
  └── apis
      ├── __init__.py
      ├── hubs
      |   ├── __init__.py
      |   └── routes.py
      └── nodes
          ├── __init__.py
          └── routes.py
  </code></pre>
  <p>app.py</p>
  <pre><code>from flask import Flask
  from apis import hubsblueprint as api
  
  app = Flask(__name__)
  app.register_blueprint(api)
  app.run(debug=True)
  </code></pre>
  <p>apis.init.py</p>
  <pre><code>from flask_restx import Api
  
  from .nodes.routes import nodesblueprint
  from .hubs.routes import hubsblueprint
  
  
  hubsblueprint.register_blueprint(nodesblueprint)
  
  api = Api(
      hubsblueprint,
      version=&quot;1.0&quot;,
      title=&quot;hubs and nodes API&quot;,
      description=&quot;A Rest API for managing the network&quot;)
  </code></pre>
  <p>apis.hubs.routes.py</p>
  <pre><code>from flask import Blueprint
  from flask_restx import Resource
  
  hubsblueprint = Blueprint('hubs', __name__, url_prefix='/hubs')
  
  @hubsblueprint.route(&quot;/&quot;)
  class HubsCollection(Resource):
      def get(self):
          pass
  
      def post(self):
          pass
  
  @hubsblueprint.route('/&lt;int:id&gt;')
  class HubsItem(Resource):
      def get(self):
          pass
  
      def put(self):
          pass
  
      def delete(self):
          pass
  </code></pre>
  <p>apis.nodes.routes.py</p>
  <pre><code>from flask import Blueprint
  from flask_restx import Resource
  
  nodesblueprint = Blueprint('nodes', __name__, url_prefix='/nodes')
  
  @nodesblueprint.route('/')
  class NodesCollection(Resource):
      def get(self):
          pass
  
      def post(self):
          pass
  
  @nodesblueprint.route('/&lt;int:id&gt;')
  class NodesItem(Resource):
      def get(self):
          pass
  
      def put(self):
          pass
  
      def delete(self):
          pass
  </code></pre>