Install FastAPI and uvicorn first:
How the process starts is on Self-hosting. This page covers the route API once you have chosen which process serves it. After a service is running, FastAPI’s OpenAPI UI is at http://<host>:<port>/docs.
Mount on your own FastAPI app
@pxt.query evaluates the function body at decoration time. Define @pxt.query functions only after pxt schema update, because the decorator runs the function body immediately. Use a plain FastAPI @app.post() when one FastAPIRouter helper cannot express the request.
Compute without inserting
Table.compute() runs computed columns and returns the values without writing a row.
Same over HTTP:
Decorator-style routes
add_insert_route() builds the response model from the column schema. To return a custom JSON body, use @router.insert_route instead of add_insert_route(). The function receives outputs as keyword arguments and returns a pydantic.BaseModel.
At registration:
- Every parameter is keyword-only and annotated.
- Parameter names match
outputs exactly.
- Annotations match column types (nullable column:
T | None). Media columns arrive as URL strings: annotate str.
- Return type is a
pydantic.BaseModel subclass.
background=True works the same as the non-decorator forms. Decorator routes are Python-only.
Export to an external database
SqlExport writes each successful insert or update to another SQL table. Pixeltable commits first; then the external write. If the external write fails, the request is HTTP 500. No rollback.
The row is the response body (outputs). Media columns are URL strings. The target table must already exist.
SqlExport.method:
'insert' (default): append. Replaying the request duplicates the row.
'update': match on the target primary key. Not an upsert. No match: HTTP 500. Response columns must include every target PK plus at least one non-PK.
'merge': not supported.
A Pixeltable insert with method='update' is allowed: append-only here, current-state there.
export_sql= cannot combine with return_fileresponse=True. It works with background=True (the SQL write runs in the worker).
SqlExport
A connection string with an embedded password is plaintext in the application file. Pull credentials from the environment, a .pgpass-style file, or a pxt.Secret config var.
Full example
Return computed columns
insert(), update(), and batch_update() can return computed columns without a follow-up query:
status.rows is a list of dicts. For typed access, model_validate() with extra="ignore":
After .collect(), use to_pydantic(). After return_rows=True, use model_validate().
Background jobs
background=True returns a job handle immediately:
Poll job_url (pxt service list prints the base URL):
background cannot combine with return_fileresponse.
Flags: CLI.