Sam Allardyce was able to celebrate his 100th match in charge of West Ham with a victory - but it was nothing like as comfortable as it should have been.
The Hammers were 2-0 up and cruising against Cardiff with just seven minutes gone thanks to goals from Ravel Morrison - after just 20 seconds - and Matt Jarvis.
But they wasted a host of chances to kill City off, and the team who inflicted a home defeat in Allardyce's first game at the helm two years ago threatened to repeat the trick as they drew level through Craig Noone and Peter Odemwingie.
That was until Ricardo Vaz Te popped up with a late winner to secure a 3-2 win and ensure Allardyce could toast his century in style.
"It should have been all over at half-time," admitted assistant boss Neil MacDonald.
"That's the most disappointing thing. We played some brilliant stuff in the first half, we got the early goals and created chance after chance after chance, but we didn't finish them off.
"We probably thought it was too easy and took our foot of the pedal. And Cardiff scored a great goal just before half-time.
"When you are on the back foot after conceding just before half-time it changes everything. People come in expecting a pat on the back but instead we gave them a talking-to.
"To be fair to Cardiff they got the equaliser but we stuck together, we worked hard for each other and we'll take a late winner and a 3-2 win.
"It's always good to win and Sam is pleased we are through, although he's slightly disappointed with some aspects."
There was little sign of the drama to come when Morrison stunned the visitors with a goal straight from their own kick-off, taking Jarvis' pass in his stride before firing home.
Jarvis doubled the lead from close range and James Collison, Modibo Maiga and Vaz Te all missed chances to put the game to bed.
Instead, with their first attack, Noone pulled one back on the stroke of half-time with a superb left-foot curler.
And in the 75th minute former West Brom striker Odemwingie, making his full debut as one of 11 changes to City's starting line-up, levelled with his first goal since his infamous transfer deadline day drive to QPR eight months ago.
But Vaz Te, who himself asked for a transfer during the last window, had the final word when he nodded in Collison's cross with two minutes left.
"We had a mountain to climb with the way we started," admitted City manager Malky Mackay.
"I thought we were a shadow of ourselves in that first period but we scored a beautiful goal on the stroke of half-time to bring ourselves back into it
"We had a conversation at half-time and certainly put West Ham on the back foot in the second half.
"To quieten the crowd to that extent at 2-2, I'm very proud of the players."