Hey there, fellow code wrangler! Ready to dive into the world of ServiceTitan API integration? You're in for a treat. ServiceTitan's API is a powerful tool that'll let you tap into their robust field service management platform. Whether you're looking to sync data, automate processes, or create custom solutions, this guide will get you up and running in no time.
Before we jump in, make sure you've got:
First things first, let's get you authenticated:
$client = new GuzzleHttp\Client(); $response = $client->post('https://auth.servicetitan.io/connect/token', [ 'form_params' => [ 'grant_type' => 'client_credentials', 'client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_CLIENT_SECRET', ] ]); $token = json_decode($response->getBody(), true)['access_token'];
Pro tip: Implement a token refresh mechanism. Your future self will thank you.
Now that you're authenticated, let's structure those requests:
$client->request('GET', 'https://api.servicetitan.io/v2/endpoint', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ] ]);
Time to get your hands dirty with some CRUD operations:
$response = $client->get('https://api.servicetitan.io/v2/customers/1234'); $customer = json_decode($response->getBody(), true);
$response = $client->post('https://api.servicetitan.io/v2/jobs', [ 'json' => [ 'customerId' => 1234, 'jobType' => 'Service', // ... other job details ] ]);
You get the idea. Rinse and repeat for PUT and DELETE.
Always expect the unexpected:
try { $response = $client->get('https://api.servicetitan.io/v2/customers/1234'); $data = json_decode($response->getBody(), true); } catch (GuzzleHttp\Exception\ClientException $e) { $errorResponse = $e->getResponse(); $errorData = json_decode($errorResponse->getBody(), true); // Handle the error like a boss }
Don't let large datasets bog you down:
$response = $client->get('https://api.servicetitan.io/v2/customers', [ 'query' => [ 'page' => 1, 'pageSize' => 100, 'filter' => 'createdOn ge 2023-01-01' ] ]);
If you're setting up webhooks, remember:
Quick and dirty example:
$payload = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_SERVICETITAN_SIGNATURE']; if (verifySignature($payload, $signature)) { $data = json_decode($payload, true); // Do something awesome with the data }
Play nice with the API:
Use the sandbox environment to test your integration. It's like a playground, but for code!
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.servicetitan-test.io']);
Let's say you want to create a job when a new customer is added to your CRM:
function createServiceTitanJob($customerData) { global $client, $token; // First, create or find the customer $customerResponse = $client->post('https://api.servicetitan.io/v2/customers', [ 'headers' => ['Authorization' => 'Bearer ' . $token], 'json' => [ 'name' => $customerData['name'], 'phoneNumber' => $customerData['phone'], // ... other customer details ] ]); $customer = json_decode($customerResponse->getBody(), true); // Now, create a job for this customer $jobResponse = $client->post('https://api.servicetitan.io/v2/jobs', [ 'headers' => ['Authorization' => 'Bearer ' . $token], 'json' => [ 'customerId' => $customer['id'], 'jobType' => 'Service', 'summary' => 'New customer onboarding', // ... other job details ] ]); return json_decode($jobResponse->getBody(), true); }
And there you have it! You're now armed with the knowledge to build a robust ServiceTitan API integration. Remember, the API documentation is your best friend, so keep it bookmarked. Happy coding, and may your integrations be ever smooth and your callbacks always resolve!