Sensory information from mechanoreceptors and nociceptors in the skin plays key roles in adaptive and protective motor behaviours. To date, very little is known about how this information is encoded by spinal cord cell types and their activity patterns, particularly under freely behaving conditions. To enable stable measurement of neuronal and glial cell activity in behaving mice, we have developed fluorescence imaging approaches based on two- and miniaturized one-photon microscopy. We show that distinct cutaneous stimuli activate overlapping ensembles of dorsal horn neurons, and that stimulus type and intensity is encoded at the single-cell level. In contrast, astrocytes show large-scale coordinated calcium responses to intense but not weak sensory inputs. Sensory-evoked activity is potently suppressed by anaesthesia. By revealing the cellular and computational logic of spinal cord networks under behaving conditions, our approach holds promise for better understanding of healthy and aberrant spinal cord processes.