On Her Majesty's Secret Service was written in Jamaica at Fleming's Goldeneye estate in January and February 1962, whilst the first Bond film, Dr. No was being filmed nearby. The first draft of the novel was 196 pages long and called The Belles of Hell. Fleming later changed the title after being told of a nineteenth-century sailing novel called On Her Majesty's Secret Service, seen by Fleming's friend Nicholas Henderson in Portobello Road Market.
As with all his Bond books, Fleming used events or names from his life in his writing. In the 1930s, Fleming often visited Kitzbühel in Austria to ski; he once deliberately set off down a slope that had been closed because of the danger of an avalanche. The snow cracked behind him and an avalanche came down, catching him at its end: Fleming remembered the incident and it was used for Bond's escape from Piz Gloria. Fleming would occasionally stay at the sports club the Schloss Mittersill in the Austrian Alps; in 1940 the Nazis closed down the club and turned it into a research establishment examining the Asiatic races. It was this pseudo-scientific research centre that inspired Blofeld's own centre of Piz Gloria.
The connection between M and the inspiration for his character, Rear Admiral John Godfrey, was made apparent with Bond visiting Quarterdeck, M's home. He rings the ship's-bell for HMS Repulse, M's last command: it was Godfrey's ship too. Godfrey was Fleming's superior officer in Naval Intelligence Division during the war and was known for his bellicose and irascible temperament. During their Christmas lunch, M tells Bond of an old naval acquaintance, a Chief Gunnery Officer named McLachlan. This was actually an old colleague of both Godfrey and Fleming's in the NID, Donald McLachlan.
The name Hilary Bray was that of an old-Etonian with who Fleming worked at the stock broking firm Rowe & Pitman, whilst Sable Basilisk was based on "Rouge Dragon" in the College of Arms. Rouge Dragon was the title of heraldic researcher Robin de la Lanne-Mirrlees who asked Fleming not to use the title in the book; in a play on words, Fleming used Mirrlees's address, a flat in Basil St and combined it with a dragon-like creature, a basilisk, to come up with the name. Mirrlees had Spanish antecedents, generally born without earlobes and Fleming used this physical attribute for Blofeld. Mirrlees also discovered that the line of the Bonds of Peckham bears the family motto "The World is Not Enough", which Fleming appropriated for Bond's own family.
Fleming also used historical references for some of his names and Marc-Ange Draco's name is based upon that of El Draco, the Spanish nickname for Sir Francis Drake, a fact also used by J. K. Rowling for the naming of her character Draco Malfoy. For Tracy's background, Fleming used that of Muriel Wright, a married wartime lover of Fleming's, who died in an air-raid and Bond's grief for the loss of his wife is an echo of Fleming's at the loss of Wright. Fleming did make mistakes in the novel, however, such as Bond ordering a half-bottle of Pol Roger Champagne: Fleming's friend Patrick Leigh Fermor pointed out that Pol Roger was the only champagne at the time not to be produced in half-bottles.
On Her Majesty's Secret Service is the second book in what is called "the Blofeld trilogy", sitting between Thunderball, where SPECTRE is introduced and You Only Live Twice, where Blofeld is finally killed by Bond. Although Blofeld is present in Thunderball, he directs operations from a distance and as such he and Bond never meet and On Her Majesty's Secret Service constitutes his and Bond's first meeting.