Browse the docs
Platform
Self-hosting & deployment
Run Habitat yourself — requirements, environment variables, Postgres with pgvector, Redis, the background worker and deploying to Vercel.
Habitat is a Next.js (App Router) application with a separate background worker, backed by PostgreSQL with pgvector and, optionally, Redis.
Requirements#
| Component | Notes |
|---|---|
| Node.js 20+ and pnpm | Web app and worker |
PostgreSQL with pgvector, pg_trgm and pgcrypto | Developed against PostgreSQL 18. All tables live in a habitat schema, so the database can be shared safely |
| Redis (optional) | Rate limits and caching. Without it, Habitat falls back to Postgres |
| At least one inference provider key | Experiential Labs, OpenAI, Anthropic, Google or OpenRouter |
Environment variables#
| Variable | Required | Purpose |
|---|---|---|
NEXT_PUBLIC_APP_URL | Yes | Public origin, e.g. https://habitat.example. Also the domain shown in wallet sign-in messages |
DATABASE_URL | Yes | Postgres connection string |
SESSION_SECRET | Yes | Random secret used for privacy-preserving IP hashes |
ENCRYPTION_KEY | Yes | 32 random bytes, base64 — encrypts creator provider keys (openssl rand -base64 32). Rotating it makes saved provider keys unreadable |
REDIS_URL | No | Redis connection string |
EXPLABS_API_KEY, EXPLABS_BASE_URL | One provider | Experiential Labs gateway (OpenAI-compatible) |
OPENAI_API_KEY | One provider | OpenAI |
ANTHROPIC_API_KEY | One provider | Anthropic |
GOOGLE_GENERATIVE_AI_API_KEY | One provider | |
OPENROUTER_API_KEY | One provider | OpenRouter |
SOLANA_RPC_URL | No | Server-side RPC for wallet info and Solana tools (default: public mainnet-beta) |
SOLANA_RPC_URLS | No | Comma-separated RPC endpoints tried in order with automatic failover (on 429/5xx/timeouts). On mainnet, PublicNode's free endpoint is always added as a last fallback |
CRON_SECRET | For serverless jobs | Bearer token for /api/cron/jobs, the serverless job runner (see below) |
DB_PREPARE | No | Set false behind a transaction-mode pooler (auto-detected for port 6543) |
NEXT_PUBLIC_SOLANA_RPC_URL, NEXT_PUBLIC_SOLANA_NETWORK | No | Browser wallet connection RPC and cluster (mainnet-beta, devnet, testnet) |
ADMIN_EMAILS, ADMIN_WALLETS | No | Comma-separated lists; matching accounts become admins when they sign in |
STORAGE_DRIVER | No | db (default — files in Postgres) or fs (./.data/storage, needs a persistent disk) |
DB_POOL_MAX, WORKER_POLL_MS | No | Connection pool size (default 10) and worker poll interval (default 1000 ms) |
TEST_DATABASE_URL | No | Database used by the automated tests |
Providers without a key simply don't appear (creators can still add their own keys for them).
First run#
pnpm install
pnpm db:migrate # applies db/migrations/*.sql in order, once each
pnpm dev # web app on :8800
pnpm worker # background jobs: ingestion, crawling, embeddings, model sync
Then sign up and make yourself an admin:
pnpm make-admin you@example.com # or a Solana wallet address, a username or a user id
pnpm make-admin you@example.com --revoke # remove the role again
Open /admin, press Sync now on the Models page to pull the model catalogue, choose the default and fallback models, and review the limits and budget under Limits & flags.
For local development, node scripts/stack.mjs up starts a private Postgres (with pgvector) and Redis under ./.data on ports 58800 and 58801.
Background jobs#
Long-running work never runs inside a user's request. Jobs live in a Postgres queue (FOR UPDATE SKIP LOCKED, so any number of runners can work it safely): knowledge ingestion and crawling, embeddings, the model-catalogue sync (every six hours), conversation titles and housekeeping (expired nonces and rate counters). Runners report a heartbeat that the admin overview and the knowledge screens show; without one, knowledge stays queued. There are two ways to run them:
- Long-running worker:
pnpm workeron any always-on machine (VM, container, your laptop in development). - Serverless runner:
POST /api/cron/jobswithAuthorization: Bearer $CRON_SECRET. It answers202at once and keeps working the queue for up to about four minutes (after()), so call it every minute from any scheduler. With Supabase, the database can call it itself:
create extension if not exists pg_cron with schema pg_catalog;
create extension if not exists pg_net with schema extensions;
select vault.create_secret('<CRON_SECRET>', 'habitat_cron_secret');
select cron.schedule('habitat-jobs', '* * * * *', $$
select net.http_post(
url := 'https://<your-domain>/api/cron/jobs',
headers := jsonb_build_object('authorization', 'Bearer ' ||
(select decrypted_secret from vault.decrypted_secrets where name = 'habitat_cron_secret')),
body := '{}'::jsonb);
$$);
Deploying to Vercel#
- Deploy the Next.js app to Vercel and set the environment variables above.
- Use a managed Postgres that supports
pgvectorand runpnpm db:migrateagainst it from your machine or CI (use a session-mode or direct connection for migrations). - Point
DATABASE_URLat a transaction-mode pooler for serverless (Supabase port 6543). Habitat then turns off prepared statements and query pipelining automatically (pipelined queries stall in transaction-mode poolers). Transaction poolers drop per-connection settings, so set the schema search path on the role once:alter role <db user> set search_path = habitat, public, extensions; - Use
STORAGE_DRIVER=db(Vercel has no persistent disk), or swap in object storage. - Run background jobs with the serverless runner (above) or a long-running
pnpm workerelsewhere. - Optionally add a hosted Redis and set
REDIS_URL. - Set
NEXT_PUBLIC_APP_URLto your production origin before anyone signs in with a wallet.
Tests#
pnpm test # vitest — unit and integration tests (integration tests use TEST_DATABASE_URL)
Backups and upgrades#
Back up the database regularly (it contains users, agents, knowledge, conversations and — with STORAGE_DRIVER=db — uploaded files). Keep ENCRYPTION_KEY safe and backed up separately. New releases add migrations; run pnpm db:migrate before starting the new version.