Sometimes known by its first line, My Country, 'tis of Thee, this song was written in 1832 by the Reverend Samuel F. Smith. Some references give 1831 as the date, but by Samuel Smith's own testimony, they are not correct.
Many original "autograph" copies of Smith's words still exist, and as he wrote out each copy he was very careful to preserve the original wording and punctuation - unlike Key, whose various copies of The Star Spangled Banner varied in words and punctuation.
The origin of the title, America, is unclear. The early handwritten copies do not bear the name, but in a copy written in 1890 by Smith, he has included the title (and has transposed two lines in the third verse). A much earlier autograph copy included a fifth verse, but Smith himself had crossed it out.
It all started when Smith is supposed to have received a collection of German music books from a friend, Lowell Mason, who in turn had received them from William Woodbridge in 1831 after Woodbridge had returned form a visit to Europe.
By his own account (in February, 1832), Smith was reading through the music and was particularly attracted to the tune of God Save the King, although he was unable to understand the German words. He had realised that they were patriotic, and, in his own words, "in a brief period of time at the close of a dismal winter afternoon" he penned the verses as they now appear.
Its first "performance" in public was at a children's Sunday school celebration of American independence at the Park Street Church, in Boston, on July 4th, 1832. There is a minor uncertainty here, as a writer in the Boston Evening Transcript claims to have sung it earlier than this in the Bowdoin Street Church, but Smith's own personal recollection continued to be that it was at the Park Street Church, and there is no reason to doubt his account.
The tune itself was controversial. It was considered to be "un-American" in later years (somewhat ironic, considering the origins of the tune of the national anthem itself!), yet the tune had actually appeared in America before 1776. It was used for a number of patriotic songs, including God Save the President, and God Save George Washington.
The Pennsylvania Packet printed the words God Save the Thirteen States, to be sung to the same tune, in the 1770s. Another was written to be sung at the opening of the Charles River Bridge at Bunker Hill in the 1780s (it started with the words Now let rich music sound), and in 1798 it appeared again as an Ode to the Fourth of July, with the words, Come All ye Sons of Song.
In the Philadelphia Minerva, in October, 1795, a suffragette wrote a poem called Rights of Woman, intended to be sung to the same tune (then called God Save America), and beginning:
save each Female's right
The only real curiosity is why, with so many uses of the tune (arising from its use by the British), Smith only discovered it when reading the German music books.