The Messenger of Allah ﷺ said to me, "When you go to your bed, perform ablution as you do for prayer, then lie down on your right side and say, 'O Allah, I have submitted my face to You, entrusted my affair to You, and turned my back to You, out of hope and fear of You. There is no refuge or escape from You except to You. I believe in Your Book which You revealed and in Your Prophet whom You sent.' If you die, you will die upon the natural disposition of faith. Make these the last words you speak."Bara said, "I tried to memorize them and I said, 'And in Your Messenger whom You sent.'"He replied, "No, say, 'And in Your Prophet whom You sent.'"
حدثنا مسدد حدثنا المعتمر قال سمعت منصورا يحدث عن سعد بن عبيدة قال
قال لي رسول الله ﷺ إذا أتيت مضجعك فتوضأ وضوءك للصلاة ثم اضطجع على شقك الأيمن وقل اللهم أسلمت وجهي إليك وفوضت أمري إليك وألجأت ظهري إليك رهبة ورغبة إليك لا ملجأ ولا منجى منك إلا إليك آمنت بكتابك الذي أنزلت ونبيك الذي أرسلت قال فإن مت مت على الفطرة واجعلهن آخر ما تقول قال البراء فقلت أستذكرهن فقلت وبرسولك الذي أرسلت قال لا ونبيك الذي أرسلت
Bukhari, Supplications, chapter "If He Spends the Night in a State of Purity," hadith 6311, from Musaddad, and Muslim, Remembrance and Supplication, chapter on supplication before sleep, hadith 2710, from the hadith of Mansur, through the same chain.