Skip to Content

Python

Query a ZXCV database from server-side Python. You’ll end with a script that reads live data, then wire the same client into a FastAPI endpoint.

AI Assistance
Help me connect ZXCV to my Python app. Do the following:
1. Install the zxcv package with pip.
2. Initialize a client with my project ref + anon key (from Settings → API), reading them from environment variables (ZXCV_PROJECT / ZXCV_ANON_KEY) via os.environ rather than hard-coding.
3. Fetch rows from a "todos" table with .table("todos").select("*").execute() and print them, then serve them from a FastAPI endpoint.
4. Keep the anon key server-side — the Data Gateway enforces my policies on every request.
Ask me for my project ref and anon key if I haven't given them.

Create a project

In the dashboard  click Start building, or run zxcv from your terminal. Open Settings → API and copy your project ref and anon key — access is enforced server-side by your authorization rules.

Create a table

In the SQL editor, create a todos table with a couple of rows:

create table todos ( id bigint generated always as identity primary key, title text not null, is_complete boolean not null default false ); insert into todos (title) values ('Read the docs'), ('Ship an app');

Install the SDK

pip install zxcv

Initialize the client

Read your keys from the environment rather than hard-coding them. Set them in your shell (or a .env you load at startup):

.env
ZXCV_PROJECT=acme-store ZXCV_ANON_KEY=your-anon-key

Create a shared client:

zxcv_client.py
import os from zxcv import create_client zxcv = create_client( project=os.environ["ZXCV_PROJECT"], anon_key=os.environ["ZXCV_ANON_KEY"], )

Fetch and render

Query the table and print the rows. execute() returns data and errorerror is None on success:

main.py
from zxcv_client import zxcv response = zxcv.table("todos").select("*").execute() if response.error: raise SystemExit(response.error) for todo in response.data: print(todo["id"], todo["title"])

Run it:

python main.py

The anon key is safe in server-side code too — the Data Gateway enforces your .policy rules on every request, so a client can only read what its rules allow.

Serve it from FastAPI

Import the same client and return the rows from an endpoint:

app.py
from fastapi import FastAPI, HTTPException from zxcv_client import zxcv app = FastAPI() @app.get("/todos") def list_todos(): response = zxcv.table("todos").select("*").order("id").execute() if response.error: raise HTTPException(status_code=500, detail=str(response.error)) return response.data

Run the dev server:

pip install "fastapi[standard]" fastapi dev app.py

Deploy

Ship to the global edge from your terminal:

npm i -g zxcv zxcv link acme-store zxcv deploy --prod

Every push gets a preview URL; --prod promotes to your domain. See Deploy for custom domains and canaries.

Next steps