Spain's parliament has legalised euthanasia and assisted suicide for people with serious and incurable or debilitating diseases who want to end their life, making Spain the fourth country in the European Union to take the step.
The lower house of parliament's vote in a final reading was 202 in favour, 141 against and two abstentions.
Until now, helping someone end their life in Spain carried a jail term of up to 10 years.
"Today is an important day: we are heading towards the recognition of human rights. We are heading towards a more humane and fair society," Health Minister Carolina Darias told MPs.
The law, available for adults with legal residence in Spain, will take force in three months to give time to set up regional control committees which will review and authorise requests.
Outside the parliament, groups for and against the bill demonstrated during the debate and the vote.
The new legislation faces staunch opposition from the hard right and religious groups. The far-right Vox party has said it will challenge the law before the Constitutional Court.
The legislation follows growing public pressure generated by several high-profile cases, including that of Ramon Sampedro whose plight was immortalised in the Oscar-winning 2004 film 'The Sea Inside'.
Mr Sampedro was left tetraplegic after breaking his neck and fought an unsuccessful 30-year court battle to end his own life with dignity.
He died in 1998 with the help of his friend Ramona Maneiro.
Ms Maneiro hailed the legislation as a victory "for those who can benefit from it" and "for Ramon".
At the time, she was arrested but released due to lack of evidence, only admitting her role years later when the statute of limitations expired.
Almost 90% of Spaniards are in favour of decriminalising euthanasia, according to a 2019 opinion poll.
Rafael Botella, a 35-year-old Spaniard paralysed from the neck down since a car crash at 19, told Reuters before the vote that he was relieved that the law would give him the option in case he needed it.
"If for some reason someone is tired of living, no one has the power to tell him, 'No, you will live because my voters or my ideology tell the contrary'," Mr Botella said.
Belgium, Luxembourg and the Netherlands have already legalised euthanasia. A similar plan in neighbouring Portugal suffered a setback on Monday when the country's top court rejected as unconstitutional a bill approved by parliament.
The Spanish legislation will permit euthanasia in which medical staff intentionally end a life to relieve suffering, and assisted suicide in which it is the patient who carries out the procedure.
Various other countries permit the second option, as well as so-called "passive euthanasia" in which life-saving medical treatment is halted.
Backed by leftwing and centrist parties, the legislation will allow anyone with a "serious or incurable illness" or a condition which is "chronic or incapacitating" to request help dying, thereby avoiding "intolerable suffering".
But it imposes strict criteria: the patient - a Spanish national or a legal resident - must be "fully aware and conscious" when they make the request, which must be submitted twice in writing, 15 days apart.
The request can be rejected if it is believed the requirements have not been met; it must be approved by a second medic and by an evaluation body.
Any healthcare professional could withdraw on grounds of "conscience" from taking part in the procedure that would be available through Spain's national health service.
The move has been hailed by patients and right-to-die campaigners.
"It doesn't make any sense that people... would choose to live an undignified life," said Sofia Malagon, 60, who has Parkinson's and worries what will happen if she gets dementia.
"I don't want to be left like a vegetable," she said.
But the move has been roundly rejected by the Catholic Church and Spain's rightwing parties, with its promulgation also raising questions among some medical professionals.
Euthanasia "is always a form of murder since it involves one man causing the death of another," said the Episcopal Conference, which groups Spain's leading bishops and has accused the government of going from "defending life to being responsible for causing death".
"Doctors don't want anyone to die - it's in their DNA," said Manuela Garcia Romero, deputy head of the Medical College Organisation (OMC), expressing doubts over implementation of the law.
Euthanasia: Where it is legal in Europe
The Netherlands legalised active and direct euthanasia in 2002. Lethal doses of drugs are authorised if patients make the request while lucid.
They must also be experiencing unbearable suffering from a condition diagnosed as incurable by at least two doctors.
Last year the country's highest court ruled that doctors will be able to conduct assisted suicides on patients with severe dementia without fear of prosecution, even if the patient no longer expressed an explicit death wish.
The Netherlands also moved towards making euthanasia legal for terminally-ill children aged between one and 12.
Belgium lifted restrictions on euthanasia in 2002 for patients facing constant, unbearable and untreatable physical or psychological suffering.
They must be aged 18 or over and request termination of life in a voluntary, deliberated and repeated manner, free from coercion.
In 2014 Belgium became the first country to authorise children to request euthanasia if they suffer a terminal disease and understand the consequences of the act.
In Luxembourg a text legalising euthanasia in certain terminal cases was approved in 2009. It excludes minors.
Switzerland is one of the rare countries that allows assisted suicide with patients administering a lethal dose of medication themselves. It does not allow active, direct euthanasia by a third party but tolerates the provision of substances to relieve suffering, even if death is a possible side-effect.
Italy's Constitutional Court ruled in 2019 it was not always a crime to help someone in "intolerable suffering" commit suicide. Parliament is set to debate a change in the law banning the practice.
The halting of medical procedures that maintain life, called passive euthanasia, is also tolerated.
In France a 2005 law legalised passive euthanasia as a "right to die". A 2016 law allows doctors to couple this with "deep and continuous sedation" for terminally ill patients, while keeping euthanasia and assisted suicide illegal.
Sweden authorised passive euthanasia in 2010 and Ireland also recognises the "right to die".
Britain has allowed medical personnel to halt life-preserving treatment in certain cases since 2002. Prosecution of those who have helped a close relative die, after clearly expressing the desire to end their lives, has receded since 2010.
In Austria and Germany passive euthanasia is permitted if requested by the patient.
Austria's constitutional court ruled in October the country was violating fundamental rights in ruling assisted suicide illegal and ordered the government to lift the ban in 2021.
Denmark has allowed people to file written refusal of excessive treatment in dire situations since 1992, with the document held in a centralised register.
In Norway passive euthanasia is permitted if requested by the patient or by a relative, if the patient is unconscious.
In Hungary people with incurable diseases can refuse treatment.
It is also legal to end treatment of terminally ill people in Lithuania and Latvia.