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 software. In this guide, we'll walk through building a robust integration that'll have you manipulating customer data, managing jobs, and handling invoices like a pro.
Before we jump in, make sure you've got:
Got all that? Great! Let's get our hands dirty.
First things first, fire up Visual Studio and create a new C# project. We'll be using a console application for this guide, but feel free to adapt it to your needs.
Now, let's grab some NuGet packages:
Install-Package Newtonsoft.Json
Install-Package RestSharp
These will make our lives easier when dealing with JSON and HTTP requests.
ServiceTitan uses OAuth 2.0, so we need to implement that flow. Here's a quick snippet to get you started:
public async Task<string> GetAccessToken() { var client = new RestClient("https://auth.servicetitan.io/connect/token"); var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddParameter("grant_type", "client_credentials"); request.AddParameter("client_id", "YOUR_CLIENT_ID"); request.AddParameter("client_secret", "YOUR_CLIENT_SECRET"); var response = await client.ExecuteAsync(request); var token = JsonConvert.DeserializeObject<TokenResponse>(response.Content); return token.AccessToken; }
Remember to store and refresh your access token as needed. You don't want to hit the API for a new token on every request!
Now that we're authenticated, let's make some API calls. Here's a basic structure:
public async Task<string> MakeApiCall(string endpoint) { var client = new RestClient("https://api.servicetitan.io/v2"); var request = new RestRequest(endpoint, Method.GET); request.AddHeader("Authorization", $"Bearer {accessToken}"); var response = await client.ExecuteAsync(request); return response.Content; }
Don't forget to handle rate limiting and errors. The API will return appropriate status codes, so make sure you're checking those.
Now for the fun part! Let's look at some key features:
var customers = await MakeApiCall("/customer/v2/customers");
var jobData = new { customerId = 123, jobTypeId = 456, startDate = DateTime.Now }; var newJob = await MakeApiCall("/jpm/v2/jobs", Method.POST, jobData);
var techSchedule = await MakeApiCall("/dispatch/v2/technicians/123/schedule");
To keep your data up-to-date, implement webhooks. ServiceTitan will send real-time updates to your specified endpoint. Here's a basic webhook handler:
[HttpPost] public IActionResult WebhookHandler([FromBody] WebhookPayload payload) { // Process the webhook payload return Ok(); }
Unit test your API calls:
[Fact] public async Task TestGetCustomers() { var result = await apiClient.GetCustomers(); Assert.NotNull(result); Assert.True(result.Count > 0); }
When you're ready to deploy, consider using Azure Functions or AWS Lambda for a serverless approach. Keep an eye on your API usage and set up monitoring to catch any issues early.
And there you have it! You're now equipped to build a robust ServiceTitan API integration. Remember, the API is constantly evolving, so keep an eye on the documentation for updates. Happy coding, and may your integration be ever efficient!