A former hospital porter sold drugs to pay for fertility treatment for his wife, Cardiff Crown Court heard.
Ian Woodhouse, 31, from Nantyglo, Blaenau Gwent, sold amphetamines and cannabis to help his wife Shelene.
He was given a nine-month jail term, suspended for two years, after he was caught with £1,000 of amphetamines, cannabis and £2,500 in cash.
Woodhouse, who worked at Nevill Hall Hospital, Abergavenny, was also told to do 70 hours community punishment.
Andrew Morse, defending, said: "This is a most unusual case.
"The reason he committed the offences was to fund fertility treatment for his wife.
"He didn't think before he acted and at its lowest level was extremely foolish."
The court heard how Woodhouse had since left Nevill Hall Hospital.
Recorder Peter Thomas told him: "You were on a knife-edge. You will never know how close you were to going to prison.
"Drugs such as these cause misery to thousands of people across the UK every day. That's why these drugs were illegal and you knew that.
"Working in the health service, you must have known the misery they cause.
"In my opinion you were clearly making a profit out of commercial dealings on a relatively low level."