From 1707 to 1708, Bach was appointed as organist in the church of Holy Blasius (Divi Blasii) in Mühlhausen. One of the obligations of this function was to write music for the yearly recurring installation of the new town council (something he would do repeatedly later on in Leipzig with his Ratwechsel cantatas).
The cantata Gott ist mein König, BWV 71, was performed in the larger Marienkirche in Mühlhausen on February 4th, 1708, under the direction of the 22 year old Bach. The cantata pleased the town council so much, that they had the score printed, and as such it was the only cantata ever published during Bach''s lifetime. It is one of Bach''s earliest cantatas.
In 1709 Bach wrote another Ratwechsel cantata for the Mühlhausen town council, although he had already left for Weimar. Unfortunately this cantata is completely lost.