On a late October day, Amazon executives flew to New York to answer a final question before they committed to opening a massive technology center in Queens: Could Gov. Andrew M. Cuomo and Mayor Bill de Blasio stop bickering long enough to see the project through?

“They wanted to just trust-but-verify that everybody was on the same page,” said Alicia Glen, a deputy mayor who was at the meeting with Mr. de Blasio.

After meeting with both men separately that day, the Amazon officials decided the two Democrats could put aside their longstanding differences. Soon after, documents were exchanged, and re-exchanged, ironing out details of a package worth more than a billion dollars in tax incentives and state grants. The politicians even agreed to a plan to circumvent the City Council to prevent future roadblocks.

The deal for the Queens development, in Long Island City, was announced on Tuesday, after Amazon concluded a 14-month, countrywide search for a location for a second headquarters for some 50,000 well-paid tech workers. The company, which ended up picking two sites and dividing the new workers between them, is also opening a huge corporate site in Arlington, Va., in an area across the Potomac River from Washington. Amazon said the new developments, both to be called headquarters, would require $5 billion in construction and other investments.

The company also said it would develop a much smaller operations and logistics facility in Nashville, creating 5,000 jobs.

The announcement capped a frenzied competition among nearly 240 communities across the country. Many politicians saw it as an opportunity to remake their city or a neighborhood for the tech era. Some economists and policymakers warned against using public money to help one of the most valuable companies in the world, and of the potential for escalating housing costs and traffic.

Amazon said that the size and quality of the winners’ labor pools decided the competition. Amazon was also offered more than $2 billion in tax incentives from New York and Virginia. Up to $1.2 billion of that will come from New York state’s Excelsior program, a discretionary tax credit. In Virginia, the company could receive up to $550 million in cash incentives from the state by 2030, and $200 million more after that if it continues to hire. Both state programs are tied to the number of jobs the company creates — if Amazon’s hiring falls short of projections, the incentive payments will be smaller.

Both states have pledged to help Amazon with infrastructure upgrades, job-training programs and, in the case of New York, assistance “securing access to a helipad” — some of which did not come with a price tag.

Video

Amazon officially announced that it will put a major corporate outpost in the New York neighborhood of Long Island City. Here is what the Queens enclave has to offer the tech giant.CreditCreditJoshua Bright for The New York Times

Still, in New York and Virginia, most top politicians hailed the Amazon news as a major victory, and the culmination of an aggressive sales job.

“We had an unprecedented opportunity to add to the number of jobs,” Mr. de Blasio said on Tuesday.

The project is a “transformational opportunity to diversify the economy” of Northern Virginia, which is heavily dependent on government contracting, said Stephen Moret, president and chief executive of the Virginia Economic Development Partnership.

New York’s sales pitch began in September 2017, immediately after Amazon began its search for a second headquarters that Mr. Bezos promised would be a “full equal to our Seattle headquarters.”

Amazon executives had realized that if its projections were right, the company needed a plan for its fast growth, said Jay Carney, a senior vice president at Amazon. It made the search public because “you can’t quietly talk to cities about investing $5 billion and creating 50,000 jobs,” he said.

Shortly after the announcement, a group of Long Island City leaders, including Elizabeth Lusskin, the president of the Long Island City Partnership, a business group, and Alan Suna, the chief executive of Silvercup Studios, a film and television studio there, were meeting to discuss their plans to bring biotech and life science companies to the neighborhood. But they quickly began talking about Amazon, according to Mr. Suna.

Within a month, about 16 sites in the neighborhood had been identified, including the parcels along the waterfront eventually chosen by Amazon. The area is now a combination of public and private land that includes a distribution center for city school lunches, warehouses, studios and an outdoor bar and grill.

When New York City submitted its bid to the company, just a few weeks later, Long Island City was one of four spots proposed, along with Lower Manhattan, Midtown Manhattan’s West Side and an area in central Brooklyn.

By January, Amazon narrowed the list to 20 locations, including New York. Amazon visited in April, July and September, said a person familiar with the meetings. The executives narrowed their search to sites on the West Side of Manhattan and in Long Island City before finally settling on the Queens neighborhood.

Mr. Carney said Mr. Bezos did not tour any of the sites. The process was run by Holly Sullivan, who leads the company’s worldwide economic development. John Schoettler, the executive who oversees Amazon’s real estate, negotiated with the private developers.

Image

Gov. Andrew M. Cuomo and Mayor Bill de Blasio put aside long-standing differences to work on the Amazon deal.CreditChang W. Lee/The New York Times

During one visit, the Amazon executives visited the Cornell Tech campus, a new high-tech school on Roosevelt Island, and took the ferry from there to Long Island City. They rode Citi Bikes as city officials and local Queens representatives showed off the area. On another occasion, they took the ferry at sunset.

It wasn’t until the past few weeks that things really took off.

Gov. Cuomo met Amazon executives, including Jeff Wilke, who runs the company’s retail business, in his offices on Third Avenue. From the window, he said, they could see the site along the waterfront Amazon would eventually select.

“I showed them the pictures of the progress of LaGuardia, of J.F.K., Penn Station, Kosciuszko Bridge — I explained what doubling the span means,” he said, referring to building projects. He said Amazon executives were interested in having a pipeline of educated employees, not just from the top universities, but from other places such as Queens College and the nearby LaGuardia Community College in Long Island City.

Amazon expressed concern about the city’s planning process, which is slow and allows for local officials and the City Council to veto projects. The company’s lawyers appeared to know about those pitfalls and wanted to avoid them. So Mr. de Blasio and Mr. Cuomo agreed to let the state control the approval, meaning there could be local input but no local veto. Mr. Cuomo said it was a “friendly condemnation” of the city-controlled land by the state, not a source of tension.

Though the mayor and governor met separately with Amazon, they were in close contact, comparing notes and strategizing over the phone, according to a person briefed on the talks.

“I know him so well, it’s just more open and verbal,” Mr. Cuomo said in an interview. “Whether it’s good or bad.”

Mr. de Blasio said, “I’m very comfortable that we made the right move.”

In Virginia, the initial HQ2 announcement came as the state was wrapping up a planning process to focus its economic development on tech, as well as diversifying away from government work. The request for proposal from the company “hit right in the bull’s-eye,” said Mr. Moret of the Virginia Economic Development Partnership.

The proposal for Northern Virginia, one of three regions presented by Virginia, stressed regional cooperation. The Arlington neighborhoods of Crystal City and Pentagon City, as well as the adjacent Potomac Yards neighborhood in Alexandria, were rebranded into a new area, National Landing, as part of the proposal. That is the name Amazon used in its announcement on Tuesday.

Then after Virginia officials submitted the proposals in October 2017, things went quiet.

A few months later, Victor Hoskins, the director of Arlington Economic Development, was walking up to the stage at a conference with other development officials when their phones started buzzing with news reports that Virginia, Washington and Maryland were among Amazon’s 20 finalists.

Image

Long Island City in Queens is home to new apartment towers, low-rise manufacturing and the country’s largest public housing complex.CreditHiroko Masuike/The New York Times

“It quickly became, ‘Hey, we got three locations in that list of 20, we’ve got a good chance here,’” Mr. Hoskins said.

Crystal City is a neighborhood filled with dated office buildings developed in the 1970s for military contractors. Much of it has sat empty since the Pentagon reorganized in the years after the Sept. 11 attacks. The developer JBG Smith had recently acquired much of the land and buildings there and was trying to spruce up the staid streets. Ahead of Amazon’s first tour in late February, it wrapped buildings in colorful patterned fabrics, and hired muralists to decorate long construction walls and parking lots, trying to show how the neighborhood could be reimagined.

Amazon visited several times, at one point going up to the top of a tower, near Reagan National Airport, for a bird’s-eye view of Crystal City. But it was more down to business than New York. Catering was ordered in.

“Talent was at the core of a tremendous amount of our conversations,” Mr. Hoskins said. He said Amazon never discussed the incentives or proposals other locales offered.

Mr. Carney said inside Amazon, the team running the search decided at a meeting in August that it would be easier to hire the number of workers they wanted if they split the headquarters into two. (An Amazon spokesman later said the meeting happened the first week of September.) “We also think that 25,000 as a floor is easier for the communities to absorb,” Mr. Carney said.

The company did not tell the cities about the split immediately. When Mr. Hoskins first heard about it, he was conflicted. “You want the 50,000, but you know what, hey, 25,000 is a huge number,” he said.

By early November, local officials were feeling good. A small stage went up on an empty lot in Crystal City for the announcement, only to take be taken down quickly. It was premature.

After a final round to address the last details, on Monday, Mr. Moret was driving through rural Virginia when he signed the final state agreements digitally, his cell reception cutting in and out along the road.

“We had a call from them just an hour after we executed it saying we won,” he said.

The company said it would start hiring in New York, Virginia and Tennessee next year.