Move API code to api.py