The Messenger of Allah ﷺ, who is the truthful and the trusted, told us, "Each one of you has his creation gathered in his mother's womb for forty days, then he becomes a clinging clot for the same period, then a lump of flesh for the same period. Then Allah sends an angel who is commanded with four words. He is told to write down his deeds, his provision, his lifespan, and whether he will be wretched or happy. Then the soul is breathed into him.A man among you may perform the deeds of the people of Paradise until there is only an arm's length between him and Paradise, then what was written for him overtakes him and he performs the deeds of the people of the Fire. And a man may perform the deeds of the people of the Fire until there is only an arm's length between him and the Fire, then what was written overtakes him and he performs the deeds of the people of Paradise."
حدثنا رسول الله ﷺ وهو الصادق المصدوق، قال: إن أحدكم يجمع خلقه في بطن أمه أربعين يوما، ثم يكون علقة مثل ذلك، ثم يكون مضغة مثل ذلك، ثم يبعث الله ملكا، فيؤمر بأربع كلمات، ويقال له: اكتب عمله ورزقه وأجله وشقي أو سعيد، ثم ينفخ فيه الروح، فإن الرجل منكم ليعمل حتى ما يكون بينه وبين الجنة إلا ذراع، فيسبق عليه كتابه، فيعمل بعمل أهل النار، ويعمل حتى ما يكون بينه وبين النار إلا ذراع فيسبق عليه الكتاب، فيعمل بعمل أهل الجنة.
Bukhari narrated it in the Beginning of Creation (3208), and Muslim in Divine Decree (2643), both through routes from Amash, from Zayd bin Wahb, from Abdullah bin Masud, who mentioned it.