Sen. Bernie Sanders along with the Vermont delegation and his wife, Jane Sanders, cast their roll-call vote during the second day of the Democratic National Convention in Philadelphia last week. (Jabin Botsford/The Washington Post)

Late on the night before the Democratic National Convention kicked off, five Hillary Clinton and three Bernie Sanders aides huddled in a windowless room at Wells Fargo Center and began working their way through a series of concerns scrawled on a whiteboard.

Among them: the possibility of disruptions from Sanders delegates at several points in the week — and the chance that Nina Turner, a fiery Sanders advocate, would be placed in nomination for vice president to embarrass Sen. Tim Kaine of Virginia.

The meeting, a participant recalled, also included “head-butting” over speakers requested by Sanders that the Clinton camp did not want on the program, as well as the still-unresolved issue of whether the senator himself would speak during the 10 o’clock hour the next day when the networks were broadcasting live.

Like much of the convention that followed, the meeting had its share of turbulence. But it was also remarkable for its ultimate outcome: Two campaigns that had been through a bruising primary season pulled together and orchestrated a week relatively free of public controversy.

This report is based on multiple interviews with senior campaign officials in the Sanders and Clinton camps. All of the individuals requested anonymity to speak candidly about private and delicate proceedings.

The cooperation between the campaigns began even before the primaries ended, with a series of overtures that included a trip to Sanders’s home state of Vermont by Robby Mook, Clinton’s campaign manager. Both sides agreed that without their mutual efforts, what unfolded in Philadelphia could have been a disaster, just as the party was trying to unify in its fight against Republican presidential nominee Donald Trump.

As it was, things did not get off to a good start. Before the opening gavel, Sanders was booed during a meeting with his nearly 1,900 delegates when he urged them to help elect Clinton. That prompted the Clinton people to “freak out a little bit,” in the estimation of a Sanders aide who described a curse-laden phone call from Mook to Sanders’s campaign manager, Jeff Weaver.

Shortly afterward, Sanders sent an extraordinary text message to his delegates urging them “as a personal courtesy to me not to engage in any kind of protest on the floor.” That message went unheeded by dozens of delegates, as they booed mentions of Clinton’s name and sought to disrupt speakers early in the evening.

Many were smarting over the disclosure from days earlier of leaked emails from the Democratic National Committee showing staffers there talking about ways to undermine Sanders’s primary campaign.

Although heckling continued through the final night, on Thursday, the atmosphere was far different by then, as both camps worked together to tamp down dissent. During her speech, Clinton made a point of directly addressing Sanders supporters, declaring: “I’ve heard you. Your cause is our cause.” By the time the balloons dropped, Clinton’s team was ready to declare the week a success.

“It could have been very chaotic,” said an aide to Sanders, whose delegates included some who were new to party politics and unwilling to allow that Clinton should be the nominee. “It could have looked like it did when it started Monday night, only all week long.”

A supporter cries as Sen. Bernie Sanders addresses the Democratic National Convention in Philadelphia July 25. (Michael Robinson-Chavez/The Washington Post)

Instead, by Thursday night, most Sanders delegates “were waving ‘Hillary’ or ‘USA’ or ‘Stronger Together’ banners and American flags,” one Clinton adviser said. “It was a unifying event.”

The contest between Clinton and Sanders began on a higher plane last year, with both candidates rarely talking about the other. But as Sanders’s standing rose, so too did the vitriol between the two candidates, who ultimately questioned each other’s judgment and qualifications. After Clinton clinched the race in June, it took Sanders a full month to offer his endorsement.

Clinton’s efforts to make peace began well before then. On most nights during the long primary season, Clinton picked up the phone and called Sanders to offer congratulations when he scored a victory, a senior adviser to Clinton said. He did the same with her.

“By the time we got into the convention, a level of trust had developed between the two sides,” a senior Clinton adviser said. “We weren’t always happy with each other’s points of view, but we knew that we could trust each other, and we trusted each other’s words.”

The relationship between the two campaign managers, built over many months, was one of the keys.

Although both were Vermonters, Mook and Weaver had not met before the race. But as far back as October, at the Jefferson Jackson Dinner in Iowa, the two displayed a public friendliness, posing for photos together, with Mook holding a “Bern Unit” sign and the two wearing identical scuffed shoes.

By the June 7 California primary, which dashed Sanders’s last-ditch hopes of wresting the nomination from Clinton, Weaver and Mook were talking almost every day. That led to a pivotal face-to-face meeting between Clinton and Sanders a week later. They, and a few top aides, convened behind closed doors in the Carol Rowan suite at the Capital Hilton in Washington shortly after the polls closed in the District, which held the final Democratic nominating contest, on June 14.

What soon became clear was that Sanders was focused on winning concessions on policy — and that Clinton was willing to accommodate him, to a point. They agreed on two issues, college tuition and access to health care, where they could craft joint initiatives, melding proposals the two had pushed during the primaries.

As soon as the principals departed that night, Weaver and Mook remained for two hours to discuss how to move forward. Mook did two impressions to lighten the mood: one of Bill Clinton and the other of Sanders.

In the weeks that followed, policy staff from both campaigns, as well as Sanders’s wife, Jane, worked to craft proposals to advance Sanders’s agenda but remain consistent with Clinton’s principles.

Sanders, for example, had championed making college tuition free for everyone who attends public universities and colleges. Clinton often derided his proposal, saying taxpayers should not foot the bill to send Trump’s kids to college. Their compromise calls for free tuition for families making up to $125,000 a year.

Underscoring their progress, Mook traveled to Vermont in late June for dinner with Weaver at the Farmhouse Tap & Grill, in Burlington, where Sanders maintained his presidential campaign headquarters. Weaver had a pork burger; Mook had a salad. They talked until nearly 11 p.m.

In early July, Clinton rolled out her revamped proposals on college tuition and health care, promising to push for a “public option” that would allow people to buy into government insurance as part of the Affordable Care Act. That was a far cry from Sanders’s proposal for a single-payer system but seemed a step in that direction.

Sanders was also determined to advance as many of his ideas as possible through changes to the Democratic platform. His aides had started talking about the strategy back in March, when it became evident that Sanders’s odds of winning the nomination were growing longer.

Over a series of many committee meetings in multiple cities, Sanders got about 90 percent of what he wanted, Weaver said, including a call to raise the federal minimum wage to $15 an hour and some aggressive steps to combat climate change.

With a number of victories secured, Sanders was finally ready to endorse Clinton.

The pair emerged together three weeks ago in a crowded Portsmouth, N.H., gym, accompanied by a Bruce Springsteen song that Sanders had used to enter his political rallies. Although Sanders would go on to say the right things, their chemistry was awkward at times.

The endorsement did not sit well with some Sanders supporters, who saw it as a sellout to the Democratic establishment. But his advisers said no one should have been surprised, given that Sanders said from the outset that he planned to support the party’s nominee.

“I think Bernie feels he’s moved the party a long way, and meaningful things could come from it if Clinton is in the White House.” a Democrat with close ties to the campaign said. “Now that he had pushed the agenda as far as he could possibly push it, it was time to get onboard with her.”

Efforts to present a unified front at the convention hit a major speed bump just before it started, with the publication by WikiLeaks of thousands on internal Democratic National Committee emails. Sanders delegates arrived in Philadelphia “inflamed,” an aide said.

During a Friday night huddle, Weaver told Mook that it was Sanders’s long-held position that DNC Chairwoman Debbie Wasserman Schultz, a congresswoman from Florida, needed to resign — something that happened two days later. Still, the Sanders team was determined to work with the Clinton camp to ensure as smooth a convention as possible. Some of that was admittedly due to self-interest, an aide said.

“It was always our feeling that booing and outbursts would reflect badly on Bernie, so we worked in concert with them to try to avoid that,” the aide said.

In an attempt to foster as much unity as possible, the Clinton team encouraged their delegates in the states to become friends with the Sanders delegates long before the convention started. In some states, delegates organized cookouts together. Some traveled to Philadelphia together.

A top Clinton priority was to avoid messiness on the convention floor whenever possible. That included the prospect of “minority reports” being filed to contest parts of the party platform, which would have opened up divisive debate of issues, such as trade, for all of the news media to witness. Sanders himself signaled to his delegates that he did not want them to pursue that course on the platform.

Several points of tension remained between the two campaigns, including some that were never amicably resolved. One flash point was over which Sanders supporters would be allowed to address the convention.

Turner, a former Ohio senator and outspoken advocate, said that Sanders had personally asked her to be among those who put his name into nomination. But Sanders later circled back with her and said it would not be possible. Asked why, Turner said, “You can read between the lines.”

Additionally, the timing of Sanders’s speech went unresolved until shortly before the convention began. He was slated for Monday night, but the Clinton camp initially tried to steer him toward the 9 o’clock slot, before the broadcast networks picked up coverage.

“We said that’s insulting to our guy. He should be in prime time,” a Sanders aide said.

Clinton’s team finally yielded and made him the final speaker of the evening, scheduled for 10:40 p.m.

The two camps also worked to orchestrate Sanders’s role in Clinton’s formal nominating process. Clinton wanted Sanders to do something similar to what she did for President Obama in 2008 — halting the roll call of states when it reached New York and moving that Obama be nominated by acclamation. Sanders was dead-set on having all the states cast votes so that his people would feel like their efforts over the past seven months had mattered.

A plan was hatched to have Sanders make his symbolic motion at the end of the process and to make clear that all votes would still be reflected in the record of the proceedings. When it happened Tuesday night, it came across as a much-needed gesture of unity.