CADPrk6a…krfpump
habitat
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#

ComponentNotes
Node.js 20+ and pnpmWeb app and worker
PostgreSQL with pgvector, pg_trgm and pgcryptoDeveloped 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 keyExperiential Labs, OpenAI, Anthropic, Google or OpenRouter

Environment variables#

VariableRequiredPurpose
NEXT_PUBLIC_APP_URLYesPublic origin, e.g. https://habitat.example. Also the domain shown in wallet sign-in messages
DATABASE_URLYesPostgres connection string
SESSION_SECRETYesRandom secret used for privacy-preserving IP hashes
ENCRYPTION_KEYYes32 random bytes, base64 — encrypts creator provider keys (openssl rand -base64 32). Rotating it makes saved provider keys unreadable
REDIS_URLNoRedis connection string
EXPLABS_API_KEY, EXPLABS_BASE_URLOne providerExperiential Labs gateway (OpenAI-compatible)
OPENAI_API_KEYOne providerOpenAI
ANTHROPIC_API_KEYOne providerAnthropic
GOOGLE_GENERATIVE_AI_API_KEYOne providerGoogle
OPENROUTER_API_KEYOne providerOpenRouter
SOLANA_RPC_URLNoServer-side RPC for wallet info and Solana tools (default: public mainnet-beta)
SOLANA_RPC_URLSNoComma-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_SECRETFor serverless jobsBearer token for /api/cron/jobs, the serverless job runner (see below)
DB_PREPARENoSet false behind a transaction-mode pooler (auto-detected for port 6543)
NEXT_PUBLIC_SOLANA_RPC_URL, NEXT_PUBLIC_SOLANA_NETWORKNoBrowser wallet connection RPC and cluster (mainnet-beta, devnet, testnet)
ADMIN_EMAILS, ADMIN_WALLETSNoComma-separated lists; matching accounts become admins when they sign in
STORAGE_DRIVERNodb (default — files in Postgres) or fs (./.data/storage, needs a persistent disk)
DB_POOL_MAX, WORKER_POLL_MSNoConnection pool size (default 10) and worker poll interval (default 1000 ms)
TEST_DATABASE_URLNoDatabase 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 worker on any always-on machine (VM, container, your laptop in development).
  • Serverless runner: POST /api/cron/jobs with Authorization: Bearer $CRON_SECRET. It answers 202 at 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 pgvector and run pnpm db:migrate against it from your machine or CI (use a session-mode or direct connection for migrations).
  • Point DATABASE_URL at 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 worker elsewhere.
  • Optionally add a hosted Redis and set REDIS_URL.
  • Set NEXT_PUBLIC_APP_URL to 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.