Hannay was born at Dumfries, Scotland, on 17 February 1827. His father, David Hannay (1794–1864), a member of the Speculative Society at Edinburgh University, 1813–14, and author of Ned Allen, or the Past Age, 1849, was engaged in business in Dumfries. The family had some reason for believing that they were descended from the Hannays of Sorble. In James Hannay, the belief was sufficiently strong to influence his studies, inclining him to study heraldry and family history.
Hannay entered the Royal Navy on 2 March 1840, on board HMS Cambridge, and served in her during the blockade of Alexandria in the Syrian war, and had therefore no share in the operations of Sir Charles Napier's squadron at Acre. From Cambridge he passed in succession to the sloop HMS Snake in 1842, the corvette HMS Orestes in 1843, and HMS Formidable in 1844. His tastes and his impatience both of routine work and control unfitted him for the life of a naval officer. Very soon after entering the service, he began to devote himself to general reading, and even studied Latin with a priest at Malta. With the instinct of a born journalist, he started a manuscript comic paper to ridicule the admiral and captains on the Mediterranean Station. At a later period, he was wont to confess that he had been a somewhat insubordinate midshipman.
In 1845, Hannay and two brother-officers were tried by court-martial and dismissed the service. The finding of the court was generally thought to have been vindictive, and it was subsequently quashed on the ground of informality. Hannay was not, however, employed again, nor did he seriously seek for employment.
From 1846 onwards till his appointment as consul in 1868, Hannay worked on the press and at literature. His first engagement was as a reporter on the Morning Chronicle, in which capacity he relied more on his remarkable memory than on his knowledge of shorthand. In the meantime, he was reading zealously in the British Museum. At the end of 1847, he worked with Henry Sutherland Edwards on Pasquin, a very short-lived comic paper, and the forerunner of the somewhat happier Puppet Show, which lasted from 1848 to 1849.
In 1848, he began using his naval experiences, and wrote the first of the stories which were afterwards collected in his Sketches in Ultramarine, published in 1853. In 1848, he first made the acquaintance of Thackeray and Carlyle, to whom he was proud to acknowledge his obligations. He soon improved his literary connection, and worked for papers of good position, for the quarterlies and magazines, till he became editor of the Edinburgh Evening Courant in 1860.
During these years he published his best work, his two naval novels, Singleton Fontenoy (1850) and Eustace Conyers (1855), and the volume of lectures on Satire and Satirist, delivered at the Literary Institution, Edward Street, Portman Square, in 1853, and collected in book form in 1854. It was during these years also that he began to write the essays to the Quarterly, afterwards collected into a volume, and that he taught himself to read Greek.
In 1857, he contested without success the representation of the Dumfries boroughs in parliament. He stood as a tory, and was defeated by William Ewart. From 1860 to 1864, he edited the Edinburgh Evening Courant. The zeal with which he attacked conduct and persons he disliked caused his management of the paper to be somewhat conspicuous. 
In 1864, he returned to London, and remained there till he was appointed consul at Brest by Lord Stanley, in 1868. During these years, he published his Studies on Thackeray (1869), his Three Hundred Years of a Norman House (1866), a portion of a history of the Gurney family, and his Course of English Literature (1866), a reprint of articles contributed years before to the Welcome Guest. Hannay did not proceed to Brest, but exchanged this post for that of Barcelona in Spain. Although he continued to write for papers and magazines, chiefly for the Pall Mall Gazette and the Cornhill, he published no more books. 
Hannay was twice married, first, in 1853, to Margaret Thompson, who died in 1865; and then, in 1868, to Jean Hannay, a lady of the same name, but of no traceable relationship, who died in Spain in 1870. He had by the first marriage six, and by the second one child, who survived him. 
- Biscuits and Grog, 1848
- A Claret-Cup, 1848
- Hearts are Trumps, 1848
- King Dobbs, 1849
- Blackwood v Carlyle, 1850
- Singleton Fontenoy, 1850
- The Poetical Works of Edgar Allan Poe: With a Notice of His Life and Genius, 1853
- Sketches in ultra-Marine, 1853
- Sand and Shells, 1854
- Satire and satirists. Six lectures, 1854
- Eustace Conyers, 1855
- Essays from The Quarterly Review, 1861
- A Brief Memoir of the Late Mr. Thackeray, 1864
- Characters and Criticisms, 1865
- A course of English literature, 1866
- Three hundred years of a Norman house; the barons of Gournay from the 10th to the 13th century, with genealogical miscellanies, 1867
- Studies on Thackeray, 1869
- Hannay 1890.
- Biographical Index of Former Fellows of the Royal Society of Edinburgh 1783–2002 (PDF). The Royal Society of Edinburgh. July 2006. ISBN 0-902-198-84-X.
This article incorporates text from a publication now in the public domain: Hannay, David (1890). "Hannay, James". In Stephen, Leslie; Lee, Sidney (eds.). Dictionary of National Biography. 24. London: Smith, Elder & Co.
- "Hannay, James." British Authors of the Nineteenth Century H.C Wilson Company, New York, 1936.