Proposed in 1984, quantum key distribution (QKD) allows two users to exchange provably secure keys via a potentially insecure quantum channel. Since then, QKD has attracted much attention and significant progress has been made both in theory and practice. On the application front, however, the operating distance of practical fibre-based QKD systems is limited to about 150 km (ref. 4), mainly due to the high background noise of practical single-photon detectors and inefficient finite-key security analysis. Here, we present, for the first time, a compact and autonomous QKD system that is capable of distributing provably secure cryptographic keys over 307 km of optical fibre. This is achieved by using semiconductor single-photon detectors with record low background noise and a novel finite-key security analysis, which is efficient even for short key lengths. This demonstrates the feasibility of practical long-distance QKD based on standard fibre-optic telecom components.