logo

Exposing Sheets with FastAPI

To build a real web app, you often need an API. We can use FastAPI to wrap our Google Sheets code and serve it to the world.

Imagine an endpoint /users that returns your spreadsheet rows as JSON.

from fastapi import FastAPI
import gspread

app = FastAPI()
gc = gspread.service_account(filename='credentials.json')
sh = gc.open("My Database")

@app.get("/users")
def get_users():
    # Fetch fresh data every time the API is called
    worksheet = sh.sheet1
    return worksheet.get_all_records()

Now, when you visit http://localhost:8000/users, you get a beautiful JSON response generated directly from your live spreadsheet!

[
  { "Name": "Alice", "Age": 25, "City": "New York" },
  { "Name": "Bob", "Age": 30, "City": "Paris" }
]

This creates a powerful bridge between your simple spreadsheet and a modern frontend like React or Next.js.

I teach this full architecture in my Full Stack Google Sheets course.