# How CubiBase works

Source: https://cubibase.com/docs/architecture

Also available: [Markdown version](https://cubibase.com/docs/architecture.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Two HTTP surfaces, one database per project. The Console manages the project through the control API; your application calls the project host.

## The two surfaces

**How a request reaches the project database, illustration.** Developer → Console (tables · rows · SQL · keys) → Control plane / Control API (projects · schema · auth settings). Application → Your app (apikey + user JWT) → Project host (example-project.api.cubibase.com: /rest/v1 · /auth/v1 · /storage/v1 · /functions) → One project database, example_project_db (public · auth · storage · logs).

Illustration. The Console manages the project; your app calls the project host.

| Surface | Who calls it | What it does |
| --- | --- | --- |
| Control API | The Console (your developer session) | Accounts, projects, tables, rows, SQL, API keys, auth settings, usage |
| Data API · `/rest/v1` | Your application | Reads and writes on the tables you expose |
| Auth API · `/auth/v1` | Your application | Sign-up, sign-in, sessions, Google, MFA |

> **important**: Your application should only call the project host. The control API paths the Console uses are not a public application interface.

## The project host

Every project gets a host of the form `https://{slug}.api.cubibase.com`. The engine resolves the project from the trusted `Host` header — it never consults `X-Forwarded-Host` — and validates the publishable key against that project.

**Example project**

| Piece | Value |
| --- | --- |
| Slug | `example-project` |
| API host | `https://example-project.api.cubibase.com` |
| Database | `example_project_db` |
| Data API base | `/rest/v1` |
| Auth API base | `/auth/v1` |

## What provisioning creates

Provisioning creates one PostgreSQL database and four login roles, all `NOSUPERUSER` and `NOBYPASSRLS`:

| Role | Used by | Purpose |
| --- | --- | --- |
| `ddl` | Schema API | Creates and alters tables |
| `api` | Data API | Serves exposed tables, limited by grants and row policies |
| `sql` | Console | Row browser and the read-only SQL editor |
| `auth` | Auth API | Sign-up, sign-in, and sessions against the `auth` schema |

Alongside `public`, where your tables live, the database has managed schemas: `auth` (users, sessions, factors, identities), `logs` (request and auth events), `vault` (project secrets no request role can read), and `storage` (the bucket and object catalog, upload sessions, and object events for S3-backed file storage). Developer accounts live separately, in the platform metadata database.

## How a request is authorized

1. The `Host` header selects the project; the `apikey` header must be an active publishable key for it.
2. The bearer JWT is verified (HS256 for CubiBase Auth, or RS256 against your IdP’s JWKS). Its `sub` claim is the user id.
3. For session-bearing tokens, the engine checks the session is still live and the user is not banned.
4. The query runs as the API role with `cubibase.user_id` set to that subject, so row policies apply to every statement.

## Additional resources

- [API keys](https://cubibase.com/docs/platform/api-keys) Publishable keys, secret keys, and rotation.
- [Row-level security](https://cubibase.com/docs/database/row-level-security) The policy that turns a JWT subject into row access.
- [PostgreSQL feature page](https://cubibase.com/features/postgresql) The product view of database isolation.
