Last active
May 29, 2025 21:19
-
-
Save benjamin-chan/e5a49c2cfd2e2752a20282e7110d3d59 to your computer and use it in GitHub Desktop.
R function to query CMS data via API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getCMS <- function(datasetID, | |
columns = "*", | |
where = NULL, | |
limit = 0, | |
root = "https://data.cms.gov/provider-data/api/1/datastore/sql") | |
{ | |
require(magrittr) | |
require(httr) | |
require(jsonlite) | |
url <- sprintf("https://data.cms.gov/provider-data/api/1/metastore/schemas/dataset/items/%s?show-reference-ids", datasetID) | |
temp <- GET(url)$content %>% rawToChar() %>% fromJSON() | |
distributionId <- temp[["distribution"]][["identifier"]] | |
if (is.null(where)) { | |
query <- sprintf("[SELECT %s FROM %s][LIMIT %d]", columns, distributionId, limit) | |
} else { | |
query <- sprintf("[SELECT %s FROM %s][WHERE %s][LIMIT %d]", columns, distributionId, where, limit) | |
} | |
options <- "show_db_columns=true" | |
url <- sprintf("%s?query=%s&%s", root, query, options) %>% URLencode() | |
GET(url)$content %>% rawToChar() %>% fromJSON() | |
} | |
# EXAMPLE | |
# getCMS("xubh-q36u", where = 'state = \"OR\"') # Hospital General Information | |
# getCMS("ynj2-r877", where = 'facility_id = \"380009\"') # Complications and Deaths - Hospital |
- Is it possible to automate grabbing
distributionId
? Currently, user needs to knowdistributionId
ahead of time, navigating to the data.cms.gov webpage for the data set. Would be nice if there was a catalog to look up.
https://data.cms.gov/provider-data/about#api-do-distribution-ids-change
To get list of datasetID
s
library(magrittr)
library(dplyr)
library(httr)
library(jsonlite)
url <- "https://data.cms.gov/provider-data/api/1/metastore/schemas/dataset/items"
GET(url)$content %>%
rawToChar() %>%
fromJSON() %>%
select(identifier, title, description) %>%
knitr::kable() %>%
cat(file = "datasetIDs.md", sep = "\n")
identifier | title |
---|---|
23ew-n7w9 |
Dialysis Facility - Listing by Facility |
2fpu-cgbb |
Dialysis Facility - State Averages |
2rkq-ygai |
Dialysis Facility - National Averages |
59mq-zhts |
Patient survey (ICH CAHPS) - Facility |
utgq-v46w |
Patient survey (ICH CAHPS) - National |
m5eg-upu5 |
Home Health Care - Zip Codes |
6jpm-sxkc |
Home Health Care Agencies |
97z8-de96 |
Home Health Care - National Data |
95rg-2usp |
Hospice - Zip Data |
yc9t-dgbk |
Hospice - General Information |
252m-zfp9 |
Hospice - Provider Data |
gxki-hrr8 |
Hospice care - Provider CAHPS Hospice Survey Data |
7cv8-v37d |
Hospice care - National CAHPS Hospice Survey Data |
48nr-hqxx |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey for ambulatory surgical centers - Facility |
4jcv-atw7 |
Ambulatory Surgical Center Quality Measures - Facility |
wue8-3vwe |
Ambulatory Surgical Center Quality Measures - National |
tf3h-mrrs |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey for ambulatory surgical centers - National |
axe7-s95e |
Ambulatory Surgical Center Quality Measures - State |
x663-bwbj |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey for ambulatory surgical centers - State |
tqkv-mgxq |
Comprehensive Care For Joint Replacement Model - Provider Data |
muwa-iene |
CMS Medicare PSI-90 and component measures - six-digit estimate dataset |
ynj2-r877 |
Complications and Deaths - Hospital |
qqw3-t4ie |
Complications and Deaths - National |
bs2r-24vh |
Complications and Deaths - State |
y9us-9xdf |
Footnote Crosswalk |
e6hj-umam |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey - Footnotes |
xrgf-x36b |
Table 2: FY2021 Distribution of Net Change in Base Operating DRG Payment Amount |
5gv4-jwyv |
Table 1: FY2021 Net Change in Base Operating DRG Payment Amount |
u625-zae7 |
Table 3: FY2021 Percent Change in Medicare Payments |
vtqa-m4zn |
Table 4: FY2021 Value-Based Incentive Payment Amount |
dgck-syfz |
Patient survey (HCAHPS) - Hospital |
99ue-w85f |
Patient survey (HCAHPS) - National |
84jm-wiui |
Patient survey (HCAHPS) - State |
77hc-ibv8 |
Healthcare Associated Infections - Hospital |
yd3s-jyhd |
Healthcare Associated Infections - National |
k2ze-bqvw |
Healthcare Associated Infections - State |
yizn-abxn |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey for hospital outpatient departments - Facility |
s5pj-hua3 |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey for hospital outpatient departments - National |
6pfg-whmx |
Outpatient and Ambulatory Surgery Consumer Assessment of Healthcare Providers and Systems (OAS CAHPS) survey for hospital outpatient departments - State |
xubh-q36u |
Hospital General Information |
z8ax-x9j1 |
Complications and Unplanned Hospital Visits - PPS-Exempt Cancer Hospital - Hospital |
jfnd-nl7s |
Complications and Unplanned Hospital Visits - PPS-Exempt Cancer Hospital - National |
yq43-i98g |
Hospital-Acquired Condition (HAC) Reduction Program |
5hk7-b79m |
Medicare Spending Per Beneficiary - Hospital Additional Decimal Places |
k653-4ka8 |
Safety and Healthcare-Associated Infection Measures - PPS-Exempt Cancer Hospital |
iy27-wz37 |
Patient Survey (PCH - HCAHPS) PPS-Exempt Cancer Hospital - Hospital |
9g7e-btyt |
Patient Survey (PCH - HCAHPS) PPS-Exempt Cancer Hospital - National |
qatj-nmws |
Patient Survey (PCH - HCAHPS) PPS-Exempt Cancer Hospital - State |
9n3s-kdb3 |
Hospital Readmissions Reduction Program |
pudb-wetr |
Hospital Value-Based Purchasing (HVBP) - Clinical Outcomes Domain Scores |
su9h-3pvj |
Hospital Value-Based Purchasing (HVBP) - Efficiency Scores |
avtz-f2ge |
Hospital Value-Based Purchasing (HVBP) - Person and Community Engagement Domain Scores (HCAHPS) |
dgmq-aat3 |
Hospital Value-Based Purchasing (HVBP) - Safety |
ypbt-wvdk |
Hospital Value-Based Purchasing (HVBP) - Total Performance Score |
q9vs-r7wp |
Inpatient Psychiatric Facility Quality Measure Data - by Facility |
s5xg-sys6 |
Inpatient Psychiatric Facility Quality Measure Data - National |
dc76-gh7x |
Inpatient Psychiatric Facility Quality Measure Data - by State |
4j6d-yzce |
Measure Dates |
nrth-mfg3 |
Medicare Hospital Spending by Claim |
rrqw-56er |
Medicare Spending Per Beneficiary - Hospital |
3n5g-6b7f |
Medicare Spending Per Beneficiary - National |
rs6n-9qwg |
Medicare Spending Per Beneficiary - State |
wkfw-kthe |
Outpatient Imaging Efficiency - Hospital |
di9i-zzrc |
Outpatient Imaging Efficiency - National |
if5v-4x48 |
Outpatient Imaging Efficiency - State |
ygty-mm5a |
Payment - National |
c7us-v4mf |
Payment and value of care - Hospital |
yv7e-xc69 |
Timely and Effective Care - Hospital |
isrn-hqyy |
Timely and Effective Care - National |
apyc-v239 |
Timely and Effective Care - State |
632h-zaca |
Unplanned Hospital Visits - Hospital |
cvcs-xecj |
Unplanned Hospital Visits - National |
4gkm-5ypv |
Unplanned Hospital Visits - State |
6qxe-iqz8 |
Veterans Health Administration Behavioral Health Data |
ptds-r8im |
Veterans Health Administration Timely and Effective Care Data |
gbq5-7hzr |
Value of care - National |
uyx4-5s7f |
Veterans Health Administration Provider Level Data |
bzsr-4my4 |
Data Updates |
7t8x-u3ir |
Inpatient Rehabilitation Facility - General Information |
v9e4-nwhh |
Inpatient Rehabilitation Facility - Provider Data |
ka5z-ibe3 |
Inpatient Rehabilitation Facility - Conditions |
azum-44iv |
Long-Term Care Hospital - General Information |
fp6g-2gsn |
Long-Term Care Hospital - Provider Data |
ifjz-ge4w |
Fire Safety Deficiencies |
r5ix-sfxw |
Health Deficiencies |
djen-97ju |
MDS Quality Measures |
ijh5-nb2v |
Medicare Claims Quality Measures |
y2hd-n93e |
Ownership |
g6vv-u9sr |
Penalties |
4pq5-n9py |
Provider Information |
fykj-qjee |
Skilled Nursing Facility Quality Reporting Program - Provider Data |
6uyb-waub |
Skilled Nursing Facility Quality Reporting Program - Swing Beds - Provider Data |
xcdc-v8bm |
State US Averages |
tbry-pc2d |
Survey Summary |
284v-j9fz |
FY 2025 SNF VBP Facility-Level Dataset |
ujcx-uaut |
FY 2025 SNF VBP Aggregate Performance |
qmdc-9999 |
Nursing Home Data Collection Intervals |
tagd-9999 |
Citation Code Look-up |
mj5m-pzi6 |
National Downloadable File |
0ba7-2cb0 |
PY 2022 Group Public Reporting: MIPS Measures and Attestations |
8c70-d353 |
PY 2022 Group Public Reporting: Patient Experience |
7d6a-e7a6 |
PY 2022 Clinician Public Reporting: MIPS Measures and Attestations |
a174-a962 |
PY 2022 Clinician Public Reporting: Overall MIPS Performance |
svdt-c123 |
Inspection Dates |
hicp-9999 |
State-Level Health Inspection Cut Points |
ccn4-8vby |
Home Health Care - Patient Survey (HHCAHPS) 2023Q4 to 2024Q3 |
vxub-6swi |
Home Health Care - Patient Survey (HHCAHPS) National Data 2023Q4 to 2024Q3 |
m5jg-jg7i |
Home Health Care - Patient Survey (HHCAHPS) State Data 2023Q4 to 2024Q3 |
fa88-6ff2 |
Home Health Care - Patient Survey (HHCAHPS) Measure Dates 2023Q4 to 2024Q3 |
3614-1eef |
Addiction Medicine Office Visit Costs |
bdd5-4a04 |
Advanced Heart Failure and Transplant Cardiology Office Visit Costs |
4269-8a74 |
Allergy/ Immunology Office Visit Costs |
2fbd-6172 |
Anesthesiology Office Visit Costs |
075a-d487 |
Cardiac Surgery Office Visit Costs |
3f64-129e |
Cardiology Office Visit Costs |
ea2d-9467 |
Certified Clinical Nurse Specialist Office Visit Costs |
9873-722a |
Certified Nurse Midwife Office Visit Costs |
3a83-ee2d |
Certified Registered Nurse Anesthetist (CRNA) Office Visit Costs |
98d4-0871 |
Clinic or Group Practice Office Visit Costs |
94f7-ab53 |
Clinical Cardiac Electrophysiology Office Visit Costs |
adba-1d45 |
Clinical Laboratory Office Visit Costs |
bd7d-078a |
Colorectal Surgery (Proctology) Office Visit Costs |
c8a8-342e |
Critical Care (Intensivists) Office Visit Costs |
32d8-3235 |
Dentist Office Visit Costs |
d796-7f06 |
Dermatology Office Visit Costs |
c14e-6492 |
Diagnostic Radiology Office Visit Costs |
6b8f-5372 |
Emergency Medicine Office Visit Costs |
b554-ef7e |
Endocrinology Office Visit Costs |
bb4c-dcdf |
Family Practice Office Visit Costs |
501b-ecd4 |
Gastroenterology Office Visit Costs |
2e55-8767 |
General Practice Office Visit Costs |
fbfb-4b94 |
General Surgery Office Visit Costs |
bce0-b5db |
Geriatric Medicine Office Visit Costs |
0f37-cab0 |
Geriatric Psychiatry Office Visit Costs |
e84d-d357 |
Gynecological Oncology Office Visit Costs |
c44d-bde6 |
Hand Surgery Office Visit Costs |
e1a1-c9b4 |
Hematology Office Visit Costs |
2483-f62b |
Hematology-Oncology Office Visit Costs |
d150-d141 |
Hematopoietic Cell Transplantation and Cellular Therapy Office Visit Costs |
0ddf-4325 |
Hospice and Palliative Care Office Visit Costs |
a0cb-cdab |
Hospitalist Office Visit Costs |
e2bb-d371 |
Infectious Disease Office Visit Costs |
9735-7176 |
Internal Medicine Office Visit Costs |
6787-a1bf |
Interventional Cardiology Office Visit Costs |
b06f-0828 |
Interventional Pain Management Office Visit Costs |
b9bf-6883 |
Interventional Radiology Office Visit Costs |
86e1-d1d1 |
Maxillofacial Surgery Office Visit Costs |
5d65-6dcf |
Medical Oncology Office Visit Costs |
9b4a-75d5 |
Medical Toxicology Office Visit Costs |
0938-3dfa |
Nephrology Office Visit Costs |
8c8a-b911 |
Neurology Office Visit Costs |
0b1a-da3f |
Neuropsychiatry Office Visit Costs |
f90c-2246 |
Neurosurgery Office Visit Costs |
d0ce-5cad |
Nuclear Medicine Office Visit Costs |
57e0-2991 |
Nurse Practitioner Office Visit Costs |
069d-826b |
Obstetrics & Gynecology Office Visit Costs |
5f44-cb84 |
Ophthalmology Office Visit Costs |
d640-e528 |
Optometry Office Visit Costs |
8c6b-fe40 |
Oral Surgery (Dentist only) Office Visit Costs |
c382-eab7 |
Orthopedic Surgery Office Visit Costs |
8753-38d1 |
Osteopathic Manipulative Medicine Office Visit Costs |
4a4e-e0da |
Otolaryngology Office Visit Costs |
cfa7-e909 |
Pain Management Office Visit Costs |
914a-7700 |
Pathology Office Visit Costs |
d226-80a4 |
Pediatric Medicine Office Visit Costs |
ccbb-cbfa |
Peripheral Vascular Disease Office Visit Costs |
5e3a-bee4 |
Physical Medicine and Rehabilitation Office Visit Costs |
029b-dd7e |
Physical Therapist in Private Practice Office Visit Costs |
0d7d-e988 |
Physician Assistant Office Visit Costs |
32b2-9f88 |
Plastic and Reconstructive Surgery Office Visit Costs |
8634-f6a7 |
Podiatry Office Visit Costs |
0330-b6e0 |
Preventive Medicine Office Visit Costs |
057a-5bcf |
Psychiatry Office Visit Costs |
92bb-de79 |
Psychologist, Clinical Office Visit Costs |
2ca5-1007 |
Public Health or Welfare Agency Office Visit Costs |
0127-af37 |
Pulmonary Disease Office Visit Costs |
288b-4bed |
Radiation Oncology Office Visit Costs |
8283-8a65 |
Registered Dietitian or Nutrition Professional Office Visit Costs |
385c-8c97 |
Rheumatology Office Visit Costs |
39da-b8ab |
Sleep Medicine Office Visit Costs |
c713-00e8 |
Sports Medicine Office Visit Costs |
0f6a-98c3 |
Surgical Oncology Office Visit Costs |
5d9c-1e86 |
Thoracic Surgery Office Visit Costs |
d1c9-d5b4 |
Undefined Physician type Office Visit Costs |
993f-4504 |
Undersea and Hyperbaric Medicine Office Visit Costs |
afcb-07d8 |
Urology Office Visit Costs |
f226-42b7 |
Vascular Surgery Office Visit Costs |
46dc-a66c |
PY 2022 Virtual Group Public Reporting: MIPS Measures and Attestations |
pvax-cv19 |
COVID-19 Vaccination Rates - Provider Data |
avax-cv19 |
COVID-19 Vaccination Rates - State and National Averages |
a55e-5b88 |
Hospice care - State CAHPS Hospice Survey Data |
eda0-92f0 |
Hospice - State Data |
ecb7-cb46 |
Licensed Clinical Social Worker Office Visit Costs |
b599-54c1 |
Medical Genetics and Genomics Office Visit Costs |
tee5-ixt5 |
Home Health Care - State by State Data |
c886-nwpj |
Home Health Care - Measure Date Range |
98ix-2iqy |
Payment - State |
nrdb-3fcy |
Maternal Health - Hospital |
ct36-nrcq |
Medical Equipment Suppliers |
nasn-k89k |
Inpatient Rehabilitation Facility - National Data |
5zdx-ny2x |
Long-Term Care Hospital - National Data |
5sqm-2qku |
Skilled Nursing Facility Quality Reporting Program - National Data |
hanv-ru8h |
Patient survey (ICH CAHPS) - State |
3xeb-u9wp |
Hospice-National Data |
27ea-46a8 |
Facility Affiliation Data |
n0yb-util |
Utilization Data |
clinical_depression |
ESRD QIP - Clinical Depression Screening and Follow-up - Payment Year 2025 |
complete_qip_data |
ESRD QIP - Complete QIP Data - Payment Year 2025 |
ktv_comprehensive |
ESRD QIP - Dialysis Adequacy - Payment Year 2025 |
footnotes |
ESRD QIP - Footnotes - Payment Year 2025 |
hypercalcemia |
ESRD QIP - Hypercalcemia - Payment Year 2025 |
nhsn_de |
ESRD QIP - NHSN Dialysis Event Measure - Payment Year 2025 |
nhsn_bsi |
ESRD QIP - NHSN Bloodstream Infection - Payment Year 2025 |
srr |
ESRD QIP - Standardized Readmission Ratio - Payment Year 2025 |
strr |
ESRD QIP - Standardized Transfusion Ratio - Payment Year 2025 |
tps |
ESRD QIP - Total Performance Scores - Payment Year 2025 |
vat_topic |
ESRD QIP - Vascular Access Topic - Payment Year 2025 |
ufr |
ESRD QIP - Ultrafiltration Rate - Payment Year 2025 |
shr |
ESRD QIP - Standardized Hospitalization Ratio - Payment Year 2025 |
medrec |
ESRD QIP - Medication Reconciliation - Payment Year 2025 |
pppw |
ESRD QIP - Percentage of Prevalent Patients Waitlisted - Payment Year 2025 |
qip_ich_cahps |
ESRD QIP - In-Center Hemodialysis Consumer Assessment Of Healthcare Providers And Services Systems (ICH CAHPS) Survey - Payment Year 2025 |
mxtu-43qs |
Patient-Reported Outcomes - Hospital |
qoeg-w7ck |
Palliative Care - PPS-Exempt Cancer Hospital - Hospital |
qigt-w5cx |
Palliative Care - PPS-Exempt Cancer Hospital - National |
e491-a466 |
Audiologist Office Visit Costs |
730a-fcd0 |
Mass Immunizer Roster Biller Office Visit Costs |
cd1d-5f84 |
Micrographic Dermatologic Surgery Office Visit Costs |
4533-9861 |
Opioid Treatment Program Office Visit Costs |
254e-694c |
Pharmacy Office Visit Costs |
v6ff-cmgx |
Health Equity - Hospital |
5ryi-3zdn |
Health Equity - State |
anfs-nlq7 |
Health Equity - National |
covid-19_hcp |
ESRD QIP - Covid-19 healthcare personnel Data - Payment Year 2025 |
56d7-4994 |
Expanded Home Health Value-Based Purchasing (HHVBP) Model - Agency Data |
4c74-462e |
Expanded Home Health Value-Based Purchasing (HHVBP) Model - Cohort Data |
f4ga-b9gx |
Promoting Interoperability - Hospital |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
distributionId
? Currently, user needs to knowdistributionId
ahead of time, navigating to the data.cms.gov webpage for the data set. Would be nice if there was a catalog to look up.