Kim Jong-un’s clandestine trip to China was in many ways an atmospheric throwback to times long gone, when it was possible to keep the movements of even a national leader hidden from public view and the stately progress of a train was preferred to air travel. (Perhaps only North Korea’s Kim dynasty – which has a congenital fear of flying, even though Kim Jong-un’s father actually died on his train – and our own Queen remain such devotees to rail.)

Whatever his mode of transport, however, and whatever efforts were made to keep it temporarily under wraps, Kim’s visit to Beijing marks the next stage in the about-face that began with North Korea’s surprise decision to compete in a joint team with South Korea in the recent Pyeongchang Winter Olympics and should culminate in the promised summit meeting with Donald Trump. In some ways it was a necessary staging post.

Kim Jong-un paid 'unofficial' visit to Beijing, Chinese state media confirms Read more

As Kim’s first known trip outside North Korea since he became leader, it illustrates his deepened sense of personal and political security. We may in the west have underestimated how precarious Kim’s position actually was seven years ago when he inherited power. Whether or not on his direct orders, his uncle Jang Song-thaek was court-martialled and executed in 2013 on charges of treason and much else. Just over three years later, Kim’s exiled half-brother, Kim Jong-nam, was assassinated at Kuala Lumpur airport with a substance later identified as VX gas. If nothing else, Kim was clearly a leader who felt his position under threat.

The Beijing trip also underlined the importance to an autocrat of clan ties, and the usefulness of family to signal trust and esteem for another – something in which Kim and the US president could well find a rapport. Kim’s sister was his personal envoy to the Olympics, and he travelled to China with his wife – a former singer whose rare appearances at his side generate the same shock effect as the early outings of Mikhail Gorbachev and his beloved Raisa. Remember, too, that Ivanka Trump represented her father at the closing ceremony in Pyeongchang.

Play Video 0:43 North Korea's Kim Jong-un meets with China's Xi Jinping - video

The China trip was a staging post, too, in offering a dress rehearsal for the main act. While not – China emphasised – a state visit, Kim and his wife were treated to almost the same protocol. They had to look and play the part expected of the first couple of a “proper” state. China’s Xi Jinping laid on a full dress banquet and a show, as well as the talks – many of the same trappings that Kim can expect when he meets Trump. The North Korean has to hold his own alongside the US president. That is most of the point. But it is as well to have a try-out first, when the footage can be edited before release if need be. You can’t see the US or its media standing for similar terms.

Kim Jong-un's visit to China fails to hide strain in relations Read more

That the Beijing meeting produced a letter from Kim to Trump shows how much this was seen as a prelude to the future summit. But it also raised questions about the precise role of China in the sudden North Korea-US rapprochement. At the height of the rhetorical standoff – the tweet wars between Pyongyang and Washington – Trump in effect flounced away, calling on Beijing to take responsibility for what he implied was more a regional, than a global, problem. There was also known to be a North Korea-US back-channel via the UN, that may have staved off resort to weapons on either side.

So while Trump may claim that the chief reason for the thaw was his uncompromising language and apparent readiness to face Kim down at his own nuclear-missile game, Beijing’s part behind the scenes may have been crucial. After all, it sees a much closer and more lethal threat from a nuclear North Korea, and can – in being able to turn on and off the economic tap – hold a threat of immediately real, rather than apocalyptic, proportions.

Yet Kim’s Beijing awaydays will not just have been a staging post. While he clearly craves international recognition for himself and his country, and regards the Trump meeting as the ultimate validation, some progress towards normal relations with China – and from there to a belated peace treaty with the South – could ultimately be the greater prize. If it is to stop playing the global villain, arguably Pyongyang needs to feel secure and accepted in its own backyard. If that eventually happens, all Kim Jong-un’s nuclear missile-waving will look more of a means than an end.

• Mary Dejevsky is a Guardian columnist