NEW YORK — Not long after his 21st birthday, Christian Rodriguez got the contract of a lifetime for his new info-tech company: The Colombian was hired as a cybersecurity consultant by Joaquín Guzmán Loera, the Mexican drug lord known as El Chapo.

While Rodriguez had little experience or formal education, he had been recommended by one of his other clients: Jorge Cifuentes Villa, a veteran trafficker who worked with Guzmán making cocaine deals with left-wing guerrillas in Colombia.

And so in 2008, the ambitious, young techie visited Guzmán at one of his hideouts deep in the Sierra Madre, inspecting the kingpin’s communications system and his shoddy internet setup, which often broke down when it rained. In several follow-up meetings, Rodriguez testified this week, he pitched Guzmán on an elaborate plan to enhance his information security, offering to build him a private phone network that ran on the internet and was totally encrypted.

That sophisticated system was, within three years, used against Guzmán after Rodriguez became ensnared in an FBI sting operation and was then persuaded to become an informant. The IT expert helped the U.S. authorities secretly collect a vast trove of the kingpin’s phone calls and text messages — among them, dozens he had sent to his wife and mistresses. In two days of testimony that ended Thursday, Rodriguez told this riveting story to great — and damaging — effect at Guzmán’s drug conspiracy trial in U.S. District Court in Brooklyn.

Rodriguez’s account was a kind of 21st-century cautionary tale. The moral? Always treat your IT people well. It was also a high-tech spy thriller in which federal agents were able to leverage Guzmán’s obsession with espionage against him.

It all began when Rodriguez, now 32, took $100,000 to build the kingpin the encrypted network, which allowed as many as 100 members of the Sinaloa drug cartel to speak securely with one another merely by dialing three-digit extensions on their phones. Part of his contract, Rodriguez said, was to teach the kingpin’s team how to use the phones, recalling that he once gave a tutorial on the devices to Guzmán’s personal secretary in an armored car.

But Guzmán — who, according to witnesses, long had a penchant for snooping — wanted something more. The IT expert said the crime lord also asked him to install spyware called FlexiSPY on the “special phones” he had given to his wife, Emma Coronel Aispuro, as well as to two of his lovers, including one who was a former Mexican lawmaker.

The kingpin made other strange requests, Rodriguez told jurors Thursday. Once, he said, Guzmán asked him to find a way to intercept every message being sent from every internet cafe in Culiacán, a city of about 800,000 residents in the crime lord’s home state of Sinaloa. (Rodriguez said he tried, but ultimately failed.)

Working for a narco-lord could also be dangerous, he said.

Not long after the assignment to tap the internet cafes, the Mexican military raided Guzmán’s secret mountain hideout. Rodriguez said he was forced to wander for three days in the elevated terrain with the kingpin and a band of heavily armed bodyguards.

The gunmen, Rodriguez recalled, carried both “large weapons” and one “very large weapon,” which, they told him, was capable of shooting down a helicopter.

Throughout the grueling journey, Guzmán was “always very calm, very sure, very tranquil,” Rodriguez said.

When a prosecutor asked the young techie how he felt being on the lam, he answered, “very badly.”

After that experience, Rodriguez said he decided to put some “distance” between himself and Guzmán’s organization, training other technicians to run the cartel’s day-to-day communications. Shortly after, he recounted, the FBI launched a covert operation to secure his cooperation.

In February 2010, an FBI agent testified Tuesday, an undercover officer posing as a Russian mobster met Rodriguez in a Manhattan hotel. The officer said he wanted the IT expert to devise a way for him to speak with his associates without law enforcement listening in.

The next year, Rodriguez said Thursday, two other federal agents approached him in Bogotá, Colombia, saying they knew he worked for Guzmán and telling him he was “in serious trouble.”

That same day, Rodriguez said, he agreed to become a government informant. Over the next several months, he said he installed recording software in Guzmán’s network that automatically sent copies of the kingpin’s calls to the FBI each day at midnight. Rodriguez also gave the bureau the usernames and passwords of Guzmán’s FlexiSPY accounts, allowing agents to read — almost in real time — the intimate and incriminating texts he sent his romantic partners.

All this came crashing down in 2012 when Rodriguez intercepted a phone call between two of Cifuentes’ siblings in which he heard them saying they had figured out that El Chapo’s tech guy was working with the Americans. After fleeing to the United States, Rodriguez said he had a “nervous breakdown” within a year. He was hospitalized and treated with electroconvulsive therapy.

This week, he looked better, if not completely fine, telling jurors he was now on medication and seeing a psychiatrist.

As he finished his testimony, Rodriguez left the witness stand and walked out the courtroom door, nervously avoiding Guzmán’s gaze.

This article originally appeared in The New York Times.