# Hosting, getting started with the CLI

> From nothing to a working app with Infaris Auth, a database with RLS, storage, Realtime, a function and a cron, using only infaris-guard.

Source: https://docs.infaris.com/en/pulseguard/hosting-quickstart

In this quickstart you put a small todo app online: users sign in with a code by email, see only their own todos, get changes live, can attach a file and see counts from a function. Everything goes through the CLI; you never open Guard.

The example app (Next.js) lives in `examples/hosting-quickstart`. It uses `@infaris/auth-js`, `@infaris/data`, `@infaris/realtime-js` and `@infaris/storage-js`.

Everything starts closed. A table is unreachable until you put it in the Data API with a policy, the bucket is private, the function is private and sign-up is invite-only. Opening anything up takes an explicit confirmation (`--yes`) every time; see [Opening up](#opening-up) at the end.

## What you need

- The CLI, signed in with an organization that has Hosting:

  ```bash
  bun add -g @infaris/guard-cli
  npm install -g @infaris/guard-cli   # without Bun: Node 18 or later
  infaris-guard login
  ```

- `jq`, to take ids from the JSON answers. Every command below takes `--json`; without it you get readable output.
- A copy of `examples/hosting-quickstart`. Run every command in that folder.

## 1. A site for the app

```bash
infaris-guard hosting up --domain todo-quickstart
SITE=$(jq -r .siteId .infaris/hosting.json)
APP=https://$(infaris-guard hosting site show --site "$SITE" --json | jq -r .primaryDomain)
```

`hosting up` creates the site under an Infaris address, sets up its hosting and puts the app online. The app shows nothing useful yet; the next steps add that.

## 2. Infaris Auth: sign-in with a code

```bash
infaris-guard hosting auth projects create todos --site "$SITE" --origins "$APP" --redirect-urls "$APP/" --dry-run
REF=$(infaris-guard hosting auth projects create todos --site "$SITE" --origins "$APP" --redirect-urls "$APP/" --json | jq -r .ref)
infaris-guard hosting auth users invite "$REF" --email you@example.com
```

The project signs users in with a one-time code by email (OTP) and is invite-only by default. With `--site` the url and fresh keys land on the site: `INFARIS_AUTH_URL`, `NEXT_PUBLIC_INFARIS_AUTH_URL`, `NEXT_PUBLIC_INFARIS_AUTH_KEY` and the secret `INFARIS_AUTH_SECRET_KEY`. The answer only shows a hint of each key.

## 3. A database with the table

```bash
OP=$(infaris-guard hosting db create --name todos --site "$SITE" --json | jq -r .operation.id)
DB=$(infaris-guard hosting operation "$OP" --wait --json | jq -r .operation.resourceId)
infaris-guard hosting db migrate "$DB" db/migrations --dry-run
infaris-guard hosting db migrate "$DB" db/migrations --yes
infaris-guard hosting auth attach "$REF" --database "$DB"
```

The site gets `DATABASE_URL` as a secret variable; the password never reaches you or the CLI. `db migrate` runs the files in `db/migrations` that have not run yet, in name order, each in its own transaction, and records them in `public.schema_migrations`. `--dry-run` shows which ones. A second run skips them, and `hosting db migrations "$DB"` shows what ran. So the app has no migration code of its own. The migration creates `public.todos` with Row Level Security; that does not put the table in the Data API yet.

`auth attach --database` links the project to the database: the Data API and Realtime use it to check your users' tokens.

## 4. The table in the Data API

```bash
OP=$(infaris-guard hosting db api set "$DB" --enabled --auth-project "$REF" --json | jq -r .operation.id)
infaris-guard hosting operation "$OP" --wait
infaris-guard hosting db rls "$DB"
infaris-guard hosting db policies add "$DB" --schema public --table todos --name "own todos" --roles authenticated \
  --using "(select auth.uid()) = user_id" --with-check "(select auth.uid()) = user_id" --yes
infaris-guard hosting db expose "$DB" --schema public --table todos --roles authenticated \
  --privileges SELECT,INSERT,UPDATE,DELETE --dry-run
infaris-guard hosting db expose "$DB" --schema public --table todos --roles authenticated \
  --privileges SELECT,INSERT,UPDATE,DELETE --yes
infaris-guard hosting db lint "$DB"
DATA_URL=$(infaris-guard hosting db api "$DB" --json | jq -r .url)
```

Write `(select auth.uid())` instead of `auth.uid()`: Postgres then works out the user once per query instead of once per row. On 100,000 rows that makes counting and filtering up to seven times faster. Also index the columns you filter and sort on, such as `(user_id, done, id)`.

After `api set` nothing is open yet: `db rls` shows `todos` as `not_exposed`. The policy lets every signed-in user read and write only their own rows. `--dry-run` on `expose` shows what becomes reachable, and for whom; anonymous access stays off. `db lint` exits with code 1 as soon as something is open that should not be, so you can run it in CI too.

While the Data API is on, Hosting puts `INFARIS_DATA_URL` and `NEXT_PUBLIC_INFARIS_DATA_URL` on every site the database is attached to. The key is the one of the auth project (`NEXT_PUBLIC_INFARIS_AUTH_KEY`).

## 5. A private bucket

```bash
BUCKET=$(infaris-guard hosting storage buckets create todo-files --json | jq -r .id)
infaris-guard hosting storage attach "$BUCKET" --site "$SITE"
infaris-guard hosting storage lint
```

The bucket is private, with 50 MB per file and safe file types. The site gets its own key as `INFARIS_STORAGE_URL`, `INFARIS_STORAGE_BUCKET`, `INFARIS_STORAGE_ACCESS_KEY_ID` and the secret `INFARIS_STORAGE_SECRET_ACCESS_KEY`. The browser never gets a key: `app/api/upload/route.ts` checks the user's token and hands out a signed upload URL for one file below `users/<id>/`.

## 6. Realtime on the table

```bash
infaris-guard hosting db realtime enable "$DB" public.todos --wait
infaris-guard hosting db realtime lint "$DB"
```

Realtime only publishes tables with RLS. A change only goes to users whose SELECT policy lets them read the row, so here only to its owner. While Realtime is on, the site gets `INFARIS_REALTIME_URL` and `NEXT_PUBLIC_INFARIS_REALTIME_URL`.

## 7. A function and a schedule

```bash
FN=$(infaris-guard hosting functions create todo-stats --memory 128 --auth-project "$REF" --json)
FN_ID=$(echo "$FN" | jq -r .id)
FN_URL=$(echo "$FN" | jq -r .url)
infaris-guard hosting functions deploy todo-stats functions/todo-stats
KEY=$(infaris-guard hosting auth keys create "$REF" --kind publishable --name todo-stats --json | jq -r .key)
printf %s "$DATA_URL" | infaris-guard hosting functions secrets set todo-stats INFARIS_DATA_URL --value-stdin
printf %s "$KEY" | infaris-guard hosting functions secrets set todo-stats INFARIS_AUTH_KEY --value-stdin
infaris-guard hosting schedules add --function "$FN_ID" /digest "0 7 * * *" --name daily-digest
infaris-guard hosting functions lint
```

The function is private: only a signed-in user of the project (token in `Authorization`) and the schedule (signed by Hosting) get in. It reads the Data API with the user's token, so RLS decides here too. You create the function's key separately; it is shown once. The secrets have the same names as on the site, without `NEXT_PUBLIC_`: Functions only reserves its own names (`INFARIS_FUNCTION_*`, `INFARIS_PLATFORM_*`, `INFARIS_AUTH_ISSUER` and `INFARIS_AUTH_PROJECT_REF`). Secrets apply without a new build.

`hosting schedules` calls a path of a site, app or function on a schedule (formerly `hosting crons`, which still works). `hosting cron` is something else: shell commands in the crontab of a site on its own server.

## 8. The address of the function, and publishing

```bash
infaris-guard hosting env --site "$SITE"
```

The address of the function is public, not a secret; hence `--plain`. `hosting env` shows every name (never the values). They must match the app's `.env.example`. One scheme for every part: `INFARIS_<PART>_URL` for server code, `NEXT_PUBLIC_INFARIS_<PART>_URL` for the browser, and one publishable key. Hosting sets those names itself; `hosting env set` cannot set or change them. After `hosting up`, open `$APP`, request a code with the invited address and sign in.

```bash
printf %s "$FN_URL" | infaris-guard hosting env set NEXT_PUBLIC_STATS_URL --value-stdin --plain --site "$SITE"
infaris-guard hosting up
```

## 9. AI with prompts in the EU (optional)

```bash
GW=$(infaris-guard hosting ai create todo-ai --budget 10 --default-model mistral/mistral-small-latest --json | jq -r .id)
SUGGEST=$(infaris-guard hosting functions create todo-suggest --auth-project "$REF" --json | jq -r .id)
infaris-guard hosting functions deploy todo-suggest functions/todo-suggest
infaris-guard hosting ai link "$GW" --function "$SUGGEST"
infaris-guard hosting ai test "$GW"
```

The AI Gateway is one OpenAI-compatible endpoint with only providers that process in the EU (Mistral, Scaleway, IONOS, OVHcloud), a monthly budget and limits per minute. Prompts and answers are not kept. `hosting ai link` gives the function its own key and sets `OPENAI_BASE_URL` and `OPENAI_API_KEY` (plus `INFARIS_AI_URL` and `INFARIS_AI_KEY`); the same shape as `INFARIS_DATA_URL`, so no clash with the names of the Functions runtime. The function `functions/todo-suggest` asks for a next step for a todo with plain `fetch`; the OpenAI SDK, the AI SDK or LangChain work the same way. See usage per model with `hosting ai usage "$GW" --by model`. More in [AI Gateway](hosting-ai-gateway).

## Confirming

Everything that deletes, replaces or opens something asks for one confirmation: `--yes`, or yes in an interactive terminal. The message says what the action does (delete, replace, open or change). Where you type the name, when deleting or replacing data and when a database is opened to the internet, `--confirm <name>` is the confirmation; `--yes` is then not needed as well. `--dry-run` shows the plan first and asks nothing. The old flags `--confirm-open-signups` and `--confirm-public` still work, with a deprecation notice.

## Opening up

Each of these opens something to more people. They need `--yes`, are logged and the part's linter reports them:

| What | Command |
| --- | --- |
| Anyone may create an account | `hosting auth projects settings "$REF" --signup open --yes` |
| Anonymous reads from a table | `hosting db expose "$DB" ... --roles anon --public --yes` |
| A public bucket | `hosting storage buckets update todo-files --public --yes` |
| Public Realtime channels | `hosting db realtime set "$DB" --public-channels --yes` |
| A public function | `hosting functions expose todo-stats --public --yes` |
| An AI provider outside the EU | `hosting ai providers set "$GW" --providers mistral,scaleway,ionos,ovhcloud,openai --allow-non-eu --yes` |
| Keep prompts and answers | `hosting ai logging "$GW" --on --retention-days 7 --yes` |

Run the lints again after a change: `hosting db lint`, `hosting storage lint`, `hosting db realtime lint` and `hosting functions lint`. Or everything at once, for the whole workspace: `hosting security` shows every finding and everything reachable from outside Infaris, with the command that closes it again. It exits with code 1 on an open critical or high finding (`--strict`: also medium), so you can put it in CI.
