All foreigners who are resident or own property in Spain (or for some other reason pay taxes in Spain) have to get a Spanish NIE number from the Spanish government. NIE stands for número de identidad de extranjero, which translates as foreigner identity number.
Thought it used to be possible to buy a property and pay the associated taxes on the basis of your passport number it is now almost impossible to do this. Therefore it is very likely that you will need to have requested and received an NIE number by the time you purchase a property in Spain.
Getting an NIE number can take anything from 2 to 6 weeks, depending upon where you apply and how quickly the Spanish authorities deal with your application. The point to take on board here is that you need to have looked into the question of NIE numbers and made suitable arrangements to get one in good time.
You can apply for an NIE number in person in Spain, or authorise someone to get it for you if you don’t have time, or can’t bear the thought of spending your time dealing with the Spanish bureaucracy.