Ravel Morrison opened the scoring within the first 20 seconds of the game and Matt Jarvis made it two for the Hammers after only 8 minutes.
The Hammers then missed a number of chances to put the game to bed before Craig Noone struck back for Cardiff before the break.
With only 15 minutes left, Peter Odemwingie tapped in to complete Cardiff’s fightback. The Hammers then dug deep and were rewarded when Ricardo Vaz Te headed home a fine winner with only minutes remaining.
The win puts the Hammers in to the fourth round of the Capital One Cup where they will visit Championship in-form side Burnley.