This is difficult to establish, but the first recorded instance of the story popping up as one of the tales in “One Thousand and One Nights,” an old Middle-Eastern book of tales, is when Antoine Galland added it to his French translation. Most speculate that it has been part of the original group of tales since it was written, but there has not been a surviving document to verify this theory.
Since Galland’s addition of the tale in his translation in 1704, the story has been retold and changed many times. One of the most prominent retellings is actually Disney’s “Aladdin and the King of Thieves,” the final movie in the Aladdin trilogy. The story of the forty thieves is the story that is retold in this movie, even though it is significantly different than the original version.