Lookup One or More Providers
First, return information from a single Provider, then return information from multiple Providers from our Provider Lookup endpoint using Type 1 NPIs.
Importing libraries
import requests
import pandas as pd
import json
Getting an access token
Create a function to call Auth0 to get your access token using your client ID and client Secret for your application. Remember that access tokens are only valid for an hour.
def get_access_token() -> str:
my_client_id = '<replace with your client_id>'
my_client_secret = '<replace with your client_secret>'
headers = {'content-type': 'application/json'}
url = 'https://auth.trillianthealth.com/oauth/token'
data = {
'client_id': my_client_id,
'client_secret': my_client_secret,
'audience': 'https://api.trillianthealth.com',
'grant_type': 'client_credentials'
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json().get('access_token')
Returning A Single Provider
Create a function to call the Provider Lookup endpoint and return provider data using your access token, then create one to pretty-print the results.
def get_api_result(npi: str) -> dict:
user_agent = ""
url = f"https://api.trillianthealth.com/v1/providers/{npi}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {get_access_token()}",
"user-agent": f"{user_agent}"
}
response = requests.get(url, headers=headers)
return response.json()
def pprint_result(result: dict) -> None:
from pprint import pprint
pprint(result, sort_dicts=False)
Finally, we define our NPI, call the endpoint, and pretty-print the response!
npi = "1922037787" #Replace with the NPI of your choice.
pprint_result(get_api_result(npi))
Returning Multiple Providers
Create a function that accepts a list of NPIs and uses the get_api_result
function we created earlier to return selected fields from the Provider JSON response.
def create_provider_dataframe(npis: list):
data = []
for npi in npis:
npi_data = get_api_result(npi)
row = {
'npi': npi_data['npi'],
'active': npi_data['active'],
'first_name': npi_data['name']['first'],
'organization': npi_data['affiliatedOrganizations']['items'][0]['name'],
'primary_specialty_description': npi_data['primarySpecialty']['description'],
'percent_male': npi_data['patientPanel']['gender']['percentMale'],
'percent_female': npi_data['patientPanel']['gender']['percentFemale']
}
data.append(row)
df = pd.DataFrame(data)
return df
Create a list of NPIs, and call the dataframe function to return results on multiple Providers.
npi_list = ["1922037787","1053314971"]
npi_dataframe = create_provider_dataframe(npi_list)
print(npi_dataframe)
Once you have created the dataframe, you can save it locally as a CSV. This command will create (or overwrite) a file in the same directory as your notebook.
output_csv_file = 'npi_data.csv'
npi_dataframe.to_csv(output_csv_file, index=False)
print(f"DataFrame written to {output_csv_file}")
Updated 7 months ago