API endpoints: Remove duplicate code