Haarlem was granted city status or stadsrechten in 1245, although the first city walls were not built until 1270. The modern city encompasses the former municipality of Schoten as well as parts that previously belonged to Bloemendaal and Heemstede. Apart from the city, the municipality of Haarlem also includes the western part of the village of Spaarndam. Newer sections of Spaarndam lie within the neighbouring municipality of Haarlemmerliede en Spaarnwoude.
The city is located on the river Spaarne, giving it its nickname 'Spaarnestad' (Spaarne city). It is situated about 20 km (12 mi) west of Amsterdam and near the coastal dunes. Haarlem has been the historical centre of the tulip bulb-growing district for centuries and bears its other nickname 'Bloemenstad' (flower city) for this reason.
Haarlem has a rich history dating back to pre-medieval times, as it lies on a thin strip of land above sea level known as the strandwal (beach ridge), which connects Leiden to Alkmaar. The people on this narrow strip of land struggled against the waters of the North Sea from the west, and the waters of the IJ and the Haarlem Lake from the east. Haarlem became wealthy with toll revenues that it collected from ships and travellers moving on this busy North-South route. However, as shipping became increasingly important economically, the city of Amsterdam became the main Dutch city of North Holland during the Dutch Golden Age. The town of Halfweg became a suburb, and Haarlem became a quiet bedroom community, and for this reason Haarlem still has many of its central medieval buildings intact. Nowadays many of them are on the Dutch Heritage register known as Rijksmonuments. The list of Rijksmonuments in Haarlem gives an overview of these per neighbourhood, with the majority in the old city centre.
The oldest mentioning of Haarlem dates from the 10th century. The name probably comes from "Haarlo-heim". This name is composed of three elements: haar, lo and heim. There is not much dispute about the meaning of lo and heim; in Old Dutch toponyms lo always refers to 'forest' and heim (heem, em or um) to 'home' or 'house'. Haar, however, has several meanings, one of them corresponding with the location of Haarlem on a sand dune: 'elevated place'. The name Haarlem or Haarloheim would therefore mean 'home on a forested dune'.
There was a stream called "De Beek", dug from the peat grounds west of the river Spaarne as a drainage canal. Over the centuries the Beek was turned into an underground canal, as the city grew larger and the space was needed for construction. Over time it began to silt up and in the 19th century it was filled in. The location of the village was a good one: by the river Spaarne, and by a major road going south to north. By the 12th century it was a fortified town, and Haarlem became the residence of the Counts of Holland.