Skip to content

Mortgage · Calculation

Rent versus buy over a holding period

Compare the current screen's owner and renter spending, home equity and invested balances over an explicitly supplied horizon.

Example result · downPayment: 60000 See output

Cost per success
2 calculation credits
Production plans
Standard, Business, Scale, Enterprise
Plan allowances
Free sandbox
Not available

Integration example

Make a request

Use a production key scoped to rent-or-buy-horizon.

Keep the key on your server. A successful call uses 2 calculation credits.

Production request

Set HT_PRODUCTION_KEY on your server. Save as example.mjs and run node example.mjs with Node.js 18 or later.

import { randomUUID } from "node:crypto";

const key = process.env.HT_PRODUCTION_KEY;
if (!key) throw new Error("Set HT_PRODUCTION_KEY on your server.");
const endpoint = "https://calc.hasstechapi.com/v2/calc/rent-or-buy-horizon";
const input = {
  "homePrice": 300000,
  "downPaymentPercent": 20,
  "mortgageRate": 0,
  "loanTermYears": 20,
  "propertyTaxRate": 1.2,
  "annualInsurance": 1200,
  "maintenanceRate": 1.2,
  "hoaMonthly": 100,
  "closingCostRate": 2,
  "sellingCostRate": 5,
  "homeAppreciationRate": 0,
  "monthlyRent": 1500,
  "annualRentIncrease": 0,
  "rentersInsuranceMonthly": 20,
  "investmentReturnRate": 0,
  "timeHorizonYears": 5
};
const requestId = process.env.HT_REQUEST_ID || Date.now() + "." + randomUUID();
// Keep this ID to look up usage if the connection is interrupted.
console.error("Usage lookup ID:", requestId);

const response = await fetch(endpoint, {
  method: "POST",
  redirect: "error",
  headers: {
    "Authorization": "Bearer " + key,
    "Content-Type": "application/json",
    "Idempotency-Key": requestId
  },
  body: JSON.stringify(input)
});
if (!response.ok) {
  const problem = await response.json().catch(() => null);
  const code = problem?.error?.code || response.statusText;
  const delay = response.headers.get("retry-after");
  if (delay) console.error("Retry-After (seconds):", delay);
  console.error("HTTP " + response.status + " " + code + ". Usage lookup ID: " + requestId);
  if (Array.isArray(problem?.error?.details)) {
    for (const issue of problem.error.details) console.error("Input issue:", JSON.stringify(issue));
  }
  process.exitCode = 1;
} else {
  console.log(JSON.stringify(await response.json(), null, 2));
}
cURL (shell)

Set HT_PRODUCTION_KEY on your server. Run in a POSIX shell with cURL 7.76+ and Python 3.7+ to create a request ID.

(
  set -eu
  : "${HT_PRODUCTION_KEY:?Set HT_PRODUCTION_KEY on your server}"
  request_id="${HT_REQUEST_ID:-}"
  if [ -z "$request_id" ]; then
    request_id=$(python3 -c 'import time, uuid; print(str(time.time_ns() // 1000000) + "." + str(uuid.uuid4()))')
  fi
  printf 'Usage lookup ID: %s\n' "$request_id" >&2
  # On INVALID_INPUT, read error.details in the JSON response body.
  curl --disable --silent --show-error --fail-with-body --include \
    --request POST 'https://calc.hasstechapi.com/v2/calc/rent-or-buy-horizon' \
    --header "Authorization: Bearer $HT_PRODUCTION_KEY" \
    --header 'Content-Type: application/json' \
    --header "Idempotency-Key: $request_id" \
    --data-binary @- <<'HT_INPUT'
{
  "homePrice": 300000,
  "downPaymentPercent": 20,
  "mortgageRate": 0,
  "loanTermYears": 20,
  "propertyTaxRate": 1.2,
  "annualInsurance": 1200,
  "maintenanceRate": 1.2,
  "hoaMonthly": 100,
  "closingCostRate": 2,
  "sellingCostRate": 5,
  "homeAppreciationRate": 0,
  "monthlyRent": 1500,
  "annualRentIncrease": 0,
  "rentersInsuranceMonthly": 20,
  "investmentReturnRate": 0,
  "timeHorizonYears": 5
}
HT_INPUT
)
If your request fails
400 INVALID_INPUT

Read error.details when present. Node prints each issue. cURL returns the JSON body. Each path points into your request. An empty path refers to the whole body.

Check the accepted inputs for required fields, units and ranges. Send the corrected JSON as a new request.

401 or 403
Check that the server has the bearer key for the endpoint’s environment and that the key includes rent-or-buy-horizon.
429
Read the service error code, check your allowance, and wait for the Retry-After interval when supplied before trying again.
Lost response, 409 or 503
Open Request lookup, select the same environment and enter the printed usage lookup ID. A settled ID has already consumed credits. To retry the same calculation, set HT_REQUEST_ID to the original ID and reuse the same inputs. A settled request does not return its result again. Unset the variable for a new request, which can consume credits.

The usage lookup ID is the Idempotency-Key your server sends. The response’s x-request-id identifies the HTTP exchange for support.

Output

Example result

These values come from running this operation on the example inputs.

downPayment
60000
loanAmount
240000
closingCosts
6000
Full example response (JSON)
{
  "status": "available",
  "result": {
    "downPayment": 60000,
    "loanAmount": 240000,
    "closingCosts": 6000,
    "mortgagePayment": 1000,
    "startParts": {
      "principalAndInterest": 1000,
      "propertyTax": 300,
      "insurance": 100,
      "maintenance": 300,
      "hoa": 100,
      "pmi": 0
    },
    "startBuyMonthly": 1800,
    "startRentMonthly": 1520,
    "buySpent": 174000,
    "rentSpent": 91200,
    "interestPaid": 0,
    "principalPaid": 60000,
    "taxPaid": 18000,
    "insurancePaid": 6000,
    "maintenancePaid": 18000,
    "hoaPaid": 6000,
    "pmiPaid": 0,
    "homeValue": 300000,
    "balance": 180000,
    "equity": 120000,
    "saleProceeds": 105000,
    "homeownerWealth": 105000,
    "renterWealth": 82800,
    "buyNetCost": 69000,
    "wealthGap": 22200,
    "costGap": 22200,
    "costBreakeven": 30,
    "wealthBreakeven": 30,
    "winner": "buy",
    "costLeader": "buy",
    "chartData": [
      {
        "year": 1,
        "rentCost": 18240,
        "buyNetCost": 30600,
        "renterWealth": 69360,
        "homeownerWealth": 57000
      },
      {
        "year": 2,
        "rentCost": 36480,
        "buyNetCost": 40200,
        "renterWealth": 72720,
        "homeownerWealth": 69000
      },
      {
        "year": 3,
        "rentCost": 54720,
        "buyNetCost": 49800,
        "renterWealth": 76080,
        "homeownerWealth": 81000
      },
      {
        "year": 4,
        "rentCost": 72960,
        "buyNetCost": 59400,
        "renterWealth": 79440,
        "homeownerWealth": 93000
      },
      {
        "year": 5,
        "rentCost": 91200,
        "buyNetCost": 69000,
        "renterWealth": 82800,
        "homeownerWealth": 105000
      }
    ]
  },
  "formulaVersion": "rent-or-buy-horizon-htcalc-719df2c0e105",
  "units": {
    "startBuyMonthly": "USD/month",
    "startRentMonthly": "USD/month",
    "homeValue": "USD",
    "equity": "USD",
    "renterWealth": "USD",
    "homeownerWealth": "USD",
    "wealthGap": "USD"
  },
  "assumptions": [],
  "warnings": [],
  "creditCost": 2
}

Request contract

Accepted inputs

Fields for this example. Expand the schema for all supported combinations and limits.

FieldType, units and limits

homePriceRequired
number

Positive US dollars

Min 0.01 · Max 1000000000
downPaymentPercentRequired
number

Percent; 5 means 5%

Min 0 · Max 100
mortgageRateRequired
number

Annual note rate in percent; 6 means 6%

Min 0 · Max 50
loanTermYearsRequired
number

Loan term in years, including fractional years

Min 1 · Max 50
propertyTaxRateRequired
number

Percent; 5 means 5%

Min 0 · Max 100
annualInsuranceRequired
number

US dollars per year

Min 0 · Max 1000000000
maintenanceRateRequired
number

Percent; 5 means 5%

Min 0 · Max 100
hoaMonthlyRequired
number

US dollars per month

Min 0 · Max 100000000
closingCostRateRequired
number

Percent; 5 means 5%

Min 0 · Max 100
sellingCostRateRequired
number

Percent; 5 means 5%

Min 0 · Max 100
homeAppreciationRateRequired
number

Annual home appreciation in percent

Min -50 · Max 50
monthlyRentRequired
number

US dollars per month

Min 0 · Max 100000000
annualRentIncreaseRequired
number

Annual change in rent in percent

Min -50 · Max 50
rentersInsuranceMonthlyRequired
number

US dollars per month

Min 0 · Max 100000000
investmentReturnRateRequired
number

Annual investment return in percent

Min -50 · Max 50
timeHorizonYearsRequired
integer

Modeled years

Min 1 · Max 50
Example request body (JSON)
{
  "homePrice": 300000,
  "downPaymentPercent": 20,
  "mortgageRate": 0,
  "loanTermYears": 20,
  "propertyTaxRate": 1.2,
  "annualInsurance": 1200,
  "maintenanceRate": 1.2,
  "hoaMonthly": 100,
  "closingCostRate": 2,
  "sellingCostRate": 5,
  "homeAppreciationRate": 0,
  "monthlyRent": 1500,
  "annualRentIncrease": 0,
  "rentersInsuranceMonthly": 20,
  "investmentReturnRate": 0,
  "timeHorizonYears": 5
}
Full JSON schema
{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "homePrice": {
      "type": "number",
      "minimum": 0.01,
      "maximum": 1000000000,
      "description": "Positive US dollars"
    },
    "downPaymentPercent": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "description": "Percent; 5 means 5%"
    },
    "mortgageRate": {
      "type": "number",
      "minimum": 0,
      "maximum": 50,
      "description": "Annual note rate in percent; 6 means 6%"
    },
    "loanTermYears": {
      "type": "number",
      "minimum": 1,
      "maximum": 50,
      "description": "Loan term in years, including fractional years"
    },
    "propertyTaxRate": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "description": "Percent; 5 means 5%"
    },
    "annualInsurance": {
      "type": "number",
      "minimum": 0,
      "maximum": 1000000000,
      "description": "US dollars per year"
    },
    "maintenanceRate": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "description": "Percent; 5 means 5%"
    },
    "hoaMonthly": {
      "type": "number",
      "minimum": 0,
      "maximum": 100000000,
      "description": "US dollars per month"
    },
    "closingCostRate": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "description": "Percent; 5 means 5%"
    },
    "sellingCostRate": {
      "type": "number",
      "minimum": 0,
      "maximum": 100,
      "description": "Percent; 5 means 5%"
    },
    "homeAppreciationRate": {
      "type": "number",
      "minimum": -50,
      "maximum": 50,
      "description": "Annual home appreciation in percent"
    },
    "monthlyRent": {
      "type": "number",
      "minimum": 0,
      "maximum": 100000000,
      "description": "US dollars per month"
    },
    "annualRentIncrease": {
      "type": "number",
      "minimum": -50,
      "maximum": 50,
      "description": "Annual change in rent in percent"
    },
    "rentersInsuranceMonthly": {
      "type": "number",
      "minimum": 0,
      "maximum": 100000000,
      "description": "US dollars per month"
    },
    "investmentReturnRate": {
      "type": "number",
      "minimum": -50,
      "maximum": 50,
      "description": "Annual investment return in percent"
    },
    "timeHorizonYears": {
      "type": "integer",
      "minimum": 1,
      "maximum": 50,
      "description": "Modeled years"
    }
  },
  "required": [
    "homePrice",
    "downPaymentPercent",
    "mortgageRate",
    "loanTermYears",
    "propertyTaxRate",
    "annualInsurance",
    "maintenanceRate",
    "hoaMonthly",
    "closingCostRate",
    "sellingCostRate",
    "homeAppreciationRate",
    "monthlyRent",
    "annualRentIncrease",
    "rentersInsuranceMonthly",
    "investmentReturnRate",
    "timeHorizonYears"
  ]
}

Production endpoint: POST https://calc.hasstechapi.com/v2/calc/rent-or-buy-horizon
Formula: rent-or-buy-horizon-htcalc-719df2c0e105

Method and sources

Current screen model of housing, invested cash and net wealth under entered assumptions and term. For sub-20%-down conventional cases it uses its model PMI rate and scheduled cancellation.

Reviewed .