Back

How to build a public ServiceTitan integration: Building the Auth Flow

Aug 15, 20246 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of ServiceTitan integrations? Let's roll up our sleeves and build an authorization flow that'll make your integration shine. We'll keep things concise and focused, so you can get up and running in no time.

Introduction

ServiceTitan's API is a powerhouse for field service management, and building a public integration opens up a world of possibilities. But before we can tap into that potential, we need to nail the authorization flow. It's the gatekeeper of your integration, so let's make sure we get it right!

Prerequisites

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

  • A ServiceTitan developer account (if you don't have one, go grab it!)
  • A solid grasp on OAuth 2.0 (we'll be using it extensively)
  • Node.js and Express.js set up and ready to go

Got all that? Great! Let's move on to the good stuff.

ServiceTitan OAuth 2.0 Flow Overview

ServiceTitan uses the Authorization Code Grant type for OAuth 2.0. Here's the quick and dirty:

  1. Your app redirects the user to ServiceTitan's authorization page
  2. User grants permission
  3. ServiceTitan redirects back to your app with an authorization code
  4. Your app exchanges this code for an access token

Simple, right? Let's make it happen!

Setting Up Your Application

First things first:

  1. Head over to the ServiceTitan Developer Portal
  2. Register your app (give it a cool name!)
  3. Grab your client ID and secret (keep these safe!)

Implementing the Authorization Flow

Now for the fun part! Let's break it down:

Creating the authorization request

const authorizationUrl = `https://auth.servicetitan.io/connect/authorize? client_id=${clientId} &redirect_uri=${encodeURIComponent(redirectUri)} &response_type=code &scope=${encodeURIComponent(scope)} &state=${state}`; res.redirect(authorizationUrl);

Handling the callback and token exchange

app.get('/callback', async (req, res) => { const { code, state } = req.query; // Verify state to prevent CSRF attacks if (state !== expectedState) { return res.status(400).send('Invalid state parameter'); } try { const tokenResponse = await exchangeCodeForToken(code); // Store the tokens securely storeTokens(tokenResponse); res.send('Authorization successful!'); } catch (error) { res.status(500).send('Error during token exchange'); } });

Storing and refreshing access tokens

function storeTokens(tokenResponse) { // Store tokens securely (e.g., encrypted in a database) } async function refreshAccessToken(refreshToken) { // Implement token refresh logic here }

Error Handling and Edge Cases

Don't forget to handle those pesky errors:

  • Invalid state parameter (we covered this above)
  • Token expiration (use the refresh token to get a new access token)
  • Network issues (retry with exponential backoff)

Security Considerations

Security is paramount, so remember:

  • Always use HTTPS
  • Store tokens securely (encrypt them!)
  • Limit your scopes to only what you need

Testing Your Integration

Before you go live:

  1. Use ServiceTitan's sandbox environment
  2. Test all possible flows (happy path and error scenarios)
  3. Double-check your error handling

Conclusion

And there you have it! You've just built a rock-solid authorization flow for your ServiceTitan integration. Pat yourself on the back – you've taken a big step towards creating something awesome.

Remember, this is just the beginning. With your auth flow in place, you're now ready to start making API calls and building out the core functionality of your integration. The sky's the limit!

Keep coding, keep learning, and most importantly, have fun with it. You've got this! 🚀