Mortgage calculator and weekly rates
Mortgage math and weekly Freddie Mac rates: payment, full monthly cost, affordability
For when the answer to “It has a mortgage payment or affordability calculator” is yes.
Gives a real estate or finance site a correct mortgage payment calculator, a how-much-house-can-I-afford solver and this week's national average rates from Freddie Mac, in two small files with no dependencies.
Skip the rebuild
You do not write this one. It arrives in your project as ordinary source you can read, change and keep, with its audit record and its caveats beside it.
- Download the Playground. It is free and runs on your own machine.
- Start a new app and tick “It has a mortgage payment or affordability calculator”.
- This block is written in, along with anything else you ticked.
The mistake it removes
Hand-rolled mortgage math divides by zero at a 0% rate, forgets PMI below 20% down, or solves affordability for a price whose real payment overshoots the budget. A rates widget that fetches on every page view goes blank the moment the source is down.
What is already handled
Every item here is checked by running the block’s own tests every time its checks run. Tested means the block’s own test suite runs that code. It does not mean a live run against an outside service such as a payment provider.
- 45 assertions (counted here: test/mortgage-kit.test.mjs), all passing Tested
- payment() gives the textbook $1,799 a month for $300k at 6% over 30 years, and handles a 0% rate without dividing by zero Tested
- affordPrice() returns a price whose full monthly cost fits the budget it was solved for and uses nearly all of it, with and without PMI Tested
- the PMMS parser reads the latest week, last week and a year ago, and returns null for an empty or HTML response Tested
- the rates feed serves memory inside six hours, shares one fetch between concurrent callers, serves the last good copy during an outage from memory or disk, and returns null without throwing when it has nothing Tested
What the audit found
Named rather than summarized. The reasoning behind each one ships inside the block, so it travels with the code instead of living on a page you have to trust. A finding marked fixed was fixed after the audit ran; the verdict above is still the one the audit gave.
- Rates fetch timeout covers headers only; a stalled body hangs latestRates() for every caller, permanently major · Error handling & resilience (7) Fixed after the audit in
3bdb610a2 - No input validation: NaN or missing rate, negative years, negative down payment produce wrong or NaN figures instead of 0 or an error minor · Correctness (2) Fixed after the audit in
dabdfe5b2 - The back-end debt-to-income ratio is decorative in the suite: changing 0.36 to 0.5 survives all 28 assertions minor · Verification integrity (28) / Correctness (2) Fixed after the audit in
59a517e07 - A corrupt saved rates file is swallowed silently: the widget goes blank with no log line naming why minor · Error handling & resilience (7) Fixed after the audit in
c0c5291ee
What you still have to do
A copied file cannot own your secrets, your host or your legal obligations. This is the part the block deliberately does not claim.
- Show the source and sourceUrl that latestRates() returns next to every rate you display, and say it is a national weekly average and not a quote.
- Put a line under every calculator saying the figures are for illustration only, are not a loan offer and are not an advertisement of credit terms, and never publish an example payment of your own. In the US that is Regulation Z (12 CFR 1026.24).
- affordPrice() uses the 28% / 36% debt-to-income rules of thumb. Lenders differ, and the page must say these are estimates.
- The rates are Freddie Mac's national weekly survey averages, not a quote for any person or state.
- No request in the tests reaches Freddie Mac. Fetch the live CSV once before launch to confirm the column order (date, 30-year, points, 15-year) still holds.
What lands in your project
vanilla
- vanilla/mortgage-math.js
payment(principal, ratePct, years), monthlyCost({price, down, rate, ...}), affordPrice({income, debts, down, rate, ...}). Pure functions: runs in the browser and in Node.
node
- node/pmms-rates.js
latestRates() -> { week, rate30, rate15, prev30, prev15, yearAgo30, source, sourceUrl } or null. createRates({ file, userAgent, fetch }) for your own settings. Serve it from an API route; do not call Freddie Mac from the browser.
What you supply
- DATA_DIR: where the last good rates copy is saved (default ./data/pmms.json), or pass createRates({ file })
- userAgent: identify your site to Freddie Mac (default 'mortgage-kit/1.0')
Licensed MIT. It is a starting point, not a finished product.
Get it
Download the Playground See the other blocks
Nothing here is locked. The files are yours, in your folder, under a permissive license.
Questions
- Is Mortgage calculator and weekly rates audited?
- It was audited on 2026-09-29, and the verdict was not a clean pass. 4 findings have been fixed since then, and the verdict stands until the block is audited again.
- What do I still have to do myself?
- Show the source and sourceUrl that latestRates() returns next to every rate you display, and say it is a national weekly average and not a quote.
- How do I get this code?
- Download the Playground, start a new app, and tick “It has a mortgage payment or affordability calculator”. The block is written into your project as ordinary source you can read and edit.
All pre-built code blocks · Learn to build from zero · The coding guide