The current opioid epidemic warrants a better understanding of genetic and environmental factors that contribute to opioid addiction. Here we report an increased prevalence of vitamin D (VitD) deficiency in patients diagnosed with opioid use disorder and an inverse and dose-dependent association of VitD levels with self-reported opioid use. We used multiple pharmacologic approaches and genetic mouse models and found that deficiencies in VitD signaling amplify exogenous opioid responses that are normalized upon restoration of VitD signaling. Similarly, physiologic endogenous opioid analgesia and reward responses triggered by ultraviolet (UV) radiation are repressed by VitD signaling, suggesting that a feedback loop exists whereby VitD deficiency produces increased UV/endorphin-seeking behavior until VitD levels are restored by cutaneous VitD synthesis. This feedback may carry the evolutionary advantage of maximizing VitD synthesis. However, unlike UV exposure, exogenous opioid use is not followed by VitD synthesis (and its opioid suppressive effects), contributing to maladaptive addictive behavior.