Space shuttle Endeavour and its six astronauts returned to Earth on Wednesday, closing out the next-to-last mission in NASA's 30-year program with a safe middle-of-the-night landing.

Endeavour touched down on the runway a final time under the cover of darkness, just as Atlantis, the last shuttle bound for space, arrived at the launch pad for the grand finale in five weeks.  

Cmdr. Mark Kelly — whose wife, congresswoman Gabrielle Giffords, remained behind at her rehab centre in Houston — brought Endeavour to a stop before hundreds of onlookers that included the four Atlantis astronauts who will take flight in July.  

"One hundred and twenty-two million miles flown during 25 challenging spaceflights," Mission Control told Kelly and his crewmates, who wrapped up U.S. construction at the International Space Station.

"Your landing ends a vibrant legacy for this amazing vehicle that will long be remembered. Welcome home, Endeavour." 

Kelly replied: "It's sad to see her land for the last time, but she really has a great legacy."  

He thanked all those who worked on Endeavour over the years.

A considerably bigger crowd gathered a few hours earlier to see Atlantis make its way to the launch pad, the last such trek ever by a shuttle. Thousands of Kennedy Space Center workers and their families lined the route Tuesday night as Atlantis crept out of the mammoth Vehicle Assembly Building a little after sunset, bathed in xenon lights.

"The show pretty much tells itself," Atlantis's commander, Christopher Ferguson, said as he waved toward his ship. "We're going to look upon this final mission as a celebration of all that the space shuttle has accomplished over its 30-year life span."  

Bright lights also illuminated the landing strip for Kelly and his crew, who made the 25th night landing out of a total of 134 shuttle flights.

The Endeavour astronauts — all experienced spacemen — departed the 350-kilometre-high outpost over the weekend, after adding the finishing touches. They installed a $2-billion cosmic ray detector, an extension beam and a platform full of spare parts, enough to keep the station operating in the shuttle-less decade ahead.

Their flight lasted 16 days and completed NASA's role in the space station construction effort that began 12 years ago.

The official tally for NASA's youngest shuttle after 25 flights, nearly half of them to the space station: 197.6 million kilometres, 170 crew members, 299 days in space and 4,672 orbits of Earth.

Kelly was the last astronaut to exit Endeavour. "Had some nice weather, and it's nice to bring Endeavour back here to KSC," he said as climbed out.

Kelly's wife was shot in the head during a mass shooting in January, but made a remarkable recovery and was able to watch the launch May 16. She did not travel to Florida for the landing because of the inconvenient hour, her husband said. They will reunite in Houston on Thursday.

Endeavour is the second shuttle to be retired. It ultimately will be put at the California Science Center in Los Angeles.

Built to replace the destroyed Challenger, Endeavour first soared in 1992 on a satellite-rescue mission that saw a record-setting three spacewalkers grab the wayward craft. Other highlights for the baby of the fleet: the first repair mission to the Hubble Space Telescope in 1993, to fix its blurred vision, and NASA's first flight to assemble the space station in 1998.