Refactored api into a seperate file. Return JSON on api request