It is going down in history as something called "The Visit of the Leaders of the Western Hemisphere" which, translated, means the week the United States gave Panama back the canal while North, South and Central America looked on.

"It is a state visit," said U.S. Chief of Protocol Evan S. Dobelle at one point during the scramble to get ready, "by 27 times."

It is, of course, a monumental jam of high-level traffic which builds to a climax Wednesday evening at the Pan American Union when President Carter, on behalf of the United States, and Gen. Omar Torrijos, for panama, sign a pair of treaties that took 13 years to agree upon.

That's the part everybody will see. The part everybody won't is what has gone into getting the cast of several hundred - not counting supporting actors like aides and the press - together in the first place.

It all began when Secretary-General of the Organization of American States Alejandro Orfile took what one OAS official said was "a very active role" is bringing the signing ceremony to the Pan American Union. Under consideration as a site had been Panama (mostly by Torrijos), the White House (mostly by Carter) and the United Nations (mostly by everybody else hoping to avoid an argument).

Orfila says merely that "President Carter and Gen. Torrijos thought it would be a good idea to sign the treaties in a place that belonged to both countries." Orfila, not surprisingly, couldn't have agreed more - just as long as it narrowed down to the OAS' own Pan American Union.

Once Orfila's invitations were out, a multi-member task force of White House, State Department, National Security Council and Secret Service officials came into play. Their job was to coordinate who was coming, when and how they were arriving, what they were going to do when they got here, and keep everybody friends.

Housing was left to the individual embassies, with the exception of Panama, Torrijo, Mrs. Torrijo and their daughter, Racquel, 18, will stay in the official residence of former U.S. Presidents on Jackson Place. If that decision created a problem of where to put former President Gerald R. Ford or Lady Bird Johnson, both of whom are invited to the ceremony, it was quickly solved when the Carters turned over the White House's Lincoln Bedroom to Ford and the Queen's Bedroom to Mrs. Johnson.

With so many heads of state coming to town, prtocol has been no small consideration.

"You treat Panama first in the order of precedence," said Dobelle. "After that it goes by rank of seniority which means Paraguay."

At least, as far as the United States is concerned.

As far as the OAS is concerned, precedence is dictated by the Spanish alphabet.

"Everybody will arrive at the OAS alphabetically (meaning Argentina is first)," said Dobelle, "and leave for the White House according to seniority."

At the White House, where the Carter's have invited them to a state dinner Wednesday night after the signing, the hemispheric leaders very likely will not only be greeted by the Carters but by Ford and Mrs. Johnson as well.

President Ford was invited because he worked "as much" on the treaty, said one White House official who assumed that Mrs. Johnson "has an interest." Richard Nixon, another former President, was not invited.

Who will sit where at the dinner is still unresolved, but White House Social Secretary Gretchen Posten said she does not plan to work out seating until Wednesday, the day of the dinner. "I don't seat until then," she said yesterday. "I never do."

One reason is that other than the heads of state, she is uncertain about where else is coming. (Social office assistants spent the weekend telephoning invitations around the country and the hemispere.)

They tracked one guest to Venezuela, said Poston, who reported "we got him." Despite the last-minute nature of the dinner, there have been "very few" regrets. Plans for it or related social events, which include a luncheon Rosalynn Carter will give Thursday for the wives of visiting leaders, aboard a Wilson Lines catamaran to Mount Vernon, did not get underway until 10 days ago. An additional disadvantage has been the Labor Day weekend - "A lot of people aren't home," said Poston.

Even more "challenging," she said, has been arranging the entertainment, a musical program that will fature violinist Isaac Stern, pianist Andre Previn and opera singer Martina Arroyo, all flying into Washington in time for a last-minute 6 p.m. Wednesday rehearsal.

The next day, they fly out again in similar split-second timing. Stern and Previn have concert dates in Dallas and Pittsburgh, respectively. Arroyo has been in the Virgin Islands recovering from back injuries.

Dobelle, for his part, will stand by at the White House long enough Wednesday night to introduce the heads of state, heads of governments or heads of delegatins to the Carters. Then he will rush to Foggy Bottom to take over similar duties from Deputy Chief of Protocol Stuart Rockwell at the simultaneous dinner Secretary of State Cyrus Vance is giving at the State Department.

Nearly 300 guests have been invited to that event and will include everybody who didn't make it to the White House, which includes all foreign ministers and all U.S. and foreign ambassadors here for the occasion.

The largest delegation among the visitors will be that of the Panamanians. The smallest delegations - one man apiece - will be those of Barbados, Surinam and Trinidad-Tobago, arriving Wednesday from their United Nations' posts in New York.

Between those extremes will be delegations of varying numbers. Rosalynn Carter personally will welcome seven - Jamaica, Costa Rica, Venezuela, Brazil, Colombiaa, Peru and Douador - starting today when she goes to Andrews Air Force Base directly from Camp David, where she and the President spent the weekend. All were countries she visited on her Latin American diplomatic mission in June.

The week's social activities begin officially Tuesday with a 6-to-8 p.m. reception for 1,500 guests, given by Orfila at the Pan American Union. The First Lady will represent the President.

Wednesday, the Council of the Americas will give a non luncheon for visiting heads of state at the Kennedy Center Atrium. That evening, Orfila will give another reception, this one limited to heads of state, government or delegations plus the Carters and Torrijos - "a way to get them there," said one official - before the 7:30 p.m. signing ceremony.

The White House and State Department dinners wind up the day, with some leader even leaving for home that night. "They're got to get back for the King of Spain, who's making a visit to South America," said Dobelle.

Those who stick around Washington can look forward to reception at their own embassies and lunch with the Senate Foreign Relations Committee on Thursday.

Expected to be discussing the controversial treaties with the senators, who must ratify them while the Panamanians must approve them in a plebescite, will be Panama's Gen. Torrijos.