Tibetan attack on Songzhou

The first military conflict between the Chinese Tang dynasty and the rising Tibetan Empire occurred in 638. When Emperor Taizong of Tang refused a marriage alliance, the Tibetan emperor Songtsän Gampo sent an army to attack the Chinese frontier city of Songzhou (松州, in modern Sichuan). After a Tang army inflicted heavy casualties on the Tibetans in a night-time attack, Songtsän Gampo withdrew. He sent emissaries and tributes to Chang'an to apologize, and to again request marriage. Taizong decided to give Songtsän Gampo a distant niece, Princess Wencheng, in marriage. The peace held for the remainder of the reigns of Taizong and Songtsän Gampo, although Tibet would pose major military threats for most of the rest of the Tang period.

Initial contacts between Tang and Tibet
During the early decades of the 7th century, the major threat to the west of China was the Xianbei state of Tuyuhun. Thereafter, Tuyuhun's southwestern neighbor, the Tibetan Empire, rose in power.

The existence of Tibet was unknown to the Chinese until 608, when Tibetan emissaries from Emperor Namri Songtsen arrived with tribute to Sui China. In 634, his son Songtsän Gampo sent tribute and a request for a heqin ("marital alliance"). In the interim, a North China aristocrat had defeated the Sui and declared himself Emperor Gaozu of Tang. When Songtsän Gampo's marriage overture arrived, Taizong, the second Tang emperor, was battling the Tuyuhun and did not initially respond, but did sent the emissary Feng Dexia (馮德遐) to Tibet to establish peaceful relations.

Conflict in 638
Meanwhile, in late 634, Taizong had sent the general Li Jing against Tuyuhun and, in a major campaign, overpowered Tuyuhun's Busabo Khan Murong Fuyun, who was killed in flight. Tang thereafter appointed Murong Fuyun's son Murong Shun as Tuyuhun's khan and, after Murong Shun was assassinated late in 635, supported Murong Shun's son Murong Nuohebo as khan.

Feng Dexia appeared to have gotten to Tibet around the same time. By this time, Songtsän Gampo was aware that, in the past, the khans of the Eastern Turkic Khaganate and Tuyuhun had arranged marriages of state with China and therefore sent an emissary to accompany Feng back to Tang with further tribute to request to marry a Tang princess.

Taizong turned the proposal down. When the Tibetan emissary returned to Tibet, he informed Songtsän Gampo, falsely according Tibetan historical sources, that Taizong was disposed to approve a dynastic marriage but changed his mind after hearing the Tibetans slandered by the Tuyuhun. It was said that Murong Nuohebo had visited Tang and was interfering, leading to Taizong's refusal. Songtsän Gampo, believing the report, attacked Tuyuhun in late 637 and early 638, capturing some of them and forcing the rest to flee north of Qinghai Lake.

In the fall of 638, Tibetan forces, apparently commanded by Songtsän Gampo himself, then raided the Tang frontier city of Songzhou (松州, modern Songpan County in Sichuan), but meanwhile sending emissaries to the Tang capital Chang'an, again offering tributes and declaring that they were intending to welcome a princess. The size of his army is given as 100,000 by Tibetan sources and over 200,000 by Chinese sources. They defeated a force sent against them by the Songzhou governor Han Wei. According to the Chinese annals, Taizong responded by commissioning the general Hou Junji to command an army, assisted by the generals Zhishi Sili (執失思力), Niu Jinda (牛進達) and Liu Jian (劉簡). Led by Niu, the Tang army inflicted heavy casualties on the Tibetans in a surprise night-time attack. Alarmed, Songtsän Gampo withdrew, sent emissaries and tributes to Chang'an to apologize and to again request marriage. Emperor Taizong agreed this time.

Marriage between Songtsän Gampo and Princess Wencheng
However, no further action was taken to carry out the marriage for about two years. In fall 640, Songtsän Gampo sent his prime minister Lu Dongzan (祿東贊) to Tang to offer tributes of gold and jewels, again requesting marriage. In response, Taizong created a daughter of a kinsman, Princess Wencheng, preparing to give her to Songtsän Gampo in marriage. Impressed by Lu Dongzan's propriety in interacting with him, he also, over Lu Dongzan's own objection—that he already had a wife and that it would be inappropriate for him to marry before his king—gave Lady Duan, the granddaughter of Princess Langye, to Lu Dongzan as a wife as well.

In spring 641, Taizong sent his cousin, Li Daozong, Prince of Jiangxia, to accompany Lu Dongzan back to Tibet and to escort Wencheng. When they arrived in Tibet, Songtsän Gampo was said to be so pleased that he bowed to Li Daozong, using ceremony appropriate for a son-in-law toward a father-in-law. He built a palace for Wencheng and changed into Chinese clothing before he met her. It was said that at that time, the Tibetan people had a custom that Princess Wencheng hated—that people would paint their faces red—and that he prohibited the custom for her sake.

Aftermath
As part of the agreement, he also sent nobles and family members to Chang'an to study at Tang's imperial university, in an old custom which made them de facto hostages, while they learned Chinese customs and culture for better relationship. Songtsän Gampo also requested Chinese scholars. Early in Gaozong's reign, Tibet also requested technology transfers for sericulture, winemaking, gristmills and papermaking.

The marriage alliance began two decades of peace between the two empires. In 647, for when Taizong sent a force under the command of the Göktürk prince Ashina Shö-eul on a punitive expedition against the state of Kucha under its new king Hari Pushpa, after his predecessor had refused to pay tribute in protest at China's interventionist policy, Tibetan troops were requisitioned. Moreover, in 648, when the Tang emissary Wang Xuance became stuck in political turmoil of an Indian state, he sought aid from both Tibet and Nepal and was assisted by both in defeating one of the factions in 649.

During this period, the Tibetans strengthened and expanded their empire. By the late 660s they had overrun the Tuyuhun, and were in direct contact with Tang territory. The two empires fought sporadically over the following decades, and much of eastern Tibet fell to the Tang. With the weakening of Chinese power consequent on the An Lushan Rebellion (755–763), the Tibetans managed to recapture vast swathes of their lost territory, overrunning Songzhou and the surrounding area in 763, and even briefly capturing the capital Chang'an. Songzhou (Tibetan Sharkok ) was thereafter reportedly settled by Tibetans from Ngari and identified in Tibetan geographical writings as part of the expanding Tibetan empire, classified variously as part of Amdo or Kham. Sharkok and neighboring Khöpokok (Jiuzhaigou) remain Tibetan-speaking areas to this day (previously classified as Amdo Tibetan, now tentatively classified as five distinct dialects of an independent branch of Tibetan, Sharkhog Tibetan.).

The effect of the resurgence of the Tibetan Empire was to facilitate the proselytization of Buddhism north and westwards, to the detriment of the expansion of Islam. It was a decisive factor in the rerouting of China's silk commerce and East-West trade patterns, which shifted northwards through Uighur lands.