Back

Step by Step Guide to Building a Housecall Pro API Integration in PHP

Aug 14, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Housecall Pro API integration? You're in for a treat. We'll be using the nifty compwright/oauth2-housecallpro package to make our lives easier. Let's get cracking!

Prerequisites

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

  • A PHP environment (you're a pro, so I'm sure you've got this covered)
  • Composer installed (because who doesn't love dependency management?)
  • A Housecall Pro developer account (if you don't have one, go grab it now!)

Installation

First things first, let's get that package installed. Fire up your terminal and run:

composer require compwright/oauth2-housecallpro

Easy peasy, right?

Configuration

Now, let's set things up:

  1. Head over to your Housecall Pro developer account and set up those OAuth 2.0 credentials.
  2. In your PHP project, configure the OAuth client like this:
use Compwright\OAuth2\Client\Provider\HousecallPro; $provider = new HousecallPro([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'redirectUri' => 'https://your-redirect-uri.com/callback' ]);

Authentication

Time to implement the authorization flow:

$authorizationUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authorizationUrl); exit;

And handle that callback like a champ:

if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Store this token somewhere safe!

Making API Requests

Now for the fun part - let's make some requests!

GET request:

$request = $provider->getAuthenticatedRequest( 'GET', 'https://api.housecallpro.com/v1/jobs', $token ); $response = $provider->getParsedResponse($request);

POST request:

$request = $provider->getAuthenticatedRequest( 'POST', 'https://api.housecallpro.com/v1/jobs', $token, ['body' => json_encode($jobData)] ); $response = $provider->getParsedResponse($request);

Error Handling

Don't let those pesky errors catch you off guard:

try { $response = $provider->getParsedResponse($request); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Handle OAuth errors echo $e->getMessage(); } catch (\Exception $e) { // Handle other errors echo $e->getMessage(); }

And when that token expires:

if ($token->hasExpired()) { $newToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $token->getRefreshToken() ]); // Save the new token }

Best Practices

  • Keep an eye on those rate limits. Housecall Pro isn't shy about enforcing them.
  • Secure your integration like Fort Knox. Encrypt those tokens!

Conclusion

And there you have it! You're now ready to rock the Housecall Pro API like a pro. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do with this integration.

Happy coding, and may your API calls always return 200 OK! 🚀