Rewrite api.php functionality to python.

Need additional testing.