Hey there, fellow developer! Ready to dive into the world of Thryv API integration? You're in for a treat. Thryv's API is a powerful tool that'll let you tap into a wealth of business management features. Whether you're looking to streamline appointments, manage customer data, or pull business info, we've got you covered. Let's get our hands dirty and build something awesome!
Before we jump in, make sure you've got these basics squared away:
requests
library (pip install requests
)First things first, let's get you authenticated:
import requests API_KEY = 'your_api_key_here' API_SECRET = 'your_api_secret_here' headers = { 'X-API-KEY': API_KEY, 'X-API-SECRET': API_SECRET, 'Content-Type': 'application/json' }
Easy peasy, right? Just plug in your credentials, and you're good to go!
Here's the bread and butter of your Thryv API requests:
BASE_URL = 'https://api.thryv.com/v1' def make_request(endpoint, method='GET', data=None): url = f"{BASE_URL}/{endpoint}" response = requests.request(method, url, headers=headers, json=data) response.raise_for_status() return response.json()
This handy function will be your go-to for all API calls. It's like your Swiss Army knife for Thryv!
Now, let's put that function to work:
# GET request businesses = make_request('businesses') # POST request new_appointment = make_request('appointments', method='POST', data={ 'businessId': '123456', 'startDateTime': '2023-06-01T09:00:00Z', 'endDateTime': '2023-06-01T10:00:00Z', 'customerId': '789012' })
See how easy that is? You're already pulling data and creating appointments like a pro!
Thryv's got a ton of data at your fingertips. Here's how to grab what you need:
# Get business info business_info = make_request(f'businesses/{business_id}') # Get appointments appointments = make_request(f'businesses/{business_id}/appointments') # Get customers customers = make_request(f'businesses/{business_id}/customers')
Let's face it, things don't always go smoothly. Here's how to handle those bumps in the road:
import time from requests.exceptions import RequestException def make_request_with_retry(endpoint, method='GET', data=None, max_retries=3): for attempt in range(max_retries): try: return make_request(endpoint, method, data) except RequestException as e: if attempt == max_retries - 1: raise time.sleep(2 ** attempt) # Exponential backoff
This little nugget will automatically retry failed requests and help you stay within rate limits. Your future self will thank you!
Got your data? Great! Now let's do something with it:
import json import sqlite3 def save_to_db(data, table_name): conn = sqlite3.connect('thryv_data.db') cursor = conn.cursor() placeholders = ', '.join(['?'] * len(data)) columns = ', '.join(data.keys()) sql = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})" cursor.execute(sql, tuple(data.values())) conn.commit() conn.close() # Usage business_data = make_request(f'businesses/{business_id}') save_to_db(business_data, 'businesses')
Boom! You're now storing Thryv data like a boss.
Let's put it all together with a quick appointment scheduler:
def schedule_appointment(business_id, customer_id, start_time, end_time): appointment_data = { 'businessId': business_id, 'customerId': customer_id, 'startDateTime': start_time, 'endDateTime': end_time } response = make_request_with_retry('appointments', method='POST', data=appointment_data) print(f"Appointment scheduled: {response['id']}") # Usage schedule_appointment('123456', '789012', '2023-06-01T09:00:00Z', '2023-06-01T10:00:00Z')
And just like that, you've got a working appointment scheduler!
Want to take your integration to the next level? Here are some pro tips:
There you have it, folks! You've just built a solid Thryv API integration in Python. You've got the tools to authenticate, make requests, handle errors, process data, and even create a simple application. The sky's the limit from here!
Remember, the best way to learn is by doing. So go ahead, experiment with different endpoints, build more complex applications, and make this integration your own. Happy coding!