A simple HTTP API to calculate interest rates and years to save up a target amount.
More details on our Python examples can be found in our GitHub
repository.
You will need to download the simple goal seek example from us,
upload it to your GRID account and replace REPLACE_WITH_YOUR_SPREADSHEET_ID with its workbook id.
See our quick start guide for more details.
Copy
# /// script# requires-python = ">=3.9"# dependencies = [# "fastapi[standard]",# "grid-api",# "uvicorn",# ]# ///import uvicornfrom fastapi import FastAPIfrom grid_api import AsyncGrid, APIConnectionError, APIStatusError, RateLimitErrorapp = FastAPI()@app.get("/seek_interests")async def seek_interests(target_amount: float): client = AsyncGrid() try: response = await client.workbooks.query( id="REPLACE_WITH_YOUR_SPREADSHEET_ID", goal_seek={ "targetCell": "Sheet1!C7", "targetValue": target_amount, "controlCell": "Sheet1!C5", }, read=[] ) except APIConnectionError as e: return {"error": "The server could not be reached."} except RateLimitError as e: return {"error": "A 429 status code was received; we should back off a bit."} except APIStatusError as e: return {"error": e.message} solution = response.goalSeek["solution"] return {"interest_rate": solution}@app.get("/seek_years")async def seek_years(target_amount: float): client = AsyncGrid() try: response = await client.workbooks.query( id="REPLACE_WITH_YOUR_SPREADSHEET_ID", goal_seek={ "targetCell": "Sheet1!C7", "targetValue": target_amount, "controlCell": "Sheet1!C4", }, read=[] ) except APIConnectionError as e: return {"error": "The server could not be reached."} except RateLimitError as e: return {"error": "A 429 status code was received; we should back off a bit."} except APIStatusError as e: return {"error": e.message} solution = response.goalSeek["solution"] return {"years": solution}if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)
Make sure your API key is set in the environment variable GRID_API_TOKEN, or passed as an argument to the Grid class.