Complete front-end code and API function