A mortgage calculator as an API, from an Excel workbook
A mortgage calculation looks like one formula and is a small model: the rate depends on the terms of the bank, the terms depend on how much of the price is financed, and the customer wants to see the balance year by year. Banks, brokers and portals keep such models in Excel, because the people who own the terms can maintain them there.
This post takes one such workbook and shows what it looks like as an API. The workbook is a demo: the bank and its terms are invented. The service is real. It is published on SpreadAPI, the calls below were made on 13 November 2026, and the answers are the ones it returned. You can try it on the page Mortgage calculator API.
The workbook
The Excel file has three sheets.
Loan is the calculation sheet, with five input cells and seven result cells:
- The loan amount is the purchase price minus the down payment.
- The loan-to-value is the loan amount divided by the purchase price.
- The interest rate is looked up in the terms table, with
INDEX,XMATCHfor the loan-to-value tier andMATCHfor the fixed period. - The monthly payment is the loan amount times interest rate plus initial repayment, divided by 12.
- The years until the loan is fully repaid come from
NPER.
Repayment plan has one row per year of the fixed period. It is a single spilled formula built on SEQUENCE and LET, so the table grows from 5 to 10 or 15 rows with the fixed period.
Terms holds the bank's rates: four loan-to-value tiers (up to 60%, 80%, 90% and 100%) and three fixed periods (5, 10 and 15 years).
Nothing in it is a macro. That matters, because VBA macros do not run in a service; formulas do.
The service
Publishing the workbook means choosing which cells a caller may fill and which cells come back. See Your first service for the steps.
The inputs, with the limits set in the service:
purchase_price: 100,000 to 2,000,000down_payment: 0 to 1,000,000fixed_period: 5, 10 or 15 yearsinitial_repayment: 1% to 10%, sent as a decimalextra_repayment: 0 to 20,000 per year
The outputs:
loan_amount,loan_to_value,interest_rate,monthly_paymentbalance: the balance at the end of the fixed periodinterest: the interest paid in the fixed periodyears_to_repayrepayment_plan: the table from the second sheet, as a cell range
The terms sheet is neither an input nor an output. A caller never sees it.
One call
curl -X POST \
"https://spreadapi.io/api/v1/services/{serviceId}/execute" \
-H "Content-Type: application/json" \
-d '{"inputs": {"purchase_price": 420000, "down_payment": 90000,
"fixed_period": 10, "initial_repayment": 0.02,
"extra_repayment": 0}}'The outputs of the answer, shortened to name, value and number format:
loan_amount 330000 $#,##0.00
loan_to_value 0.7857 0.0%
interest_rate 0.0371 0.00%
monthly_payment 1570.25 $#,##0.00
balance 250238.93 $#,##0.00
interest 108668.93 $#,##0.00
years_to_repay 28.3 0.0
repayment_plan (a table, see below)Read it like this: 330,000 are financed, which is 78.6% of the price. That falls into the tier "up to 80%", and for 10 years fixed the terms table gives 3.71%. The payment is 1,570.25 a month. After 10 years 250,238.93 are still owed, and 108,668.93 of interest have been paid.
Values come back raw. 0.0371 is the cell's value, and formatString tells your front end to show it as 3.71%. Percentages go in the same way: 0.02 for 2%.
The repayment plan is a table
An output can be a cell range. It comes back as a list of rows, each row a list of cell values. The first rows of repayment_plan from the call above:
Year Payments Interest Repayment Extra Balance
1 18843 12129.61 6713.39 0 323286.61
2 18843 11876.26 6966.74 0 316319.87
3 18843 11613.36 7229.64 0 309090.23
...
10 18843 9473.23 9369.77 0 250238.93We rounded interest and repayment to two decimals for this listing. The API returned them as the cells hold them, for example 12129.609999999986. Round for display in your front end, or with ROUND in the workbook.
Two things to know about range outputs:
- The range is fixed when the service is set up. Here it has room for 15 years, so with a fixed period of 10 years the answer has the header row, 10 rows of numbers and 5 rows of empty cells (
null). Skip empty rows when you draw the table. - The table is part of every answer. If a client only needs the monthly payment, a second service without the table keeps its answers small.
Four scenarios in one request
A customer rarely asks one question. "What if I fix the rate for 15 years? What if I repay more?" The discovery address of a service takes 2 to 20 scenarios in one request. This is the script we ran, with Node.js 22 and no packages:
// Compare four financing options in one request
const DISCOVERY = 'https://spreadapi.io/d/{serviceId}';
const base = {
purchase_price: 420000,
down_payment: 90000,
fixed_period: 10,
initial_repayment: 0.02,
extra_repayment: 0
};
const response = await fetch(DISCOVERY, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'batch',
scenarios: [
{ label: '5 years fixed', inputs: { ...base, fixed_period: 5 } },
{ label: '10 years fixed', inputs: base },
{ label: '15 years fixed', inputs: { ...base, fixed_period: 15 } },
{ label: '10 years, 3% repayment, 5,000 extra',
inputs: { ...base, initial_repayment: 0.03, extra_repayment: 5000 } }
]
})
});
const data = await response.json();
const pick = ['interest_rate', 'monthly_payment', 'balance', 'years_to_repay'];
for (const scenario of data.scenarios) {
const values = Object.fromEntries(
scenario.outputs.map((o) => [o.name, o.value])
);
console.log(scenario.label, pick.map((name) => `${name}=${values[name]}`).join(' '));
}Its output:
5 years fixed interest_rate=0.0348 monthly_payment=1507 balance=294011.81 years_to_repay=29
10 years fixed interest_rate=0.0371 monthly_payment=1570.25 balance=250238.93 years_to_repay=28.3
15 years fixed interest_rate=0.0393 monthly_payment=1630.75 balance=195422.01 years_to_repay=27.7
10 years, 3% repayment, 5,000 extra interest_rate=0.0371 monthly_payment=1845.25 balance=150953.94 years_to_repay=21.7The first three lines show the terms table at work: a longer fixed period costs a higher rate, 3.48%, 3.71% and 3.93%. The last line is the option with 3% initial repayment and 5,000 of extra repayment a year: the payment rises to 1,845.25, and the balance after 10 years falls from 250,238.93 to 150,953.94.
Every scenario counts as one call of the service.
What a caller cannot send
The service refuses a value outside its limits before anything is calculated, with HTTP 400 and a message that names the input. A fixed period of 7 years is not in the list of allowed values; a purchase price of 50,000 is below the minimum. We did not run these cases against the mortgage service. The answer for a refused value is shown with a real example in Building AI agents that use your Excel models.
These limits are the place for the rules of the business: which loan sizes you finance, which fixed periods you offer. They are set per input in the service; a drop-down list in the workbook becomes the list of allowed values on import. See Input validation.
Where the same service is used
- On a website: your front end calls the API and draws the result. Or you use the web calculator that SpreadAPI makes from the service, without front-end code. See Excel web calculator.
- In an AI assistant: every published service has an MCP address. An assistant such as Claude or ChatGPT sends the inputs and reports the outputs, instead of estimating a payment. See Excel MCP server.
- In a workflow: as a step in n8n, Zapier or Power Automate, for example to put the payment into an offer document.
All of them get the same numbers, because all of them call the same workbook.
When the terms change
Rates change often. The person who owns the workbook changes the terms sheet in Excel. Then the new workbook is imported into the service and published again. Inputs, outputs and tokens are kept, and callers get the new rates from the next call on. Until then they get the published version.
SpreadAPI keeps no version history of a service. Keep the dated workbooks in your own filing, so that you can show later which terms applied when.
What this example is not
- Not a real bank. The terms are invented. The numbers show how the service answers, not what a loan costs.
- Not advice. A service returns what the formulas calculate. Whether an offer is binding, and which disclosures it needs, is your responsibility.
- Not every mortgage. This workbook models an annuity loan with a fixed period, an initial repayment and yearly extra repayments. Other products need other formulas, and those are yours to write, in Excel.
- Not a goal seek. "How much can I borrow for 1,500 a month?" turns the calculation around. A service does not search for an input; how to answer such questions is described in Excel goal seek over an API.