Move span logic into api layer