This bedtime prayer appears to have originated during the Middle to Modern English period and is possibly derived from a prayer sometimes referred to as the White Paternoster or Matthew, Mark, Luke and John. There are several variations of different lengths.
The Kriat Shema al Hamitah is a Jewish prayer asking four angels for protection, recited at bedtime. The requirements for this prayer come from the Old Testament Book of Deuteronomy 6:7, 11:9, and the prayer itself may be from the medieval period. In mid-sixteenth century England, the White Paternoster was a common child's nursery rhyme and prayer asking four saints to provide protection during sleep. The White Paternoster has elements in common with the Bedtime Shema.
Possibly the earliest printed version of the prayer was written by Joseph Addison, an essayist, poet and playwright. Addison, the son of a reverend, co-founded The Spectator magazine, and a version of the prayer appears in an essay he wrote on March 8. 1711.
The website blogs.ancientfaith.com and an article in the New Haven Register both reference Matthew, Mark, Luke and John as the original version. It appears in the 1737 edition of Thomas Fleet's New England Primer. A nursery rhyme version appeared in 1840 in London Jingles.
Most of the early versions include lines like "If I should die before I wake" or "If I should sleep and never wake." Later versions omitted the references to death.