In July 1840 a fleet of British warships approached the southern coast of China, intent on avenging a series of insults and injuries inflicted on British subjects over the preceding months. The first battle lasted nine minutes. Thus began the first opium war, a series of unequal military encounters lasting until 1842. A second opium war culminated in 1860 with the looting and burning of the imperial pleasure grounds, the Yuan Ming Yuan, in the northwest suburbs of Beijing by British and French troops.

At the time, as Julia Lovell explains lucidly and compellingly, these events were perceived largely as a border skirmish. The Qing emperor was preoccupied with a series of internal rebellions, and his officials were so nervous of passing on the letters the British handed in that he had little idea of what the trouble was about. When hostilities began, repeated accounts of glorious Chinese victories over the barbarians left the emperor in the dark about the real outcome. It was an inglorious episode on both sides, with its roots in an expanding imperial power being rebuffed in its efforts to trade: there was nothing, the Chinese loftily replied to the British emissaries, that China needed or wanted from the west – not their goods, not their ideas and certainly not their company. There was plenty that the British wanted to buy from China, though, and by the 1780s, the British appetite for tea and Chinese indifference to British goods had produced a trade deficit that the East India Company began to fill by supplying opium grown in British Bengal. It was a trade that greatly benefited the British exchequer, the merchants who traded it, the officials who grafted on it, the Chinese wholesalers who bought it and the foreign missionaries who travelled with it.

Opium had been consumed in China since the eighth century and several emperors had sung its praises. It began to be smoked with the introduction of tobacco in the late 16th century, turning its consumption from a medicinal to a social habit. By the 1830s, China was producing large quantities of opium domestically, though the imported drug was judged superior. The British traders argued, disingenuously no doubt, that they were merely supplying an existing demand, delivering the opium to a network of Chinese traders who distributed it across the empire.

When the indecisive and harassed Emperor Daoguang, himself a user when young, came to the crumbling Qing throne in 1820, he attempted to stamp out a habit that was all but universal. He was ostensibly moved by anxieties about a balance of payments deficit and a shortage of silver, both blamed on the opium trade, but Lovell argues that the trade also became the scapegoat for the many ills and rebellions that beset the empire. In December 1838, after years of debate and ineffective action, the emperor appointed Lin Zexu as commissioner in Canton with instructions to stamp it out. Within two months Lin had arrested 1,600 smokers and confiscated nearly 14 tonnes of opium.

In March 1839 Lin ordered all foreign merchants to hand over their stocks and to undertake to bring no more. The British representative, Charles Elliot, agreed to hand over more than 20,000 chests while assuring the merchants that the British crown would make good the losses, thus transforming the dispute into an affair of state. Lin reported to the emperor that the matters were concluded satisfactorily. A few months later, somewhat to his surprise, the British gunboats arrived.

A large cast of characters played their part in the tragicomedy that resulted: incompetent officials, merchant adventurers, unscrupulous politicians, drunken soldiers, muscular military imperialists and the bewildered and vacillating emperor. By 1842 Britain was in possession of Hong Kong after a military episode fought by now in the name of free trade, and each side's view of the other was set.

No less interesting than the events themselves is Lovell's account of the war's afterlife. For the British, the opium war defined the Chinese as decadent orientals, caricatured in popular fiction in the early 20th century. Their influence lingers in recurrent racist stereotypes as China's rise sets western nerves on edge.

In today's China, the opium war has been elevated to a national cause. For more than a century, the ruins of the Yuan Ming Yuan lay neglected. Today they sit in one of many memorials to the "century of national humiliation" constructed after the crushing of the student protests in Tiananmen Square in 1989. In that moment of national crisis the communist party's right to rule was challenged and its ideology discredited. The party's solution was to try to persuade its people that a party that had just turned its guns on the students was the sole defence against a west that had long conspired to sabotage China.

Much was made of the 150th anniversary of the war the following year. Scarcely mentioned in school text books until that point, the war became a narrative of heroic resistance to western imperialist aggression that led inevitably to socialism and communist party leadership. The Patriotic Education Campaign that followed had three key arguments: that China, with its long and unique history, was unready for democracy; that foreigners caused all its sufferings; and that only the party could save the nation. History remains, as the party defines it, a "meaningful security issue".