added api error handling