title: Accessing app object from outside app context in Flask application
body: <p>I have set a decorator for functions of my routes to abort(401) if api-key is invalid (or not specified) inside headers of requests.</p>
  <p>So I define this function in a dedicated file of my main module (named app) and it is imported inside all of my blueprints definition. But from this file, I have no access to app object to load the API key stored in config. I tried to use current_app, but I have an error telling me I cannot use it outside of the app context. I cannot import directly app object from app module either as it results with a circular importation.</p>
  <p>If I declare the decorator inside <code>__init__.py</code> file, I will be faced with the same issue: I will not be able to import require_apikey function from app module as blueprint modules are already imported in app module for blueprint registration.</p>
  <p>I assume there are some issues with my design. Could you point my flaws and help me fixing it ?</p>
  <p>My project directory looks like this:</p>
  <pre><code>project/
   - run.py
   - app/
   - - __init__.py
   - - player.py
   - - require_apikey.py
  </code></pre>
  <p>And here is the content of these files:</p>
  <pre><code># run.py
  from app import app
  
  if __name__ == &quot;__main__&quot;:
      app.run()
  </code></pre>
  <pre><code># app/__init__.py
  
  from flask import (
      Flask,
      render_template,
      jsonify)
  
  from app.db import db
  
  from app.player import player
  
  app = Flask(__name__)
  app.config.from_object(&quot;app.config.Config&quot;)
  app.register_blueprint(player, url_prefix=&quot;/player/&quot;)
  
  db.init_app(app)
  
  with app.app_context():
      db.create_all()
  </code></pre>
  <pre><code># app/player.py
  
  from flask import (
      Blueprint,
      request,
      abort,
      jsonify)
  
  from app import require_apikey
  
  from app.models.player import Player
  
  from app.db import db
  
  player = Blueprint(__name__, __name__)
  
  @player.route('/', methods=[&quot;GET&quot;, &quot;POST&quot;])
  @require_apikey
  def root():
      # Do some stuff
  </code></pre>
  <pre><code># app/require_apikey.py
  
  from functools import wraps
  from flask import (
      request,
      abort,
      current_app
  )
  
  API_KEY = current_app.config[&quot;SECRET_KEY&quot;]
  
  def require_apikey(view_function):
      @wraps(view_function)
      def decorated_function(*args, **kwargs):
          if request.headers.get(&quot;api-key&quot;) and request.headers.get(&quot;api-key&quot;) == API_KEY:
              return view_function(*args, **kwargs)
          else:
              abort(401, &quot;Invalid API key&quot;)
      return decorated_function
  </code></pre>