The Messenger of Allah ﷺ, the truthful and trustworthy, told us, "The creation of each one of you is gathered in his mother's womb for forty days and forty nights. Then he becomes a clot of blood for a similar period, then a lump of flesh for a similar period. Then an angel is sent to him who is permitted four words, and he writes down his provision, his lifespan, his deeds, and whether he will be wretched or blessed. Then the soul is breathed into him.One of you may perform the deeds of the people of Paradise until there is only an arm's length between him and it, then what is written overtakes him, so he performs the deeds of the people of the Fire and enters it. And one of you may perform the deeds of the people of the Fire until there is only an arm's length between him and it, then what is written overtakes him, so he performs the deeds of the people of Paradise and enters it."
حدثنا رسول الله ﷺ وهو الصادق المصدوق: إن خلق أحدكم يجمع في بطن أمه أربعين يوما وأربعين ليلة، ثم يكون علقة مثله، ثم يكون مضغة مثله، ثم يبعث إليه الملك فيؤذن بأربع كلمات فيكتب: رزقه، وأجله، وعمله، وشقي أم سعيد، ثم ينفخ فيه الروح، فإن أحدكم ليعمل بعمل أهل الجنة حتى لا يكون بينها وبينه إلا ذراع فيسبق عليه الكتاب فيعمل بعمل أهل النار فيدخل النار، وإن أحدكم ليعمل بعمل أهل النار حتى ما يكون بينها وبينه إلا ذراع فيسبق عليه الكتاب، فيعمل عمل أهل الجنة فيدخلها.
Bukhari narrated it in the Book of Tawhid (7454), and Muslim in the Book of Divine Decree (2643), both from the hadith of Shubah, who said, Amash narrated to us, I heard Zayd bin Wahb, I heard Abdullah bin Masud, and he related the hadith. The wording given is Bukhari's, and Muslim's wording is similar.