Problem During the 1980s, Spain had very strict laws limiting access to opioid agonist maintenance treatment (OAMT). Because of this, mortality among people who used illicit opioids and other illicit drugs was high. Spain was also the European country with the highest number of cases of acquired immunodeficiency syndrome transmitted through illicit drug injection. Approach The rapid spread of human immunodeficiency virus (HIV) infection among people using heroin led to a shift from a drug-free approach to the treatment of opioid dependence to one focused on harm reduction. A substantial change in legislation made it possible to meet public health needs and offer OAMT as part of harm reduction programmes in the public health system, including prisons. Local setting Legislative changes were made throughout the country, although at a different pace in different regions. Relevant changes Legal changes facilitated the expansion of OAMT, which has achieved a coverage of 60%. A parallel reduction in the annual incidence of HIV infection has been reported. Reductions in morbidity and mortality and improved health-related quality of life have been described in patients undergoing OAMT. Lessons learnt The treatment of opioid dependence has been more heavily influenced by moral concepts and prejudices that hinder legislation and interfere with the implementation of OAMT than by scientific evidence. To fulfil public health needs, OAMT should be integrated in harm reduction programmes offered primarily in public facilities, including prisons. Longitudinal studies are needed to detect unmet needs and evaluate programme impact and suitability.