Hey there, fellow developer! Ready to dive into the world of ServiceTitan API integration? You're in for a treat. We'll be walking through the process of building a robust integration in Java, giving you the power to tap into ServiceTitan's vast ecosystem of field service management tools. Let's get cracking!
Before we jump in, make sure you've got these essentials:
First things first, let's get you authenticated:
public String getAccessToken() { // Implementation to obtain access token } public void refreshToken() { // Logic to refresh the token when it expires }
Pro tip: Implement a token refresh mechanism to keep your integration running smoothly without manual intervention.
Let's get our project structure in order:
src/
├── main/
│ └── java/
│ └── com/
│ └── yourcompany/
│ └── servicetitan/
│ ├── ApiClient.java
│ ├── CustomerService.java
│ ├── JobService.java
│ └── InvoiceService.java
└── test/
└── java/
└── com/
└── yourcompany/
└── servicetitan/
├── ApiClientTest.java
├── CustomerServiceTest.java
├── JobServiceTest.java
└── InvoiceServiceTest.java
Don't forget to add your dependencies to your pom.xml
or build.gradle
file!
Here's where the magic happens:
public class ApiClient { private static final String BASE_URL = "https://api.servicetitan.io/v2"; private final OkHttpClient client; public ApiClient(String accessToken) { this.client = new OkHttpClient.Builder() .addInterceptor(chain -> chain.proceed(chain.request().newBuilder() .addHeader("Authorization", "Bearer " + accessToken) .build())) .build(); } public String get(String endpoint) throws IOException { Request request = new Request.Builder() .url(BASE_URL + endpoint) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } // Implement post(), put(), delete() methods similarly }
Let's tackle some core operations:
public class CustomerService { private final ApiClient apiClient; public CustomerService(ApiClient apiClient) { this.apiClient = apiClient; } public String getCustomers() throws IOException { return apiClient.get("/customers"); } // Implement other customer-related operations } // Create similar classes for JobService and InvoiceService
Time to turn that JSON into Java objects:
public class Customer { private int id; private String name; // Other fields and getters/setters } // Use a JSON library like Jackson or Gson for parsing ObjectMapper mapper = new ObjectMapper(); Customer customer = mapper.readValue(jsonString, Customer.class);
Don't let those pesky limits slow you down:
public List<Customer> getAllCustomers() throws IOException { List<Customer> allCustomers = new ArrayList<>(); String nextPageToken = null; do { String response = apiClient.get("/customers" + (nextPageToken != null ? "?pageToken=" + nextPageToken : "")); JsonNode root = mapper.readTree(response); allCustomers.addAll(mapper.convertValue(root.get("data"), new TypeReference<List<Customer>>(){})); nextPageToken = root.get("nextPageToken").asText(); Thread.sleep(100); // Respect rate limits } while (nextPageToken != null); return allCustomers; }
Keep your integration robust with proper error handling:
try { String response = apiClient.get("/customers"); // Process response } catch (IOException e) { logger.error("Error fetching customers: " + e.getMessage()); // Handle the error appropriately }
Don't forget to test! Here's a quick example:
@Test public void testGetCustomers() throws IOException { CustomerService service = new CustomerService(new ApiClient("test-token")); String customers = service.getCustomers(); assertNotNull(customers); // Add more assertions as needed }
And there you have it! You've just built a solid foundation for your ServiceTitan API integration in Java. Remember, this is just the beginning. There's a whole world of possibilities to explore with the ServiceTitan API. Keep experimenting, keep building, and most importantly, keep coding!
Happy integrating, and may your code always compile on the first try! 🚀