Back

Step by Step Guide to Building a ServiceTitan API Integration in C#

Aug 15, 20246 minute read

Introduction

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.

Prerequisites

Before we jump in, make sure you've got:

  • Visual Studio or your favorite C# IDE
  • .NET Core 3.1 or later
  • ServiceTitan API credentials (if you don't have these, reach out to the ServiceTitan team)

Got all that? Great! Let's get our hands dirty.

Setting up the project

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.

Authentication

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!

Making API requests

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.

Core integration features

Now for the fun part! Let's look at some key features:

Fetching customer data

var customers = await MakeApiCall("/customer/v2/customers");

Creating a job

var jobData = new { customerId = 123, jobTypeId = 456, startDate = DateTime.Now }; var newJob = await MakeApiCall("/jpm/v2/jobs", Method.POST, jobData);

Managing technician schedules

var techSchedule = await MakeApiCall("/dispatch/v2/technicians/123/schedule");

Data synchronization

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(); }

Best practices

  • Cache data where possible to reduce API calls
  • Use asynchronous methods for better performance
  • Implement proper error handling and logging
  • Secure your API credentials and access tokens

Testing and debugging

Unit test your API calls:

[Fact] public async Task TestGetCustomers() { var result = await apiClient.GetCustomers(); Assert.NotNull(result); Assert.True(result.Count > 0); }

Deployment and maintenance

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.

Conclusion

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!