Optimizing API queries