A young Canadian convicted of plotting terrorist attacks in New York City was sentenced by an American court on Wednesday to 40 years in prison, his Canadian lawyer said.

The sentence handed down to Abdulrahman El Bahnasawy, 20, came despite defence pleas that the accused was severely mentally ill and a drug addict, and the victim of entrapment.

"He got 40 years," lawyer Dennis Edney told The Canadian Press in an interview. "Even although we had spent a great deal of time establishing his mental illnesses, his bipolar history, that he suffers psychotic episodes, that he's addicted to drugs since the age of 14 and failed numerous rehab clinics."

Edney said U.S. District Judge Richard Berman recognized the accused's mental illness and acknowledged El Bahnasawy had tried to commit suicide in prison several times.

"He felt clearly that 40 years was doing him a favour because the FBI wanted life and he mentioned that a number of times."

Documents filed with the court in the Southern District of New York show El Bahnasawy was just 17 and living at home in Mississauga, Ont, when he met an undercover FBI agent online.

The defence argued that the agent encouraged him to plan attacks in the Big Apple. Prosecutors maintained the plot was well underway before the two connected.

The plans, which involved conspirators arrested in Pakistan and the Philippines, called for attacks on the New York subway and Times Square.

The FBI, with RCMP help, arrested the then-18-year-old El Bahnasawy at a hotel on the outskirts of New York in May 2016. Investigators said he had bought bomb-making materials and helped secure a cabin within driving distance of the city for the purpose of building explosive devices.

El Bahnasawy later pleaded guilty to terrorism charges in 2016.

In the letter filed with the court in March, El Bahnasawy explained that American airstrikes against the Middle East had motivated him. Americans were trying to disrupt the lives of people in the Middle East with airstrikes and "it was appropriate to use similar methods back until and unless they stop," he said.

"There are many issues in this world but I don't want to lose my life or freedom to try fixing them, and I definitely do not want to resort to violence or harm to fix them," El Bahnasawy wrote. "I sincerely apologize for my (behaviour) and I only ask for a second chance."

Edney said it was clear over the days of sentencing that the FBI were aware of El Bahnasawy's mental-health problems from the RCMP, who had obtained his medical records. He also said an entrapment defence had failed in every terrorism trial in the United States over the past dozen years despite "clear government over-reach highlights."

"The undercover agent, who had dealt with this damaged boy, had his medical records from the RCMP," Edney said.

In a recent pre-sentencing submission, his parents Khdiga Metwally and Osama Elbahnasawy pleaded for understanding, saying their son had struggled with mental illness and addiction for years.

"Abdulrahman is an exceptionally bright boy suffering from bipolar, a condition he was ascribed at birth and which he has no choice over," they wrote the judge. "We can either be the hand that strikes him down even further and punishes him for something which he cannot control, or the hand that leads him out of the darkness."

The hope now is to have El Bahnasawy transferred to Canada under an international treaty to serve out his sentence.