Background: Before February, 2021, there was no standard treatment regimen for locally advanced basal cell carcinoma after first-line hedgehog inhibitor (HHI) therapy. Cemiplimab, a PD-1 antibody, is approved for treatment of advanced cutaneous squamous cell carcinoma and has shown clinical activity as monotherapy in first-line non-small-cell lung cancer. Here, we present the primary analysis data of cemiplimab in patients with locally advanced basal cell carcinoma after HHI therapy. Methods: We did an open-label, multicentre, single-arm, phase 2 trial across 38 outpatient clinics, primarily at academic medical centres, in Canada, Europe, and the USA. Eligible patients (aged ≥18 years and with an Eastern Cooperative Oncology Group performance status of 0 or 1) with a histologically confirmed diagnosis of metastatic basal cell carcinoma (group 1) or locally advanced basal cell carcinoma (group 2) who had progressed on or were intolerant to previous HHI therapy were enrolled. Patients were not candidates for further HHI therapy due to progression of disease on or intolerance to previous HHI therapy or having no better than stable disease after 9 months on HHI therapy. Patients received cemiplimab 350 mg intravenously every 3 weeks for up to 93 weeks or until progression or unacceptable toxicity. The primary endpoint was objective response by independent central review. Analyses were done as per the intention-to-treat principle. The safety analysis comprised all patients who received at least one dose of cemiplimab. The primary analysis is reported only for group 2; group 1 data have not reached maturity and will be reported when the timepoint, according to the statistical analysis plan, has been reached. This study is registered with ClinicalTrials.gov, NCT03132636, and is no longer recruiting new participants. Findings: Between Nov 16, 2017, and Jan 7, 2019, 84 patients were enrolled and treated with cemiplimab. At data cutoff on Feb 17, 2020, median duration of follow-up was 15 months (IQR 8–18). An objective response per independent central review was observed in 26 (31%; 95% CI 21–42) of 84 patients, including two partial responses that emerged at tumour assessments before the data cutoff and were confirmed by tumour assessments done subsequent to the data cutoff. The best overall response was five (6%) patients with a complete response and 21 (25%) with a partial response. Grade 3–4 treatment-emergent adverse events occurred in 40 (48%) of 84 patients; the most common were hypertension (four [5%] of 84 patients) and colitis (four [5%]). Serious treatment-emergent adverse events occurred in 29 (35%) of 84 patients. There were no treatment-related deaths. Interpretation: Cemiplimab exhibited clinically meaningful antitumour activity and an acceptable safety profile in patients with locally advanced basal cell carcinoma after HHI therapy. Funding: Regeneron Pharmaceuticals and Sanofi.