Search for Providers by Location
Search for multiple providers in a geographic area and return selected fields.
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 results
Because search results are paginated, you will need to call each page individually. Pages are limited to 10 provider results each. In this example, we will only call the first 10 pages. You could narrow search results by adding a filter by Specialty or searching by zip code only.
This function accepts the page number and address and returns the full search JSON response.
def get_api_search_result(page: int, address: str) -> dict:
user_agent = ""
url = f"https://api.trillianthealth.com/v1/providers?page={page}&address={address}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {get_access_token()}",
"user-agent": f"{user_agent}"
}
response = requests.get(url, headers=headers)
return response.json()
Try it out!
The address variable below accepts all or part of a street address as a URL-encoded string. In the code block below, we use "Chicago, IL", but you could also use the following:
- Search a zipcode by setting
address = "60618"
- Search for a specific street address, such as "2544 W Montrose Ave Chicago, IL" by setting
address = "2544%20W%20Montrose%20Ave%20Chicago%2C%20IL"
address = "Chicago%2C%20IL"
page = 1
get_api_search_result(address=address, page=page)
Create a function that accepts the full search response and pulls out selected fields for each provider. In this example, we want to view all the addresses a provider is affiliated with.
def parse_response(response):
items = response.get('items', [])
extracted_data = [{'npi': item.get('npi'),
'last_name': item.get('name', {}).get('last'),
'specialty': item.get('primarySpecialty',{}).get('description'),
'address': item.get('affiliatedAddresses',{}).get('items')} for item in items]
return pd.DataFrame(extracted_data)
Now, we're ready to return results! Here, we set the location we're interested in and loop through the first few pages of responses.
address = "Chicago%2C%20IL"
dataframes = []
for x in range(1,3):
response = get_api_search_result(address=address, page=x)
dataframes.append(parse_response(response))
df = pd.concat(dataframes,ignore_index=True)
df
Updated 6 months ago