Lookup Multiple Providers
Return select fields from our Provider Lookup endpoint using a list of Type 1 NPIs.
Importing libraries
import requests
import pandas as pd
import json
import csv
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 results
Create functions that call the Provider Lookup endpoint and return provider data using your access token.
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)
Create a function that accepts a list of NPIs and returns selected fields from the JSON response.
def create_provider_dataframe(npis):
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 6 months ago
What’s Next