The War of the Keys (1228–1230) was the first military conflict between Frederick II, Holy Roman Emperor, and the Papacy. Fighting took place in central and southern Italy. The Papacy made strong gains at first, securing the Papal States and invading the Kingdom of Sicily, while Frederick was away on the Sixth Crusade. Upon his return, he defeated the papal forces, forcing Pope Gregory IX to begin peace talks. After drawn-out negotiations, the treaty of San Germano terminated the conflict with no territorial changes.

The causes of the conflict lay in conflicting papal and imperial claims in central Italy, Frederick's failure to fulfill his agreement to lead a crusade on schedule and his supposed mistreatment of the Sicilian church. The emperor was excommunicated before he left on his crusade in June 1228. His representatives, with or without his permission, entered territory claimed by the Papacy and Gregory responded with war. His aim was to take Sicily, which was a fief of the church, back from Frederick.

Gregory raised troops and funds internationally, from as far afield as Portugal and Sweden. He sent two armies under the command of John of Brienne into the disputed territory in late 1228 and another army into the Kingdom of Sicily in January 1229. The war was going in his favour as late as May, but he was running low on funds and troops. The strategy of denying the ports to Frederick failed. The return of the emperor in June quashed the rumours spread by papal agents that he was dead and caused a rapid reversal of fortunes. The sieges of Sulmona and Capua were lifted by September and by October most of the kingdom had been recovered. Negotiations were opened in November and the active phase of the war ended.

Principal sources The principal sources on the war are in Latin. The Chronica of Richard of San Germano, which has a pro-imperial stance, is one of the most important narrative sources. The biography of Gregory IX in the Liber censuum provides a valuable pro-papal narrative. Other important chronicles are Roger of Wendover's Chronica sive flores historiarum and the Chronicon of Aubry of Troisfontaines. From a crusader perspective the war is covered in the Colbert–Fontainebleau Eracles and the Chronicle of Ernoul and Bernard the Treasurer.

There are three north Italian chronicles that cover the war from the contrasting perspectives of the pro-papal Guelphs and pro-imperial Ghibellines. From the Guelph perspective are the contemporary Annales placentini guelfi of Giovanni Codagnello and the Chronicon faventinum, while the Ghibelline perspective is represented in the slightly later Annales placentini gibellini., calls the Annales placentini guelfi "one of the more informative sources on the War of the Keys", but , considers that they have relatively little to say about it compared to the later crusades against Frederick. Even the Guelph chronicles, however, do not exactly toe the papal line. They generally view the war as the fault of Frederick for refusing to respect papal territory, but do not blame the emperor for delaying his crusade.

The most important documentary source is the register of Gregory's letter's, which are edited in the Monumenta Germaniae Historica. Letters to and from Frederick also survive, including two of his that were copied into an Arabic chronicle, the Taʾrīkh al-Manṣūrī of Ibn Naẓīf al-Ḥamawī., includes a translation of a part of the Arabic source. A full translation of both letters is in . For the peace negotiations, the letters to and from Cardinal Thomas of Capua are a critical source.

An important source for contemporary critical attitudes towards the pope's war is the work of the troubadours, lyric poets writing in Old Occitan.: "certain troubadours ... are a major source for criticism of ... the papal campaigns against the Staufen".

Background and causes The main points of contention between Frederick and the Papacy were the crusade and the Papal States. Frederick had vowed several times in public ceremonies to lead a crusade to the Holy Land on pain of excommunication. Yet Pope Honorius III had granted him many delays. Frederick finally sailed from Apulia in August 1227, but almost immediately turned back, claiming illness. Gregory IX was not prepared to accept another delay. He accused Frederick of faking his illness (although the Guelph annals accept the excuse). On 10 October 1227, Gregory excommunicated Frederick. The Annales placentini guelfi identifies the harm Frederick's delay caused the crusaders who had gathered in Apulia as one of the causes of the war.

In March 1228, Gregory renewed the excommunication and ordered Frederick to stop attending religious services. Otherwise, Gregory would absolve his subjects of their oaths of fealty to him and confiscate the Kingdom of Sicily, which was a papal fief since 1059. The popes also claimed that the march of Ancona and duchy of Spoleto belonged to them, but Frederick refused to relinquish imperial control. He appointed Rainald of Urslingen as duke of Spoleto.

In November 1225, Frederick II had married the thirteen-year-old queen Isabella II of Jerusalem, thus depriving her father, John of Brienne, of the regency. Although John refused an offer to lead the Lombard League in its rebellion against Frederick while Isabella was alive, the queen died in May 1228. Still excommunicated, Frederick II left on the Sixth Crusade at the end of June. Gregory did not recognize the expedition as a crusade. With no reason any longer to keep on good terms with Frederick, John accepted command of the papal army preparing to invade Sicily. The absence of the emperor on crusade provided the perfect opportunity and, in August, Gregory released both Frederick's imperial and Sicilian subjects from their oaths of obedience. Gregory intended to replace Frederick as emperor. Although Frederick believed that Gregory also wanted to replace him on the Sicilian throne with one of his pliable young sons, the pope intended to dissolve the kingdom and rule it directly through papal governors.

The war against Frederick began while he was still in the east. In March 1229, he signed a treaty with the Ayyubid sultan al-Kāmil by which he took control of Jerusalem and brought his crusade to a successful conclusion. In July, Gregory published an encyclical denouncing the treaty and Frederick as un-Christian.

Papal war planning Gregory's call for support, both in men and money, was international. He issued a bulletin to his legates and commanders on 1 December 1228 detailing his plans for raising troops and funds.

Forces Sources generally refer to the pope's forces as the "papal army" or "army of the lord pope". Gregory himself called it the "army of the church" () and in one instance the "army of Christ". Richard of San Germano describes the papal army as "enemies bearing the sign of the keys" ()Translation from . "Keysader host" is the translation of . and ('key-signed'), a play on (cross-signed), which was the common term for crusaders. These are references to the insignia of the keys that appeared on the papal banners. The keys were also apparently sewn onto clothing over the breast in imitation of the cross borne by crusaders. The conventional name "War of the Keys" is a reference to this insignia.The term is used by ; ; ; ; and . It has been favoured by scholars "because it embodies the quasi-crusade status of the campaign".

The papal army contained both knights and infantry. It was mostly raised in the Papal States and in Tuscany. These, however, could not supply a large enough army for Gregory's purposes, and so he sought to raise troops abroad. He sent requests throughout the Holy Roman Empire: to the German princes, to the Lombard League and to the Republic of Genoa. He also came to employ many mercenaries from France, England and Spain. Bishops Milo of Beauvais and Hugh of Clermont brought troops from France. That a few Germans heeded the pope's call is known from the fact that Frederick later pardoned them. Gregory was still trying to raise troops as late as June 1229, when he asked the Peter of Portugal to bring knights to Italy to fight Frederick. The army of 1228 was deployed in three units against Ancona, Spoleto and the Campania. On 21 December 1228, when Gregory wrote to King Eric XI of Sweden requesting financial aid, he confirmed that his forces were already divided into three armies.

The papal commanders were John of Brienne, Cardinal Giovanni Colonna, Cardinal Pelagius of Albano and the papal chaplain Pandulf of Anagni, acting as papal legate., and , list all four together, but , implies that Pelagius only joined the army later. John is sometimes seen as the overall commander of the papal forces, but this is not clear from the sources., seems to be of this opinion, but see . He was joined by his nephew, Walter IV of Brienne, who claimed the principality of Taranto and the county of Lecce in the Kingdom of Sicily. His northern army facing Ancona had the more urgent task, but the southern army facing Sicily under Pandulf seems to have been the main force., and , have this army under Pandulf, but , puts it under Colonna. The southern army was drawn mainly from the Papal States. It also contained many Sicilian exiles and was even captained by two, counts Thomas of Molise and Roger of Aquila. These two armies are easily identified through the sources. They are the second and third, but of the first army mentioned by the pope no details or names of commanders have come down., writes that "concerning the first [army] there are no details; a second under the command of John de Brienne and John of Colonna ... hastened into the March of Ancona ... the third division was entrusted to ... Pandulfus of Anagni, who led his men southward." , refers only to "two papal armies as well as mercenaries".

Finances The bulletin of 1 December 1228 contains the earliest reference to papal fundraising for a war against Christians. Since Gregory did not initially call a crusade against Frederick, he could not offer indulgences to those who took part in the war or gave money. The use of direct taxation to fund a papal army was a "first of its kind". He levied tithes, the so-called "crusade tenth", from France, England, Wales, Scotland, Ireland, Denmark, Sweden, northern Italy and eastern Europe.: England, France and Scandinavia; : England and Wales; : France, Sweden, Denmark and England; : Sweden, eastern Europe, northern Italy, France and England; : Sweden, Denmark, England and northern Italy; : England, Scotland and Ireland. In France, the remainder of a five-year levy first collected in 1225 for the Albigensian Crusade of 1226 was set aside for the new war after the crusade ended with the treaty of Paris in April 1229. In total, about 100,000 were received from France. The tithe was successfully collected in northern Italy and eastern Europe, possibly in Sweden, but not in England or Scotland., denies that the tithe was successful in Sweden, but , claims that it was. For Scotland, see .