Quantum cascade lasers are compact, electrically pumped light sources in the technologically important mid-infrared and terahertz region of the electromagnetic spectrum1,2. Recently, the concept of topology3 has been expanded from condensed matter physics into photonics4, giving rise to a new type of lasing5,6,7,8 using topologically protected photonic modes that can efficiently bypass corners and defects4. Previous demonstrations of topological lasers have required an external laser source for optical pumping and have operated in the conventional optical frequency regime5,6,7,8. Here we demonstrate an electrically pumped terahertz quantum cascade laser based on topologically protected valley edge states9,10,11. Unlike topological lasers that rely on large-scale features to impart topological protection, our compact design makes use of the valley degree of freedom in photonic crystals10,11, analogous to two-dimensional gapped valleytronic materials12. Lasing with regularly spaced emission peaks occurs in a sharp-cornered triangular cavity, even if perturbations are introduced into the underlying structure, owing to the existence of topologically protected valley edge states that circulate around the cavity without experiencing localization. We probe the properties of the topological lasing modes by adding different outcouplers to the topological cavity. The laser based on valley edge states may open routes to the practical use of topological protection in electrically driven laser sources.