Sam Allardyce's side raced into a two-goal lead but was pegged back by Craig Noone and Peter Odemwingie before the out-of-favor Portuguese striker netted late.
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 favor.
Craig Noone pulled one back with a wonder strike just prior to the break before Peter Odemwingie – in his full debut – looked to have sent the game to extra time with a 76th-minute equalizer.
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 prioritizing 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 11 from the 1-0 defeat against Tottenham, with Odemwingie handed his full debut.
Straight from the kickoff 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 a shot in, with the help of a deflection.
Cardiff was 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 halftime when he cut inside and arrowed a superb effort into the top corner.
Having barely troubled the home defense prior to that strike, Cardiff came out with plenty of purpose after the break, although the team was thankful to goalkeeper Joe Lewis for keeping it 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 end line and cut the ball back. But with Vaz Te ready to pounce, the shot-stopper turned the ball away for a corner.
West Ham was 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 defense, and when he beat two men and squared across the box, the former West Brom man Odemwingie was on hand to score the equalizer.
But just as the game appeared to be heading for extra time, Vaz Te had the final say, with Collison producing a glorious cross into the box that was finished emphatically.