Context : Although the star itself is not helium enriched , the periodicity and the variability in the UV wind lines of the pulsating B1 IV star \beta Cephei are similar to what is observed in magnetic helium-peculiar B stars , suggesting that \beta Cep is magnetic . Aims : We searched for a magnetic field using high-resolution spectropolarimetry . From UV spectroscopy , we analysed the wind variability and investigated the correlation with the magnetic data . Methods : Using 130 time-resolved circular polarisation spectra that were obtained with the MuSiCoS échelle spectropolarimeter at the 2m Telescope Bernard Lyot from 1998 , when \beta Cep was discovered to be magnetic , until 2005 , we applied the least-squares deconvolution method on the Stokes V spectra and derived the longitudinal component of the integrated magnetic field over the visible hemisphere of the star . We performed a period analysis on the magnetic data and on equivalent-width measurements of UV wind lines obtained over 17 years . We also analysed the short- and long-term radial velocity variations , which are due to the pulsations and the 90-year binary motion , respectively . Results : \beta Cep hosts a sinusoidally varying magnetic field with an amplitude 97 \pm 4 G and an average value -6 \pm 3 G. From the UV wind line variability , we derive a period of 12.00075 ( 11 ) days , which is the rotation period of the star , and is compatible with the observed magnetic modulation . Phases of maximum and minimum field match those of maximum emission in the UV wind lines , strongly supporting an oblique magnetic-rotator model . We discuss the magnetic behaviour as a function of pulsation behaviour and UV line variability . Conclusions : This paper presents the analysis of the first confirmed detection of a dipolar magnetic field in an upper main-sequence pulsating star . Maximum wind absorption originates in the magnetic equatorial plane . Maximum emission occurs when the magnetic north pole points to the Earth . Radial velocities agree with the \sim 90-year orbit around its Be-star binary companion .