Quick API Auth for Flask

Saturday, March 21, 2020

A quick decorator function for api authorization on Flask endpoints

def require_appkey(view_function):
    def decorated_function(*args, **kwargs):
        if request.headers.get('x-api-key') and request.headers.get('x-api-key') == key:
            return view_function(*args, **kwargs)
    return decorated_function

Simply put this on your methods and it will require the API key for authorization.

@app.route('/create', methods=['POST'])
def create():

  return 'Your function here'

Easy, and doesn’t require any more dependancies or packages.


Use JQuery to Write Data to a Variable

JSON to Data Object in Python