The short answer: melatonin is not routinely recommended during pregnancy, mainly because there isn't enough safety data. The NHS says it's "not usually recommended" while pregnant, and major bodies don't endorse it for pregnant people. If you're struggling with sleep, talk to your provider before starting or continuing it.
Melatonin is a hormone your body makes naturally to help regulate your sleep-wake cycle. As a supplement, it's sold over the counter in the US to help with sleep, though in the UK the NHS notes it's normally a prescription medicine. Your body already produces melatonin during pregnancy, and levels naturally rise as pregnancy progresses.
The main issue is a lack of good safety data, not clear proof of harm. The NHS states melatonin is "not usually recommended" in pregnancy because little is known about its effect on babies. Melatonin can cross the placenta, and researchers note we don't yet fully understand how added doses might affect a developing baby's own rhythms. Because supplement doses can be far higher than what the body makes, most authorities take a cautious stance until more is known.
If you're already taking melatonin and find out you're pregnant, the NHS advises speaking to your doctor rather than stopping on your own, so you can decide together. For sleep trouble in pregnancy, providers often start with non-drug approaches first, such as sleep-hygiene habits, and can recommend options they consider safer for you. When breastfeeding, the NHS says melatonin can be used if your baby is healthy, but advises against co-sleeping while taking it and suggests watching for unusual drowsiness in the baby.