Hey there, fellow developer! Ready to dive into the world of real estate data? Today, we're going to walk through building a Zillow Leads API integration in Python. This powerful tool will let you tap into Zillow's vast lead database, opening up a world of possibilities for your real estate tech projects. Let's get started!
Before we jump in, make sure you've got these basics covered:
requests
and json
(you can install them with pip
)Got all that? Great! Let's move on to the fun stuff.
First things first, we need to get you authenticated with Zillow. You'll need two key pieces:
Once you've got these, set up your authentication headers like this:
headers = { 'X-Zillow-API-Key': 'YOUR_API_KEY', 'X-Zillow-ZWSID': 'YOUR_ZWSID' }
Now, let's look at the basic structure of a Zillow API request:
import requests base_url = 'https://api.zillow.com/leads/v1/' endpoint = 'leads' # This will change based on what you're doing response = requests.get(f'{base_url}{endpoint}', headers=headers, params=params)
Pretty straightforward, right? The endpoint
and params
will vary depending on what you're trying to do.
Let's start with something simple - getting info about a specific lead:
lead_id = '12345' endpoint = f'leads/{lead_id}' response = requests.get(f'{base_url}{endpoint}', headers=headers) lead_data = response.json()
Now, let's create a new lead:
new_lead_data = { 'name': 'John Doe', 'email': '[email protected]', 'phone': '555-1234' } response = requests.post(f'{base_url}leads', headers=headers, json=new_lead_data)
Keeping your leads up-to-date is crucial. Here's how to update a lead's status:
lead_id = '12345' update_data = {'status': 'contacted'} response = requests.patch(f'{base_url}leads/{lead_id}', headers=headers, json=update_data)
Need to find specific leads? Here's how to search:
search_params = { 'status': 'new', 'created_after': '2023-01-01' } response = requests.get(f'{base_url}leads', headers=headers, params=search_params) search_results = response.json()
Always remember to handle those responses! Here's a quick example:
if response.status_code == 200: data = response.json() # Process your data here else: print(f"Error: {response.status_code}") print(response.text)
Zillow has rate limits, so be nice to their servers! Here's a simple way to implement rate limiting:
import time def rate_limited_request(url, headers, params=None): response = requests.get(url, headers=headers, params=params) if response.status_code == 429: # Too Many Requests retry_after = int(response.headers.get('Retry-After', 60)) time.sleep(retry_after) return rate_limited_request(url, headers, params) return response
Let's put it all together with a simple lead management system:
def get_new_leads(): response = rate_limited_request(f'{base_url}leads', headers=headers, params={'status': 'new'}) return response.json() def update_lead_status(lead_id, new_status): update_data = {'status': new_status} response = requests.patch(f'{base_url}leads/{lead_id}', headers=headers, json=update_data) return response.status_code == 200 new_leads = get_new_leads() for lead in new_leads: # Process the lead... if update_lead_status(lead['id'], 'contacted'): print(f"Updated lead {lead['id']} to 'contacted'")
Always test your API calls! Here's a quick unit test example:
import unittest class TestZillowAPI(unittest.TestCase): def test_get_lead(self): lead_id = '12345' response = requests.get(f'{base_url}leads/{lead_id}', headers=headers) self.assertEqual(response.status_code, 200) lead_data = response.json() self.assertIn('id', lead_data) if __name__ == '__main__': unittest.main()
And there you have it! You've just built a Zillow Leads API integration in Python. From authentication to handling responses, you're now equipped to tap into Zillow's powerful lead database. Remember, this is just the beginning - there's so much more you can do with this API.
Want to dive deeper? Check out these resources:
Happy coding, and may your leads be plentiful!