Abdullah bin Masud reported,

The Messenger of Allah ﷺ, the truthful and trusted, told us, "Each one of you is gathered in his mother's womb for forty days, then becomes a clinging clot for the same period, then becomes a lump of flesh for the same period. Then Allah sends an angel to him with four commands, and he writes down his deeds, his lifespan, his provision, and whether he will be wretched or blessed. Then the soul is breathed into him.A man may perform the deeds of the people of the Fire until there is only an arm's length between him and it, then what was written overtakes him, so he performs the deeds of the people of Paradise and enters it. And a man may perform the deeds of the people of Paradise until there is only an arm's length between him and it, then what was written overtakes him, so he performs the deeds of the people of the Fire and enters it."

Bukhari 3332

Sahih

حدثنا عمر بن حفص حدثنا أبي حدثنا الأعمش حدثنا زيد بن وهب حدثنا

عبد الله

حدثنا رسول الله ﷺ وهو الصادق المصدوق إن أحدكم يجمع في بطن أمه أربعين يوما ثم يكون علقة مثل ذلك ثم يكون مضغة مثل ذلك ثم يبعث الله إليه ملكا بأربع كلمات فيكتب عمله وأجله ورزقه وشقي أو سعيد ثم ينفخ فيه الروح فإن الرجل ليعمل بعمل أهل النار حتى ما يكون بينه وبينها إلا ذراع فيسبق عليه الكتاب فيعمل بعمل أهل الجنة فيدخل الجنة وإن الرجل ليعمل بعمل أهل الجنة حتى ما يكون بينه وبينها إلا ذراع فيسبق عليه الكتاب فيعمل بعمل أهل النار فيدخل النار

البخاري 3332صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Hadiths of the prophets
Chapter
Creation of Adam, peace be upon him, and his descendants
Numbering
Darussalam 3332
Bukhari 3332