A fresh-faced rookie cop with ties to New York and the Middle East was the undercover who eventually brought down a pair of would-be jihadists gunning for Americans.

The NYPD officer was only a couple of years out of the Police Academy when he was assigned the treacherous duty of hanging out with suspects Mohamed Alessa, 20, and Carlos Eduardo Almonte, 24.

Operating out of a Jersey City apartment, the cop — who is of Egyptian descent — grew a thick beard and adopted a convincing enough demeanor to fool the two wannabe terrorists into providing him with all the details of their gruesome plot to train overseas and kill countrymen.

Alessa and Almonte occasionally used the undercover cop’s local gym to pump iron, beefing themselves up for their planned future, military warfare.

They would sometimes practice tactics in the cop’s own backyard — until they realized the risk.

“On March 28, 2010, at the [undercover’s] residence in Jersey City, Almonte stated that it would look strange for three bearded males to do training outdoors,” the criminal complaint states. “Almonte further stated that the criminal defendants who were convicted of plotting an attack on Fort Dix in New Jersey were caught after they had trained outdoors.”

The suspects shared recorded rantings of radical cleric Anwar al-Awlaki with the cop and routinely showed off goodies from their shopping sprees: typically military or camouflage clothing.

The cop once went with them to help pick out gear at a military surplus store in New York.

But the ultimate show of trust may have come when the defendants allegedly gave him thousands of dollars to deposit so they could access it when they traveled overseas.

The NYPD newbie was first put to the test in 2009, after officials were tipped off that Alessa and Almonte had the US in their crosshairs.

Unlike some undercover agents who have offered weapons to various suspects in other terrorist operations, the plant who duped Almonte and Alessa was more passive, listening at key moments and letting his subjects take the lead.

The incriminating gems he collected for authorities included Alessa saying to him and Almonte: “A lot of people need to get killed, bro, swear to God. My soul cannot rest until I shed blood.”

Sources said the Police Department employed similar undercover methods in the 1960s to infiltrate groups such as the Black Panther Party.