Fiqh-us-Sunnah Volume 4, Funerals and Dhikr, Fiqh 4.122.

Section : When One is Frightened During Sleep.

‘Umar b. Shu’aib reported from his father and he from his grandfather that the Prophet, peace be upon him, said, “When one of you is frightened during sleep he should say, ‘I seek protection in the perfect words of Allah, from His displeasure and punishment and from evil people, and from the (evil) prompting of devils and from their presence.’ Then nothing will harm him.” The narrator added, “Ibn ‘Umar used to teach this to his children, both grown up and young, and he would write it on a piece of paper and make them wear it around their necks.” The chain of authorities of this hadith is sound.

Khalid b. Walid reported that once he suffered from insomnia. The Prophet, peace be upon him, said to him, “Shall I teach you words that will make you go to sleep when you say them? Say, ‘O Allah, the Lord of the seven heavens and whatever they cover, Lord of the earths and whatever they contain, Creator of devils and whomever they mislead, be my protector from the evil of all Your creatures lest some of them may hasten with insolence against me or transgress the bounds. Honoured is he who is in Your protection and blessed be Your name, there is no god except You’.’ (Reported by At-Tabrani in his Al-Kahir and Al-Awsat. Its chain is sound, although Abdur-Rahman did not hear it from Khalid. Al-Hafiz al-Munzhari has mentioned it)

Bara’ b. ‘Azib reported that a man complained to the Prophet, peace be upon him, about fear and depression. The Prophet said to him, ‘Say, ‘Glory to Allah, the Holy, the King, Lord of the angels and of Gabriel. Your power and glory encompass the heavens and the earth.” The man said these words and Allah healed him of his depression and fear.