The Messenger of Allah ﷺ, the truthful and trusted one, told us, "Each one of you has his creation gathered together in his mother's womb for forty days. Then he becomes a clinging clot for a similar period, then a lump of flesh for a similar period. Then Allah sends an angel who is commanded with four matters. He is told to write down his deeds, his provision, his lifespan, and whether he will be wretched or blessed. Then the soul is breathed into him..." and the rest of the hadith.
حدثنا رسول الله ﷺ، وهو الصادق المصدوق، قال: إن أحدكم يجمع خلقه في بطن أمه أربعين يوما، ثم يكون علقة مثل ذلك، ثم يكون مضغة مثل ذلك، ثم يبعث الله ملكا، فيؤمر بأربع كلمات ويقال له: اكتب عمله، ورزقه، وأجله، وشقي أو سعيد. ثم ينفخ فيه الروح… الحديث.
Bukhari reported it in the Book of Divine Decree (6594), and Muslim in the Book of Divine Decree (2643), both through Amash, from Zayd bin Wahb, who said, Abdullah [i.e., narrated it], and mentioned it. The wording given here is Bukhari's, and Muslim's wording is similar to it. There are other hadiths on this subject, which have been mentioned in the Book of Faith.