Sam Allardyce's first competitive game as West Ham manager ended in defeat after a late goal from Kenny Miller.
The game seemed destined to end as a stalemate until Miller collected Rudy Gestede's pass before turning to fire past Robert Green.
The Hammers controlled most of the game but failed to find a way past David Marshall in the Cardiff goal.
Striker Frederic Piquionne came closest to scoring when he hit the post from Matt Taylor's low cross.
Both sides now have first round League Cup games, West Ham hosting Aldershot on Tuesday while Cardiff travel to Oxford on Wednesday evening.