This information is from the Agencia Tributaria, the equivalent of HMR&C in the UK:
Every person in Spain is assigned a Fiscal Identification Number which is used for all tax returns and correspondence with the Tax Administration. In the case of Spanish nationals, this number is the same as the National Identity Number, “DNI”, while in the case of foreign individuals, the number is the Foreign Individual Identity Number, “NIE”, which is obtained at the General Directorate of Police. However, nationals belonging to member states of the European Union and other states subscribing to the Agreement for European Economic Space, are not obliged to request an identity number as a foreigner and may ask to be assigned a Fiscal Identification Number from the Spanish Tax Authorities.
Spanish Independent Agent
Based in the UK