API middleware