Aim To assess the role of momentary pain on opioid craving and illicit opioid use among individuals receiving opioid agonist treatment. Design Observational study using ecological momentary assessment. Setting The National Institute of Drug Abuse Intramural Research Program in the United States. Participants Fifty-six adults who qualified for opioid agonist treatment. Measurements Participants completed randomly prompted assessments of pain severity, stress, negative mood, opioid craving and illicit opioid use for a mean of 66 days [standard deviation (SD) = 27]. Urine samples were collected two to three times/week throughout. Findings Almost 70% of participants reported moderate average pain severity in the past 24 hours at intake and 35% of participants reported chronic pain. There were no significant differences in percent of opioid-positive urine samples (P = 0.73) and average level of opioid craving during the study period (P = 0.91) among opioid agonist treatment only patients versus opioid agonist treatment patients with chronic pain. However, momentary pain severity significantly predicted concurrent opioid craving [B = 0.02, 95% confidence interval (CI) = 0.01, 0.04], over and above stress and negative mood. Momentary opioid craving, in turn, significantly predicted illicit opioid use that was assessed in the next moment [odds ratio (OR) = 1.72, 95% CI = 1.12, 2.64), while controlling for autocorrelation and the effects of pain, negative mood and stress. Momentary opioid craving significantly mediated the prospective association between momentary pain and illicit opioid use (95% CI = 0.003, 0.032). Exploratory analysis revealed that momentary pain severity also significantly moderated the momentary association between stress and opioid craving (B = 0.02, 95% CI = 0.00, 0.04), such that when momentary pain severity increased, the association between the two intensified. Conclusions Among people receiving opioid agonist treatment, momentary pain appears to be indirectly associated with illicit opioid use via momentary opioid craving.