The spin of an electron is a promising memory state and qubit. Connecting spin states that are spatially far apart will enable quantum nodes and quantum networks based on the electron spin. Towards this goal, an integrated spin-photon interface would be a major leap forward as it combines the memory capability of a single spin with the efficient transfer of information by photons. Here, we demonstrate such an efficient and optically programmable interface between the spin of an electron in a quantum dot and photons in a nanophotonic waveguide. The spin can be deterministically prepared in the ground state with a fidelity of up to 96%. Subsequently, the system is used to implement a single-spin photonic switch, in which the spin state of the electron directs the flow of photons through the waveguide. The spin-photon interface may enable on-chip photon-photon gates, single-photon transistors and the efficient generation of a photonic cluster state.