In the Mexican–American War, Anderson took part in the Siege of Veracruz in March 1847 and then skirmishing near La Hoya on June 9. He fought in the Battle of Contreras on August 19, the skirmish near San Agustin Altapulco the following day, and the Battle of Molino del Rey on September 8. For gallantry during the fighting near San Agustin, he was brevetted to the rank of first lieutenant as of August 17. Anderson also participated in the fight for and capture of Mexico City from September 12–14.