Background Ambient air pollution has been associated with gestational diabetes (GD), but critical windows of exposure and whether maternal pre-existing conditions and other environmental factors modify the associations remains inconclusive. Methods We conducted a retrospective cohort study of all singleton live birth that occurred between April 1st 2006 and March 31st 2018 in Ontario, Canada. Ambient air pollution data (i.e., fine particulate matter with a diameter ≤ 2.5μm (PM2.5), nitrogen dioxide (NO2) and ozone (O3)) were assigned to the study population in spatial resolution of approximately 1 km x 1 km. The Normalized Difference Vegetation Index (NDVI) and the Green View Index (GVI) were also used to characterize residential exposure to green space as well as the Active Living Environments (ALE) index to represent the active living friendliness. Multivariable Cox proportional hazards regression models were used to evaluate the associations. Results Among 1,310,807 pregnant individuals, 68,860 incident cases of GD were identified. We found the strongest associations between PM2.5 and GD in gestational weeks 7 to 18 (HR = 1.07 per IQR (2.7 µg/m3); 95% CI: 1.02 – 1.11)). For O3, we found two sensitive windows of exposure, with increased risk in the preconception period (HR = 1.03 per IQR increase (7.0 ppb) (95% CI: 1.01 – 1.06)) as well as gestational weeks 9 to 28 (HR 1.08 per IQR (95% CI: 1.04 –1.12)). We found that women with asthma were more at risk of GD when exposed to increasing levels of O3 (p- value for effect modification = 0.04). Exposure to air pollutants explained 20.1%, 1.4% and 4.6% of the associations between GVI, NDVI and ALE, respectively. Conclusion An increase of PM2.5 exposure in early pregnancy and of O3 exposure during late first trimester and over the second trimester of pregnancy were associated with gestational diabetes whereas exposure to green space may confer a protective effect.