title: How to access api.url_for in big Flask-Restful API
body: <p>I'm working on a Restful API using Flask-Restful with more resources than I'd like to keep in my app.py. So I've applied the suggested <a href="https://flask-restful.readthedocs.org/en/0.3.3/intermediate-usage.html#project-structure" rel="nofollow">project structure</a>. Now I'd like to access <a href="https://flask-restful.readthedocs.org/en/0.3.3/api.html#flask_restful.Api.url_for" rel="nofollow">api.url_for()</a> from a resource to generate some links, but it seems I'd have to <code>from app import api</code> to do that.</p>
  
  <p>To avoid circular imports, my current solution is to do a lazy import. But there has be a better way, right?</p>
  
  <p><code>app.py</code>:</p>
  
  <pre><code>from flask import Flask
  from flask_restful import Api
  from myapi.resources.foo import Foo
  from myapi.resources.bar import Bar
  from myapi.resources.baz import Baz
  
  app = Flask(__name__)
  api = Api(app)
  
  api.add_resource(Foo, '/Foo', '/Foo/&lt;str:id&gt;')
  api.add_resource(Bar, '/Bar', '/Bar/&lt;str:id&gt;')
  api.add_resource(Baz, '/Baz', '/Baz/&lt;str:id&gt;')
  </code></pre>
  
  <p><code>resource/foo.py</code> (<code>bar.py</code> respectively):</p>
  
  <pre><code>from flask_restful import Resource
  from bar import Bar
  
  class Foo(Resource):
      def get(self):
          from app import api
          related = api.url_for(Bar, foo=self.id)
          return {'Foo':self.id, 'related_bar':related}, 200
  
      def post(self):
          pass
  </code></pre>