factor out api handling