The text of Afghan President Babrak Karmal's letter to Iranian leader Ayatollah Ruhollah Khomeini this week shows a clear determination to make common cause against "American imperialism."

Khomeini so far has not publicly reacted to the Soviet invasion of Afghanistan. Preoccupied with his crisis with the United States, he has stressed the slogan, "all guns trained on America."

Other Iranian Moslem leaders, however, have condemned the communist Afghan government's war against Moslem insurgents, who want to set up an Islamic republic similar to Iran's.

In his letter to Khomeini, Babrak said "certain Iranian men and circles . . . are engaged in unfriendly and unbrotherly activities at the instigation of American world imperialism -- the number one enemy of all the peoples of the world including the honorable people of Iran."

The letter appeared to reflect Soviet policy toward Iran: a desire to seek an accommodation with Khomeini to fill the vacuum left in Iran by the exit of U.S. influence. Further down the road in Kremlin thinking, diplomats say, is a possible role for Iran's pro-Moscow Tudeh communist party, which has given Khomeini blanket support despite his disdain for its atheism.

Playing on themes dear to Khomeini, Babrak in his letter linked his slain predecessor, Hafizullah Amin, to the deposed shah of Iran "and other historic hangmen."

Denouncing an "unholy alliance" of the United States, Egypt, Pakistan and China, Babrak also accused "American imperialism" of preparing an offensive against the "liberating Islamic revolutionary movement of the heroic people of Iran."

Babrak assured Khomeini in the letter published here that Soviet troops had entered Afghanistan only to repel a foreign threat and that they would "return home" after "the elimination of the danger of aggression and intervention." Western intelligence sources have claimed that much of the Soviet military power in Afghanistan has been placed within 100 miles of Iran's border.

Addressing Khomeini repeatedly as "your reverend grace," Babrak promised that his government "will never allow anybody to use our soil as a base against the Islamic revolution of Iran . . . and we expect our Iranian brethren to assume a reciprocal stance."

The Afghan communist leader expressed a desire to meet Khomeini personally "as soon as possible" and remove "any misunderstandings between the two nations."