Python FastAPI backend
A FastAPI server. main.py serves a health check and an example route that lists a table’s rows. vaif_client.py creates two clients from the environment: one with the publishable key, under which row rules apply, and one with the secret key, which bypasses them.
Create the app
Section titled “Create the app”vaif init --template python-fastapi-backendRun it in a new, empty folder. It writes vaif.config.json and the template's files there. It stops if the folder already has a vaif.config.json, and leaves any other file that already exists alone; --force overwrites both.
It asks which features to include. This template has files for Database, Authentication, Storage and Functions; Database and Authentication are selected to start with. To skip the question, name them: vaif init --template python-fastapi-backend --features database,auth,storage,functions. Add one later with vaif init --template python-fastapi-backend --add-features <feature>.
What it writes
Section titled “What it writes”| File | Written |
|---|---|
main.py | Always |
vaif_client.py | Always |
requirements.txt | Always |
.env.example | Always |
.gitignore | Always |
README.md | Always |
migrations/0001_initial.sql | With Database |
models.py | With Database |
middleware/auth.py | With Authentication or Storage |
middleware/__init__.py | With Authentication or Storage |
routes/storage.py | With Storage |
routes/__init__.py | With Storage |
routes/functions.py | With Functions |
functions/hello.py | With Functions |
Authentication adds a middleware that checks the caller’s access token. Storage and Functions add routes that upload files and invoke your project’s functions.
Install
Section titled “Install”The code this template writes uses the client for its language:
pip install "vaifstudio==1.0.0"requirements.txt lists the Python client with FastAPI and its server, so pip install -r requirements.txt installs everything once the client is on PyPI; the notice above says whether it is. Until then, call the REST API from Python with any HTTP client.
Configure
Section titled “Configure”Copy .env.example to .env and fill it in. The project ID and the keys are on Security > Credentials in the console.
| Variable | Value |
|---|---|
VAIF_PROJECT_ID | Your project ID. |
VAIF_PUBLISHABLE_KEY | The project's publishable key (vk_pub_). Row rules apply to it, so it is safe in an app. |
VAIF_SECRET_KEY | The project's secret key (vk_srv_), for server code only. It bypasses row rules; never ship it in a browser or mobile app. |
CORS_ORIGINS | The addresses whose browser code may call this server, separated by commas. Without it, only http://localhost:3000. |
Set up the database
Section titled “Set up the database”The template writes one SQL file for your database. The comment line above each CREATE TABLE sets that table's row rules.
migrations/0001_initial.sql, written with the Database feature, is an example: edit it to describe your own tables before you apply it.
-- Initial migration. Customize this schema for your project; vaif db push-- applies it. The comment directly above each CREATE TABLE sets that table's-- row rules: here each signed-in user reads and changes only their own rows,-- and the owner column is filled in from their sign-in on insert. Keep a rule-- comment on every table you add: a table with no row rules is closed to-- signed-out visitors but open to every signed-in user.
-- vaif:owner idCREATE TABLE IF NOT EXISTS "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), "email" text NOT NULL UNIQUE, "name" text, "created_at" timestamp with time zone NOT NULL DEFAULT now());
-- vaif:owner user_idCREATE TABLE IF NOT EXISTS "posts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), "user_id" uuid NOT NULL REFERENCES "users"("id"), "title" text NOT NULL, "content" text, "published" boolean NOT NULL DEFAULT false, "created_at" timestamp with time zone NOT NULL DEFAULT now());users(-- vaif:owner id): each signed-in user reads and changes only their own rows, andidis filled in from their sign-in.posts(-- vaif:owner user_id): each signed-in user reads and changes only their own rows, anduser_idis filled in from their sign-in.
Put your project ID in vaif.config.json ("projectId"), sign in, and apply it. With no --dir, vaif db push finds the template's SQL folder, or a ./migrations folder, and applies its .sql files in name order.
vaif loginvaif db push --dry-run # what each file would changevaif db pushRun it
Section titled “Run it”uvicorn main:app --reloadNext steps
Section titled “Next steps”- Python SDK for the client’s tables, sign-in, storage and functions.
- REST API for calling the API from Python with any HTTP client.