in 1346 the old Prussian Warmian forest in the vicinity was cleared and a place was selected on the Alle now Łyna River for a new settlement.
The Teutonic Knights began construction of Ordensburg castle in 1347 to protect against the Old Prussians, and the settlement of Allenstein was first mentioned the following year. The German name Allenstein meant a castle on the Alle River. It became known to Polish settlers as Olsztyn.
The settlement received municipal rights from Johannes von Leysen on 31 October 1353, and the castle was completed in 1397. Allenstein was incorporated into the Kingdom of Poland during the Polish-Lithuanian-Teutonic War in 1410 and in 1414 during the Hunger War, but was returned to the monastic state of the Teutonic Knights after hostilities ended.
Allenstein joined the Prussian Confederation in 1440. It rebelled against the Teutonic Knights in 1454 upon the outbreak of the Thirteen Years' War and requested protection from the Polish Crown.
Although the Teutonic Knights captured the town in the next year, it was retaken by Polish troops in 1463. The Second Peace of Thorn (1466) allocated Allenstein and the Bishopric of Warmia as part of Royal Prussia under the sovereignty of the Crown of Poland.
From 1516-21, Nicolaus Copernicus lived at the castle as administrator of Allenstein and Mehlsack (Pieniężno); he was in charge of the defenses of Allenstein and Warmia during the Polish-Teutonic War of 1519-21.
Allenstein was sacked by Swedish troops in 1655 and 1708 during the Polish-Swedish wars, and the town was nearly wiped out in 1710 by epidemics of bubonic plague and cholera.