TypeScript
npm install @hometown-workforce/estimator
It needs Node.js 20.19 or newer. Use it on your server: the key it takes is secret.
Make an estimate
import { Hometown } from '@hometown-workforce/estimator';
const hometown = new Hometown({ apiKey: process.env.HOMETOWN_API_KEY! });
const estimate = await hometown.estimates.create({ input: { text: 'sandbox:priced' } });
console.log(estimate.status, estimate.price);
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 of each takes an AbortSignal and a per-attempt timeout. On estimates.create, estimates.followUp and customers.create it also takes your own idempotencyKey.
Catch errors
import { HometownError, InsufficientCreditsError } from '@hometown-workforce/estimator';
try {
await hometown.estimates.create({ input: { text: 'Paint a 12 by 14 ft bedroom' } });
} catch (error) {
if (error instanceof InsufficientCreditsError) {
// buy credits in the dashboard
} else if (error instanceof HometownError) {
console.error(error.code, error.requestId);
} else {
throw error;
}
}
The class names follow the codes: rate_limited is RateLimitedError, validation_failed is ValidationFailedError. A request that never gets an answer throws ConnectionError.
Check a webhook
Pass the raw body, before any JSON parsing. With Express, read it with express.raw():
import { verifyWebhook } from '@hometown-workforce/estimator';
const event = await verifyWebhook(req.body, req.get('Hometown-Signature'), process.env.HOMETOWN_WEBHOOK_SECRET!);
It throws WebhookSignatureError when no signature matches or the delivery's timestamp is more than five minutes off.