refactor api code