API Spreadsheets Logo

Automate calculations from your spreadsheets

API Spreadsheets Team ·

Automate calculations from your spreadsheets

Overview

Maybe you want to build a web calculator that figures out mortgage payments and eligibility. Or maybe you're building a calculator that figures out your ideal tire pressure.

No matter what you build, the first step is often to understand and translate complex formulas someone has already built in a spreadsheet. In this guide, you'll learn how to automate those spreadsheet calculations using Python and API Spreadsheets (free to start).

What you'll build

Prerequisites

1. Prepare your spreadsheet

Start with a spreadsheet that takes in some basic input about your finances and returns key mortgage payment details. In our example:

Example mortgage calculator spreadsheet (inputs in green, outputs in yellow)
Example mortgage calculator spreadsheet (inputs in green, outputs in yellow)

In this spreadsheet, all of the yellow cells are calculated by formulas. We're going to let API Spreadsheets run those formulas for us, on demand, from a Python script.

2. Upload your spreadsheet to API Spreadsheets

  1. Go to www.apispreadsheets.com and upload your mortgage calculator spreadsheet. Create a free account if you don't already have one.
Upload your spreadsheet to API Spreadsheets
Upload your spreadsheet to API Spreadsheets
  1. After a successful upload, you'll be taken to the file page. From here, copy your:
    • File ID
    • File Access Key
    • File Secret Key
Copy File ID, File Access Key & File Secret Key
Copy File ID, File Access Key & File Secret Key

We'll plug these values into the Python script in the next step so it can call the /calculate endpoint securely.

3. Create & run your Python script

Now create a new Python file (for example mortgage_calculate.py) and paste in the script below. Replace the placeholders with your own File ID, access key, and secret key.

import requests

## TODO: REPLACE YOUR FILE ID, ACCESS_KEY AND SECRET_KEY HERE
YOUR_FILE_ID = "5xx8KSWn5QGeBW69"
YOUR_ACCESS_KEY = "YOUR_ACCESS_KEY"
YOUR_SECRET_KEY = "YOUR_SECRET_KEY"

## this is the API URL
url = f"https://api.apispreadsheets.com/calculate/{YOUR_FILE_ID}"

## format is {"sheet_name": {"cell_ref_1": input_values, ...}}
input_cells = {
    "Mortgage Calculator": {
        "B3": 400000,  # Home price
        "B4": 20,      # Down payment %
        "B5": 3        # Interest rate %
    }
}

## format is {"sheet_name": ["cell_1", "cell_2"]}
output_cells = {"Mortgage Calculator": ["B14", "B15", "B16"]}

req_body = {"input_cells": input_cells, "output_cells": output_cells}

headers = {
    "accessKey": YOUR_ACCESS_KEY,
    "secretKey": YOUR_SECRET_KEY
}

r = requests.post(url, json=req_body, headers=headers)
print(r.status_code)
print(r.text)

How this works

Run your script with python mortgage_calculate.py. You should see a 200 status code and a JSON response with your output values.

4. Adapting this to any calculator

The same pattern works for any spreadsheet-based calculator you have:

Let us know what you build

We'd love to see what you create with automated spreadsheet calculations—whether it's mortgage tools, pricing calculators, or something totally different. Share what you build in the comments, or reach out if you have any questions or run into any issues!