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
Each of these was checked by running the code, not by reading it.
- 28 assertions (counted here: test/mortgage-kit.test.mjs), all passing
- payment() gives the textbook $1,799 a month for $300k at 6% over 30 years, and handles a 0% rate without dividing by zero
- 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
- the PMMS parser reads the latest week, last week and a year ago, and returns null for an empty or HTML response
- 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
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?
- No. No full audit has been run against this block yet, which is not the same as a pass. What is verified about it is listed on this page, and what is not is listed beside it.
- 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