Improved api.php

Made this file more dynamic and flexible