‘Ali reported that God’s messenger used to say when he lay down, "O God, I seek refuge in Thy noble Person and in Thy perfect words from the evil of what Thou seizest by its forelock; O God, Thou removest debt and sin; O God, Thy troop is not routed, Thy promise is not broken, and the riches of the rich do not avail against Thee. Glory and praise be unto Thee."
Hafsa said that when God’s messenger wanted to go to sleep he put his right hand under his cheek, and would then say three times, "O God, guard me from Thy punishment on the day when Thou raisest up Thy servants."
Abu Sa'id reported God's messenger as saying that if anyone says three times when going to bed, "I ask the forgiveness of God than whom there is no god, the Living, the Eternal, and I turn in repentance to Him," God will forgive him his sins, even if they are like the foam of the sea, or in number like the sand which is accumulated,* or as many as the leaves of the trees, or as numerous as the days of this world.
*The word is ‘alij. Some say ‘Alij is a place in the desert, the translation being ‘the sand of ‘Alij; others treat it as translated above.
Tirmidhi transmitted it, saying this is a gharib tradition.
Hudhaifa said that when the Prophet wanted to go to sleep he placed his hand under his head and said, "O God, guard me from thy punishment on the day when Thou gatherest Thy servants," or "raisest up Thy servants."
Tirmidhi transmitted it, and Ahmad gave al-Bara’ as his authority.