When Brian Cashman pulled the trigger on the trade that sent Aroldis Chapman to the Cubs nearly two years ago, he quickly texted Donny Rowland, the Yankees’ head of international scouting.

“ ‘We finally got him,’ ’’ the general manager told Rowland. “ ‘We finally got Gleyber Torres.’ ”

It was a pursuit years in the making. Rowland and the Yankees pegged Torres — a 16-year-old out of Caracas, Venezuela — as the top talent in the 2013 international free-agent class.

“He had everything you look for,’’ Rowland said. “Wherever you saw him play, he stood out. He oozed ‘major leaguer.’ ”

Despite the Yankees’ interest in the shortstop, they lost contact with him in the prelude to the signing period, which occasionally happens when multiple teams are chasing the same player.

For the Yankees and Torres, it happened in late 2012.

“It was like he had fallen off the face of the earth,’’ Rowland said. “I was walking the streets of Venezuela but he was gone. I tried for two days, but there was no chance of getting to him.’’

Ultimately, Torres inked a $1.7 million deal with the Cubs on the July 2 signing day and developed into one of baseball’s top prospects.

“It was frustrating,” Rowland said. “But these things are dogfights, and in this case, we got our tail bit off.’’

The Yankees’ history with Torres, though, helped as the 2016 non-waiver trade deadline approached and Cashman looked to remake the team’s roster, as well as its minor league system.

In a move uncharacteristic for the Yankees, Cashman traded Chapman to Chicago, Andrew Miller to Cleveland and Carlos Beltran to Texas in exchange for minor leaguers — including Justus Sheffield, Clint Frazier and Torres.

“Even though we didn’t sign him, we watched him when he was with the Cubs and obviously liked him,” Cashman said of Torres. “So I relied on Donny to let me know about his makeup and whatever other information we could get that maybe other teams didn’t know because they didn’t go as far back with him.”

Now, though Torres is on the disabled list with a sprained right hip, he’s already made his first All-Star team and helped the Yankees to the second-best record in the majors.

Torres is expected back after the break following a rehab assignment. When he returns, the 21-year-old second baseman will have 15 homers and an OPS of .905 — both tops among American League rookies.

Due in part to the lofty numbers, Torres is the front-runner for the AL Rookie of the Year award, especially with the Angels’ Shohei Ohtani’s elbow injury keeping the two-way star from pitching.

It’s an evolution several in the organization saw coming.

“The first day he showed up with us, you could envision what type of player we’re seeing now,’’ Single-A Tampa manager Pat Osborn said. “He was a natural.

“We were in Brevard County [facing the Brewers’ Single-A team] and there were about 15 people in the stands,’’ Osborn said. “Gleyber played the same way he does on the biggest stage in New York. Nothing got to him. You could tell he was at a different level.”

Osborn, who is back with Tampa after spending last season managing Single-A Charleston, said Torres was “by far the best 19-year-old I’ve ever seen.”

“Just watching him work before games, you could tell he was gonna be special,’’ Osborn said. “His tools were off the charts and he was mature beyond his years. Most guys in the Florida State League were three or four years older than him. So the fact that he’s an All-Star at 21 is great, but it’s not totally unexpected. You could see it coming.’’

He played for Osborn at Tampa for a little over a month that season, then went to the instructional league before being named MVP of the Arizona Fall League.

At every stop that season was Carlos Mendoza, now the Yankees’ infield and quality control coach.

“His ability to make adjustments has always stood out,’’ Mendoza said. “And the better the competition, the better he played. He was the talk of the Arizona Fall League. Guys don’t win the MVP of that league at 19. He was the talk of the league. He looked like he belonged. It didn’t matter who was on the mound.’’

Gordon Blakeley saw the development coming as an international scout with the Yankees.

“I had a 65 [rating] on him,’’ said Blakeley, who also helped the Yankees sign Robinson Cano and Gary Sanchez, among others. “To put that in perspective, I had a 70 on [Alex Rodriguez] and a 62 on Sanchez. Torres had no holes. We thought he was the best international player that year.”

So did other teams.

“You can’t get every guy you want,’’ Cashman said of how Torres slipped away.

Still, Torres’ emergence as one of the best young players wasn’t without some difficulty.

After an impressive 32 games with Double-A Trenton last season, Torres was promptly promoted to Triple-A Scranton/Wilkes-Barre, where he also excelled.

He was on the fast track to a 2017 call-up to the Bronx before he tore the UCL in his left elbow on an awkward slide into home while with SWB, resulting in season-ending surgery.

A slow start to spring training got Torres a ticket back to minor league camp in March, but Mendoza insisted he wasn’t concerned the injury would have a lasting impact.

“He didn’t get the results, but I was confident that was just from time off,’’ Mendoza said. “He wasn’t favoring the elbow. And in his first game of the spring, I saw him dive for a ground ball and he didn’t hesitate. That was a good indication to me that he was over the injury.”

Another stellar few weeks with SWB in 2018 resulted in Torres making his MLB debut on April 22 at Yankee Stadium.

He went hitless in that game, but the Yankees beat the Blue Jays and won their first eight games with Torres in the lineup. They went on to win 16 of Torres’ first 17 starts, as they shook off a 10-9 beginning to the season, largely because of Torres’ play.

“He’s like a [Miguel] Cabrera in the middle of the diamond — and he should just be coming out of college,’’ Blakeley said. “Instead, he’s an All-Star. He was the best hitter of his age every time you saw him.”

Not much has changed.

Asked what’s stood out the most about Torres, manager Aaron Boone points to a defensive play he made earlier in the year.

“It was a 3-1 or 3-2 count, the base runner was going from first late in the game and Gleyber was covering second,’’ the manager said. “There was a grounder to second and he was able to shut it down and almost instinctually make the play. He made it look easy and routine, and it wasn’t at all. It was like he could almost sense and read it ahead of time that it was coming in his direction. And he was there.”

Torres’ impact hasn’t stopped there.

After the disappointment of losing out on Torres in 2013, the Yankees spent a whopping $17 million in the next international signing period — over $10 million more than any other team.

Cashman insisted it was not a result of Torres landing elsewhere, but instead was based on what he called “an exceptional class.”

In 2014, the Yankees ended up with a group that included Dermis Garcia, Wilkerman Garcia, as well as the team’s top prospect, outfielder Estevan Florial, who just returned from a wrist injury.

And the man behind the signings, Rowland, acknowledged that not getting Torres initially played somewhat of a role in the way they have scouted the Latin American market since.

“What it did was collectively open minds to react to it and move earlier,’’ Rowland said. “We learned our lesson and changed the process. It did change the strategic and execution of the timing of it.’’

Whether it results in another player like Torres remains to be seen.

“That’s the guy I saw,” Rowland said. “What he’s doing now is not a surprise. … I’m thrilled he’s doing it for us.’’

— Additional reporting by George A. King III