Hey there, fellow developer! Ready to dive into the world of ServiceTitan API integration? You're in for a treat. ServiceTitan's API is a powerful tool that can supercharge your field service management workflows. In this guide, we'll walk through building a robust integration that'll have you managing customers, scheduling jobs, and crunching numbers like a pro.
Before we jump in, let's make sure you've got your ducks in a row:
requests
, json
Got all that? Great! Let's roll up our sleeves and get coding.
First things first: we need to get that golden ticket – the access token. Here's how:
import requests def get_access_token(client_id, client_secret): url = "https://auth.servicetitan.io/connect/token" payload = { "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret, "scope": "all" } response = requests.post(url, data=payload) return response.json()["access_token"]
Pro tip: These tokens expire, so implement a refresh mechanism to keep your integration humming along smoothly.
Now that we're authenticated, let's start making some requests:
def make_api_request(endpoint, method="GET", data=None): headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } url = f"https://api.servicetitan.io/v2/{endpoint}" response = requests.request(method, url, headers=headers, json=data) response.raise_for_status() return response.json()
This function is your Swiss Army knife for API requests. Use it wisely!
The API might throw you a curveball now and then. Be ready:
from requests.exceptions import RequestException try: response = make_api_request("customers") except RequestException as e: print(f"Oops! Something went wrong: {e}")
Once you've got your data, it's time to make it sing:
def process_customers(customer_data): return [ { "id": customer["id"], "name": customer["name"], "phone": customer["phoneNumber"] } for customer in customer_data["data"] ]
Let's put it all together with some real-world examples:
def create_customer(name, phone): data = {"name": name, "phoneNumber": phone} return make_api_request("customers", method="POST", data=data)
def schedule_job(customer_id, service_type, start_time): data = { "customerId": customer_id, "serviceType": service_type, "startDateTime": start_time } return make_api_request("jobs", method="POST", data=data)
Remember, the API has rate limits. Be a good citizen:
import time def rate_limited_request(endpoint): response = make_api_request(endpoint) time.sleep(1) # Be nice to the API return response
Always test your code. Your future self will thank you:
import unittest class TestServiceTitanIntegration(unittest.TestCase): def test_customer_creation(self): customer = create_customer("John Doe", "1234567890") self.assertIsNotNone(customer["id"]) if __name__ == "__main__": unittest.main()
Consider containerizing your integration with Docker for easy deployment and scaling. And don't forget to keep an eye on those API changes!
And there you have it! You've just built a solid foundation for your ServiceTitan API integration. Remember, this is just the beginning. The API has tons more to offer, so keep exploring and building awesome things!
Happy coding, and may your services always be titan-ic! 🚀