From the beginning of the War of 1812, American military leaders were certain the British would attack Fort McHenry, which guarded the city of Baltimore. A local seamstress, Mary Young Pickersgill, was hired to fashion a large flag to be flown over the fort in defiance. The finished product was so large, 30 by 42 feet, that final assembly had to be accomplished in a brewery.
The British attack on Fort McHenry did not occur until September 13, 1814. At that time a Washington attorney, Francis Scott Key, visited the British fleet to secure the release of an American physician. Permission to take the prisoner was granted, but Key could not do so until the battle subsided. He spent a rainy night observing the British bombardment from several miles away and had no way of knowing if the Americans had managed to hold the fort. On the morning of the 14th, the American commander had the Pickersgill flag (which was not flown through the night) raised as proof that McHenry had not fallen. Key saw the flag through the rain and smoke, then immediately began work on a poem.
The poem was later set to the melody of a popular British song entitled, Anacreon in Heaven, which honored an ancient Greek poet who wrote of love and wine. The same melody had been used for other patriotic songs in America, including one written by Key in 1805. The song immediately became popular, but Yankee Doodle and Hail Columbia were for years performed at public occasions with the same regularity as The Star Spangled Banner.
Woodrow Wilson issued an executive order in 1916, designating the song as the national anthem and Congress passed legislation to that effect in 1931.
The name "Star Spangled Banner" applies to both the Pickersgill flag and the song. The flag was donated to the Smithsonian Institution in 1912.