The Alpha Magnetic Spectrometer (AMS-02) is an experiment which will be mounted on the international space station (ISS) to measure primary cosmic ray spectra in space. A key element is the transition radiation detector (TRD) to extract an e+ or p- signal reducing the p+ or e- background by a rejection factor 102--103 in an energy range from 10 to 300 GeV. This will be used in combination with an electromagnetic calorimeter to provide overall p+ rejection of 106 at 90% e+ efficiency. The detector consists of 20 layers of 6 mm diameter straw tubes alternating with 20 mm layers of polyethylene/polypropylene fleece radiator. The tubes are filled with a 4:1 mixture of Xe:CO2 at 1 bar absolute pressure from a recirculating gas system designed to operate >3 years in space. There are in total 5248 straw tubes which are read out by a custom-made DAQ system in less than 80 μs. The electronics must be low in power consumption and sustain the stringent requirements of operation in space. The construction of the detector and its electronics is presented in this paper.