Update to api.php

- Fixed bugs found while trying to use this api in a standalone fashion.