Abdullah bin Umar reported,

The Messenger of Allah ﷺ said, "When Allah عز وجل wills to create a soul, the angel of the wombs says, 'My Lord, male or female?' and Allah decrees His command to him. Then he says, 'My Lord, wretched or blessed?' and Allah decrees His command to him. Then it is written between his eyes everything he will encounter, even the stumble he will suffer."

Azami 1005

Sahih

عن ابن عمر، قال

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

الأعظمي 1005صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Faith
Chapter
Belief in the divine decree and predestination
Section
How the human is created in his mother's womb and the recording of his provision, lifespan, deeds, misery, and happiness
Numbering
Jami al-Kamil 1005 (2nd edition)
Azami 1005