Many sci-fi works have planets which are just one big city. (e.g. Trantor in Foundation and Coruscant in Star Wars)
According to these sources, the first person to write about city planets was the preacher and spiritualist poet Thomas Lake Harris: way back in the 19th century, he wrote poetry about themes including interplanetary empires, imperial cities entirely covering planets, and the "ancient astronaut" myth in which space travellers help early humans with agriculture, technology and spiritual development.
But if you're only looking for really science-fictiony references, then the first instance would be
one you already mentioned: Trantor in Isaac Asimov's Foundation series from the 1940s maybe the one @user14111 found, Edmond Hamilton's The Universe Wreckers (1930)?
1 Warning: following this link will lead you to the TV Tropes website, where you may spend hours following a maze of further links.
Isaac Asimov's Foundation series starts with the novelette "Foundation" in Astounding Science-Fiction, May 1942 (available at the Internet Archive), so that's the date to beat. No doubt that's the first notable work of science fiction, and probably the first readable one, to feature a city-planet, but you asked for the earliest one. I don't know if it's the very earliest, but the earliest one I know of, a dozen years before Asimov's "Foundation", is Edmond Hamilton's "The Universe Wreckers", which ran as a 3-part serial in the May, June, and July 1930 issues of Amazing Stories (available at the Internet Archive , , ), and which featured not one but two city-planets, Neptune and its moon Triton.
In the story, both Neptune and Triton, the latter having been colonized by the Neptunians, are completely covered by spherical roofs. A roofed-over planet is not necessarily a city, but I think these ones qualify. In case you want to check up on me, all the quotations are from part 2 of the serial, in the June 1930 Amazing. This one is from p. 257; I quote the whole paragraph so you can enjoy the prose:
We were abruptly silent as the guards glanced suspiciously toward us with their bulging multiple eyes. And as the great cylinder and those behind shot on, the huge metal roof of Neptune below and the vast vapor-masses of its dense atmosphere stretched above us, I wondered if ever men had found themselves in the position that now was ours. Captured by monstrous disk-bodied beings of horror unutterable, flashing with them above the vast roof that sheathed Neptune and its dead, deserted and colossal compartment-city, to a destination of which we could not dream! And as that thought passed, another came, and I remembered the great mission that had brought us out here to the terrors of mighty Neptune, our great flight outward to find and put an end to the huge force-ray that was stabbing across the solar system and turning the sun ever faster, with every day bringing it nearer to the division that meant doom for almost all its universe. What chance was ours to accomplish that mission now, separated and captured as we were, not knowing even from what source the great ray was issuing, from what strange place these disk-bodied beings had come and to which they were now returning?
From p. 258:
Triton! It was from it, then, that there had come these strange disk-bodied Neptunians who had captured us, who had annihilated our space-flier and our friends. It was on Triton, then, that there must remain whatever Neptunians still were left of those who had built the vast compartment-city that covered all the surface of Neptune itself, who had shielded it with that gigantic floating roof that enclosed all the mighty planet. Yet why had they deserted their vast compartment-city, their great world of Neptune? Why had they left that world for the single moon of Neptune, so much smaller in size?
Arriving at Triton, p. 261:
Above our sinking cylinders now there stretched the great roof, and, even as Neptune's enclosing roof, this one was almost entirely transparent from below, though opaque from above. And here as on Neptune we could see no supporting pillars whatever for this vast spherical roof that enclosed all Triton. This world seemed, indeed, but a smaller replica of mighty Neptune. For, as our cylinders sank down through the shadows of its darker side and then leveled out and began to race back around its curving surface toward the sunward side once more, we saw that all of Triton's surface was covered, even like Neptune, with a great compartment-city whose intersecting black walls stretched in their vast checkerboard arrangement over all the great moon's surface. But as our cylinders shot over these, over the darkened portion of the surface of Triton and toward the sunward side, we saw that the compartment city beneath was different, in some features, from that of Neptune.
All the Neptunians have migrated to Triton and it's really crowded, p. 261:
At last, as Marlin and I gazed ahead, we could make out a brighter crescent of light at the edge of the strange moon-world, and as we shot on we saw that we were approaching the edge of the sunward or sunlit side. A moment more and we could see it clearly and as we did so Marlin and I gasped in utter amazement. For that part of the great compartment-city that lay on Triton's sunward side, in the pale sunlight was swarming with incalculable millions upon tens of millions of Neptunians! Crowding, seething, pressing together, they were pouring to and fro through the compartments in the pale light of day, busy with the mechanisms that scarce had room in those compartments, so great were their crowds! And over this sunward side hundreds upon hundreds of cylinders swarmed, rushing to and fro!
"Neptunians! Neptunians in countless millions here on the sunward side of Triton! But why then are there so few upon the dark side?"
The dark side not as empty as it looks, p. 262:
And as that dazzling light-band moved around the big moon-world's dark side, around the almost empty compartment-city that covered that dark side, we saw emerging into that compartment-city of the dark side, as though from its walls themselves, millions on millions of disk-bodied Neptunians that matched in number the vast swarms on the sunlit side! And as we gazed down in utter amazement we saw from whence they came. There were in the dark compartment-city's extent many compartments like those we had seen upon Neptune, with nothing in them save shelving, which formed in their walls myriads of shelfed openings a few feet in height and some four feet in width, one above the other. And in these narrow, flat shelf-openings countless Neptunians had been sleeping! Their disk-bodies, with the flexible legs drawn up, fitted snugly into those flat, strange openings in the walls, and vast hordes of them, countless millions of them, had been sleeping in the shelf-compartments on Triton's dark side!
(Maybe I should explain that in this story Triton has a permanent dark side; although it is in orbit about Neptune, it always turns the same face to the sun.)
"The Machine Stops" (1909) by E.M. Forster
The Machine Stops at Wikisource
By the same author that wrote A Passage to India, it also predicts the internet, instant messages and tablet computers.
In this story, the whole earth is essentially one air-conditioned building.