High plasma 25-hydroxyvitamin D [25(OH)D] concentration has been associated observationally with a high risk of nonmelanoma skin cancer (NMSC), whereas many studies suggest that vitamin D could have a protective effect against cancer. The true association between vitamin D and risk of skin cancer remains unclear. To test the hypothesis that genetically high plasma 25(OH)D protects against NMSC. We included 103 084 individuals from the Danish general population, of whom 35 298 had plasma 25(OH)D measured and 97 849 were genotyped for four genetic variants near DHCR7 and CYP2R1 associated with 25(OH)D concentrations. We tested the association between plasma 25(OH)D levels and NMSC observationally and between genetically determined 25(OH)D levels and NMSC, using an instrumental variable approach. Multivariate-adjusted hazard ratios of NMSC were 3·27 [95% confidence interval (CI) 2·22-4·84] for plasma 25(OH)D ≥ 50 nmol L-1 vs. < 25 nmol L-1 . Genetic variants around DHCR7 and CYP2R1 were associated with up to 8·2 nmol L-1 higher 25(OH)D concentrations (F = 314). The odds ratio (OR) for a genetically determined 20 nmol L-1 higher plasma 25(OH)D was 1·11 (95% CI 0·91-1·35) for NMSC, with a corresponding observational multivariable adjusted OR of 1·13 (95% CI 1·10-1·17). Genetically determined high 25(OH)D levels did not appear to protect against NMSC, whereas high plasma 25(OH)D concentrations were associated with an observational high risk of NMSC. Thus, the observational association likely reflects confounding by sun exposure rather than causality. © 2017 British Association of Dermatologists.