Sam Allardyce's side raced into a two-goal lead but were pegged back by Craig Noone and Peter Odemwingie before the out-of-favour Portuguese striker netted late on
A goal after just 19 seconds from Ravel Morrison set the Londoners on their way to victory over their Premier League rivals at Upton Park.
Matt Jarvis had a hand in that opener and scored a second himself seven minutes later after Morrison and Ricardo Vaz Te had returned the favour.
Craig Noone pulled one back with a wonder strike just prior to the break before Peter Odemwingie – on his full debut – looked to have sent the game to extra time with a 76th-minute equaliser.
However, with two minutes left the hosts snatched the win when Jack Collison produced a superb cross and Vaz Te applied the finish.
With both managers clearly prioritising Premier League points, there were a raft of changes.
West Ham retained just four starters from the weekend defeat to Everton, with Vaz Te and Collison among those given a chance to shine.
Cardiff boss Malky Mackay, meanwhile, named an entirely fresh starting XI from the 1-0 defeat against Tottenham, with Odemwingie handed his full debut.
Straight from the kick-off the visitors found themselves chasing the game as Jarvis burst forward and steered the ball into path of Morrison, who reacted smartly to lash home the opener.
And seven minutes later things got even better for the hosts, when Morrison combined with Vaz Te to find Jarvis on the left of the box and he drilled in, with the help of a deflection.
Cardiff were reeling and before the break the game could have been all but over, as first Modibo Maiga robbed Mark Hudson and shot wide of the far post after 18 minutes before Collison's downward header 13 minutes from the interval was hacked away virtually on the line.
But Noone gave them hope on the stroke of half-time when he cut inside and arrowed a superb effort into the top corner.
Having barely troubled the home defence prior to that strike, Cardiff came out with plenty of purpose after the break, although they were thankful to goalkeeper Joe Lewis for keeping them in touch just after the hour mark when he tipped Collison's shot over crossbar.
Lewis was called into action again soon after when Jarvis got to the byline and cut the ball back, but with Vaz Te ready to pounce, the shot-stopper turned the ball away for corner.
West Ham were made to pay for those wasted opportunities after 76 minutes as Odemwingie made his mark in style.
Noone was the architect when he was allowed to run unopposed at the home defence and when he beat two men and squared across the box, the former West Brom man Odemwingie was on hand to score the equaliser.
But, just as the game appeared to be heading for extra time, Vaz Te had the final say – Collison producing a glorious cross into the box that was finished emphatically.