Medicare
HealthExploring the Medicare API with JavaScript
The Medicare API provides access to a wealth of healthcare data from the Centers for Medicare & Medicaid Services (CMS). In this blog post, we'll explore how to use the API with JavaScript, using examples to showcase different features and endpoints.
Getting Started
Before we can start using the Medicare API, we need to obtain an API key. To do this, we'll need to create an account on the Medicare Developer Portal (https://data.medicare.gov/developers). Once we have an API key, we can use that to authenticate our requests.
Example 1: Retrieving Facility Data
In this first example, we'll retrieve data for all long-term care hospitals (LTCHs) in the United States. We'll use the /Facility
endpoint to get this data, and we'll filter the results to include only LTCHs.
const API_KEY = "YOUR_API_KEY_HERE";
fetch(
"https://data.medicare.gov/resource/xubh-q36u.json?provider_type=Long%20Term%20Care%20Hospital",
{
headers: {
"X-API-KEY": API_KEY,
},
}
)
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
In this example, we're making a fetch
request to the /Facility
endpoint, specifying a filter for provider_type
to only include LTCHs. We pass our API key in the request headers to authenticate the request. Once we receive the response, we log the data to the console.
Example 2: Retrieving Procedure Data
In this second example, we'll retrieve data for a specific procedure from the Inpatient Prospective Payment System (IPPS)
dataset. We'll use the /Procedure
endpoint to get this data.
const API_KEY = "YOUR_API_KEY_HERE";
fetch(
"https://data.medicare.gov/resource/4pq5-n9py.json?drg_definition=470%20MAJOR%20JOINT%20REPLACEMENT%20OR%20REATTACHMENT%20OF%20LOWER%20EXTREMITY%20W/O%20MAJOR%20CC/MCC",
{
headers: {
"X-API-KEY": API_KEY,
},
}
)
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
In this example, we're making a fetch
request to the /Procedure
endpoint, specifying a filter for drg_definition
to only include data for the 470 MAJOR JOINT REPLACEMENT OR REATTACHMENT OF LOWER EXTREMITY W/O MAJOR CC/MCC
procedure. Once we receive the response, we log the data to the console.
Example 3: Retrieving Provider Data
In this third example, we'll retrieve data for all providers in a specific state. We'll use the /Provider
endpoint to get this data, and we'll filter the results to include only providers in the state of California.
const API_KEY = "YOUR_API_KEY_HERE";
fetch(
"https://data.medicare.gov/resource/ukfj-tt6v.json?state=CA",
{
headers: {
"X-API-KEY": API_KEY,
},
}
)
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
In this example, we're making a fetch
request to the /Provider
endpoint, specifying a filter for state
to only include data for California. Once we receive the response, we log the data to the console.
Conclusion
The Medicare API provides a rich source of healthcare data for developers to explore and utilize in their applications. With JavaScript, we can make requests to different endpoints and retrieve data that is specific to our needs. These examples only scratch the surface of what's possible with the Medicare API, and there are many more endpoints and filters to explore.