With sleeves rolled up, Canadian Prime Minister Justin Trudeau walked into a San Francisco startup Thursday — followed by bodyguards and a flurry of media representatives — to join a brief strategy session with the company’s engineering team.

The prime minister’s visit to AppDirect, a cloud subscriptions company with a significant Canadian presence, was part of a two-day trip to San Francisco that had a clear underlying motive: persuading more American companies to come to Canada.

“The world is going through a transformation right now, and we need to make sure we are a part of that,” Trudeau said later at a news conference, for which he rolled down his sleeves and donned a blazer. He bragged about the Canadian workforce several times, using adjectives like “educated,” “diverse” and “excited.”

It was the first visit of a Canadian prime minister to the city since Mackenzie King led a delegation to the United Nations Conference in 1945, according to the U.S. State Department. And Trudeau will not leave empty-handed: Salesforce CEO Marc Benioff, who met with Trudeau earlier in the day, pledged to invest $2 billion in Canada over the next five years. AppDirect, which already has offices in Montreal and Calgary, announced a commitment to add 300 Canadian jobs over the next few years.

Trudeau, who also had a meeting with Amazon CEO Jeff Bezos on his itinerary, said he planned to tout his country as the perfect location for the Seattle company’s second headquarters. Toronto is the only Canadian city in the running for Amazon’s multimillion-dollar project, which promises tens of thousands of high-paying jobs.

“I’m enthused about the interest that these global companies are taking in Canada,” Trudeau said. “It is a benefit for those companies that invest in Canada, because they get such a high caliber of talent.”

Trudeau’s U.S. trip comes as the Trump administration has increased its scrutiny of H-1B visas, which allow foreigners with specialized skills to live and work in the U.S. for a period of time. As the uncertainty around the foreign work program intensifies — and the wait for permanent residency lengthens for people from some countries — foreigners are viewing Canada as a refuge from the red tape of the U.S. immigration system.

To capture this talent, the Canadian government created a two-week fast-track work permit called the “global skills strategy visa.” But immigrating to Canada is not as easy as buying a plane ticket and filing some paperwork: Foreigners still need to meet certain requirements and have a job offer.

But as more companies move up north, so do the job opportunities.

Several major Bay Area companies, including Uber, Lyft, Slack and Apple, have expanded in Canada with new offices or acquisitions over the past year. Startups are also eyeing Canada as a way to get around strict U.S. visa laws and the Bay Area’s expensive housing and competition.

One Bay Area company, Terminal, helps companies quickly expand their engineering teams by connecting them with talent in Canada.

Canada has a “good proximity in terms of time zone and distance to travel to the U.S.,” said Terminal co-founder Dylan Serota, who will meet Trudeau Friday with other Bay Area business leaders who are doing work in Canada. “The depth of the talent is what it comes down to.”

Also hanging over Trudeau’s visit is the renegotiation of the North American Free Trade Agreement, a pact that President Trump called a job-killing “disaster” on the campaign trail. Trump’s threats to withdraw from the treaty could affect many Canadians working in the Bay Area tech sector.

Trudeau emphasized that he had promised to come up with a compromise that is a “win, win win,” for the three countries in the treaty — the U.S., Canada and Mexico.

“We know that bringing in great talent from around the world is a tremendous benefit, not just for the companies that want to do that, but for Canadian jobs and to our country as a whole,” he said.

Trisha Thadani is a San Francisco Chronicle staff writer. Email: tthadani@sfchronicle.com Twitter: @TrishaThadani