Francis Scott Key wrote the “Star-Spangled Banner” in 1814, shortly before the end of the War of 1812. In actuality, Key didn’t write the “Star-Spangled Banner,” he wrote a poem, “The Defence (sic) of Fort McHenry,” part of which became our national anthem. Key, a Georgetown lawyer, was sent to Baltimore by President James Madison to negotiate the release of an American physician, Dr. William Beanes, who was held prisoner by the British following the Battle of Bladensburg. Key and another negotiator, State Department lawyer John Stuart Skinner, were on an American flag-of-truce ship, President, the morning of September 13, 1814, when British warships in Chesapeake Bay launched a 25-hour bombardment of Fort McHenry, which guarded Baltimore Harbor.
Fearing that Key and Skinner would warn the Americans of the pending attack, the British frigate HMS Surprise prevented the American ship from leaving the area, forcing it to drop anchor in the mouth of the Patapsco River until the siege and battle ended. In reality, being “held hostage” during the battle was one of the terms of Beanes’ release. Regardless, Key and Skinner witnessed the battle from the decks of the truce ship. One can only image their anxiety watching the British fleet bombard the American fort throughout the night. According to (faulty) British estimations, Fort McHenry and the city of Baltimore would fall in a matter of hours.
When dawn rose the following morning, the American flag was still flying over Fort McHenry. In actuality, it wasn’t the same flag that Key saw at the beginning of the battle. There are various stories but the flag Key saw at dawn was a large garrison flag, not the smaller battle flag that flew throughout the night.
Unsure of American troop strength, British soldiers under the command of Colonel Arthur Brooke—who planned to bypass Fort McHenry and attack Baltimore—returned to the fleet. Key’s truce ship was released as the British sailed for New Orleans shortly thereafter. Inspired by what he witnessed, Key jotted down some notes and supposedly finished writing his poem, “The Defence of Fort McHenry,” within a day of returning home. The poem was published in the Baltimore Patriot and Evening Advertiser on September 20, 1814. There is evidence that Key suggested setting the poem to music and ironically used the melody of “To Anacreon in Heaven,” a popular British drinking song at the time.
Unofficially, the “Star Spangled Banner” was viewed as a national hymn in the nineteenth century. During the Civil War, 1861, Oliver Wendell Holmes added a fifth stanza to the hymn in support of the Union cause. It was subsequently removed. In 1916, Woodrow Wilson issued an executive order proclaiming the “Star Spangled Banner” the national anthem to little effect. It wasn’t until 15 years later, 1931, that congress passed (and Herbert Hoover signed) legislation formally declaring the “Star Spangled Banner” our national anthem.
I think it only fair to give full coverage to Key’s poetic talents. Here is the full poem from which the national anthem is derived both in transcription and an original penned copy.
The Defence of Fort McHenry
What so proudly we hailed at the twilight’s last gleaming?
Whose broad stripes and bright stars through the perilous fight,
O’er the ramparts we watched were so gallantly streaming?
And the rockets’ red glare, the bombs bursting in air,
Gave proof through the night that our flag was still there.
O! say does that star-spangled banner yet wave
O’er the land of the free and the home of the brave?
On the shore, dimly seen through the mists of the deep,
Where the foe’s haughty host in dread silence reposes,
What is that which the breeze, o’er the towering steep,
As it fitfully blows, half conceals, half discloses?
Now it catches the gleam of the morning’s first beam,
In full glory reflected now shines in the stream:
’Tis the star-spangled banner! Oh long may it wave
O’er the land of the free and the home of the brave.
And where is that band who so vauntingly swore
That the havoc of war and the battle’s confusion,
A home and a country should leave us no more!
Their blood has washed out their foul footsteps’ pollution.
No refuge could save the hireling and slave
From the terror of flight, or the gloom of the grave:
And the star-spangled banner in triumph doth wave
O’er the land of the free and the home of the brave.
O! thus be it ever, when freemen shall stand
Between their loved home and the war’s desolation!
Blest with victory and peace, may the heav’n rescued land
Praise the Power that hath made and preserved us a nation.
Then conquer we must, when our cause it is just,
And this be our motto: ’In God is our trust.’
And the star-spangled banner in triumph shall wave
O’er the land of the free and the home of the brave! 
 An original broadside can be seen at http://www.mdhs.org/digitalimage/defence-sic-fort-mhenry. Historians still argue whether or not Key intended to write a poem or a song.
 There are several good, easy to read books on the history of the battle of Baltimore, the attack on Fort McHenry, and the origins of the national anthem including: Walter Lord, with an introduction by Scott S. Sheads, The Dawn’s Early Light, (Baltimore: Johns Hopkins University Press, 2012), and Steve Vogel, Through the Perilous Night: Six Weeks that Saved the Nation, (New York: Random House, 2013) as well as Sheads’ blogpost, https://maryland1812.wordpress.com/2011/04/03/the-defense-of-fort-mchenry-words-of-the-national-anthem/
 There are numerous transcribed versions of The Defence of Fort McHenry including https://maryland1812.wordpress.com/2011/04/03/the-defense-of-fort-mchenry-words-of-the-national-anthem/