# How it works

> The path of a sign-up, a sign-in and a query, from your app through the engine to your database, and where each decision is made.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/start/how-it-works/

For agents: Read this for the end-to-end flow: the app calls https://<ref>.data.sypnia.com with a publishable key; sign-up creates a user and sends a confirmation e-mail; verifying the link returns an access token (EdDSA JWT) and a refresh token; every Data API request runs in one Postgres transaction with SET LOCAL ROLE and request.jwt.claims so row-level security applies; machines use OAuth tokens the same way.

Your app talks to one address per project, `https://<ref>.data.sypnia.com` (or your own domain when you self-host). Behind it there is one program, the engine, and your Postgres.

**Figure**: The parts of Sypnia Data. Apps, agents and the console call the engine; the engine is one Rust binary with five modules; everything it keeps lives in your Postgres.

1. Your app, with `@sypnia/data` or any ORM, sends a publishable key and, once signed in, the user's access token.
2. Agents and services sign in with OAuth (key pairs, DPoP, the device flow) or a personal access token.
3. Auth checks credentials, keeps sessions, signs tokens and sends e-mails through your provider.
4. The Data API turns a URL into one SQL statement and runs it as the caller's database role.
5. Postgres holds everything: your tables, the `sypnia_auth` schema and the rules that decide who sees which row.

## A sign-up, step by step

### 1. Your app calls sign-up

`sypnia.auth.signUp({ email })` sends `POST /auth/v1/signup` with the publishable key. The engine checks the rate limits and, if you turned it on, the CAPTCHA.

### 2. The engine creates the user and sends the e-mail

A row in `sypnia_auth.users`, unconfirmed. A one-time token, stored only as a hash, goes in an e-mail through the project's e-mail provider: a link and a 6-digit code.

### 3. The person opens the link

`GET /auth/v1/verify` checks the token, confirms the e-mail and redirects to your app with a session in the address fragment. Without a password yet, your app shows «create your password».

### 4. Your app holds a session

An **access token** (a JWT signed with the project's Ed25519 key, one hour by default) and a **refresh token** (rotated on every use). The SDK renews the access token a minute before it expires.

## A query, step by step

`sypnia.from('orders').select('id, total')` becomes `GET /rest/v1/orders?select=id,total`. The engine:

1. reads the key or token and decides the **database role**: `anon`, `authenticated` or `service_role`;
2. opens a transaction in your database, runs `SET LOCAL ROLE authenticated` and stores the token's claims in `request.jwt.claims`;
3. runs one SQL statement; your [row-level security](https://data.docs.sypnia.com/data/row-level-security) policies, which call `auth.uid()`, keep only the rows this user may see;
4. commits and returns JSON.

The engine never filters rows itself: Postgres does. That is why the same rules hold whether a request comes from the Data API, from an ORM on your server or from an agent.

## Where each decision is made

| Decision                                                                | Made by                                                                                    |
| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| Is this password right, is this link valid, may this client get a token | The engine (Rust)                                                                          |
| How long a session lives                                                | Your project's [session policy](https://data.docs.sypnia.com/auth/sessions-and-tokens)     |
| Which rows a user sees or changes                                       | Your database, through row-level security                                                  |
| What your app does for a person                                         | Your code, with any ORM or none ([Use any ORM](https://data.docs.sypnia.com/data/any-orm)) |
