Francis Scott Key wrote the poem in 1814; it was set to the tune "To Anacreon in Heaven." It became the official national anthem on March 3, 1931—117 years later—via congressional resolution signed by President Hoover.
Before 1931, "Hail, Columbia" and "My Country, 'Tis of Thee" served as de facto anthems.