Skip to main content

PHP

composer require hometown/estimator

It needs PHP 8.2 or newer with ext-curl and ext-json.

Make an estimate​

use Hometown\Estimator\Hometown;

$hometown = new Hometown(getenv('HOMETOWN_API_KEY'));

$estimate = $hometown->estimates->create(['input' => ['text' => 'sandbox:priced']]);
echo $estimate['status'];

Objects are arrays with the keys of the API. Their shapes live in Hometown\Estimator\Types for PHPStan, Psalm and your editor. Every call that takes your key is a method on a resource: $hometown->estimates->retrieve($id), $hometown->customers->list(limit: 20), $hometown->webhookEndpoints->create([...]). The last argument takes timeout. On estimates->create, estimates->followUp and customers->create it also takes your own idempotency_key.

Catch errors​

use Hometown\Estimator\Exception\HometownException;
use Hometown\Estimator\Exception\InsufficientCreditsException;

try {
$hometown->estimates->create(['input' => ['text' => 'Paint a 12 by 14 ft bedroom']]);
} catch (InsufficientCreditsException) {
// buy credits in the dashboard
} catch (HometownException $error) {
error_log("{$error->errorCode} {$error->requestId}");
}

The class names follow the codes: rate_limited is RateLimitedException, validation_failed is ValidationFailedException. A request that never gets an answer throws ConnectionException.

Check a webhook​

Pass the raw body:

use Hometown\Estimator\Webhook;

$event = Webhook::verify(file_get_contents('php://input'), $_SERVER['HTTP_HOMETOWN_SIGNATURE'] ?? null, $secret);

It throws WebhookSignatureException when no signature matches or the delivery's timestamp is more than five minutes off.