|
| 1 | +--- |
| 2 | +title: Project-Scoped Routing |
| 3 | +description: Enable multi-project isolation at the URL level — /api/v1/projects/:projectId/... — with REST, Client SDK, and dispatcher support. |
| 4 | +--- |
| 5 | + |
| 6 | +# Project-Scoped Routing |
| 7 | + |
| 8 | +ObjectStack's project-scoping feature puts each project (the "workspace" or "base" in Airtable / "environment" in traditional stacks) into its own URL segment. When enabled, every data, metadata, automation, and AI request runs through `/api/v1/projects/:projectId/...` instead of relying on hostname / header / session resolution alone. |
| 9 | + |
| 10 | +This gives you: |
| 11 | + |
| 12 | +- **Explicit tenancy in URLs** — no more guessing what project a request targets. |
| 13 | +- **Safer client code** — `client.project(id).data.find(...)` cannot be confused with the default project. |
| 14 | +- **Future foundation for RBAC** — the project is in the URL, so middleware can enforce access before any handler runs. |
| 15 | +- **Incremental rollout** — the `'auto'` resolution strategy mounts both scoped and unscoped routes, so you can migrate clients on your own schedule. |
| 16 | + |
| 17 | +## Server configuration |
| 18 | + |
| 19 | +Enable scoping in your `objectstack.config.ts`: |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { defineStack } from '@objectstack/spec'; |
| 23 | + |
| 24 | +export default defineStack({ |
| 25 | + // ... rest of your stack |
| 26 | + api: { |
| 27 | + enableProjectScoping: true, |
| 28 | + projectResolution: 'auto', // 'required' | 'optional' | 'auto' |
| 29 | + }, |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +### Resolution strategies |
| 34 | + |
| 35 | +| Strategy | Behavior | When to use | |
| 36 | +| ------------ | ------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------- | |
| 37 | +| `'auto'` | Registers **both** `/api/v1/data/:object` and `/api/v1/projects/:projectId/data/:object`. | Default. Zero risk during migration — old and new clients both work. | |
| 38 | +| `'optional'` | Same as `auto` today; reserved so a future release can require one of URL / header / session to be present. | Same as `auto`. | |
| 39 | +| `'required'` | Only scoped routes are registered. Unscoped requests return 404. | Production hardening after all clients have migrated. | |
| 40 | + |
| 41 | +The setting also propagates to: |
| 42 | + |
| 43 | +- **Dispatcher plugin** — AI (`/ai/*`) and automation (`/automation/*`) routes gain scoped variants. |
| 44 | +- **Package management** — `/api/v1/projects/:projectId/packages` mirrors `/api/v1/packages`. |
| 45 | +- **Discovery** — the `/api/v1/discovery` response includes a `scoping` block so clients can detect the mode. |
| 46 | + |
| 47 | +## Client SDK |
| 48 | + |
| 49 | +The client exposes a `project(id)` factory that returns a sub-client wired to the scoped URL prefix: |
| 50 | + |
| 51 | +```typescript |
| 52 | +import { ObjectStackClient } from '@objectstack/client'; |
| 53 | + |
| 54 | +const client = new ObjectStackClient({ baseUrl: 'https://api.example.com' }); |
| 55 | + |
| 56 | +const scoped = client.project('00000000-0000-0000-0000-000000000001'); |
| 57 | + |
| 58 | +// All calls hit /api/v1/projects/:projectId/... |
| 59 | +const tasks = await scoped.data.find('task', { top: 20 }); |
| 60 | +const objects = await scoped.meta.getItems('object'); |
| 61 | +const pkg = await scoped.packages.get('my-package'); |
| 62 | +``` |
| 63 | + |
| 64 | +The top-level `client.data.*`, `client.meta.*`, `client.packages.*` namespaces continue to work unchanged — they hit the unscoped routes, falling back to hostname / `X-Project-Id` header / session resolution exactly as before. |
| 65 | + |
| 66 | +You can switch between projects without rebuilding the client: |
| 67 | + |
| 68 | +```typescript |
| 69 | +const projectA = client.project('proj-a'); |
| 70 | +const projectB = client.project('proj-b'); |
| 71 | + |
| 72 | +await Promise.all([ |
| 73 | + projectA.data.find('task'), |
| 74 | + projectB.data.find('task'), |
| 75 | +]); |
| 76 | +``` |
| 77 | + |
| 78 | +## Request resolution order |
| 79 | + |
| 80 | +When `enableProjectScoping: true`, `HttpDispatcher.resolveEnvironmentContext` resolves the project in this order: |
| 81 | + |
| 82 | +1. **URL path parameter** — `/api/v1/projects/:projectId/...`. Highest priority. |
| 83 | +2. **Hostname** — `acme-dev.objectstack.app`. |
| 84 | +3. **`X-Project-Id` header** — the client SDK sets this automatically if you pass `projectId` in `ClientConfig`. |
| 85 | +4. **Session** — `session.activeProjectId` (or legacy `activeEnvironmentId`). |
| 86 | +5. **Default project for active organization** — falls back to the org's default. |
| 87 | + |
| 88 | +Under `'required'` mode, the URL path is the only supported source; the other fallbacks are bypassed for data/meta/AI/automation routes. |
| 89 | + |
| 90 | +## System project |
| 91 | + |
| 92 | +Every deployment has a built-in system project at the well-known UUID `00000000-0000-0000-0000-000000000001`. It's used for platform infrastructure (system packages, platform-level flows, etc.) and is created automatically on first startup if you register the plugin: |
| 93 | + |
| 94 | +```typescript |
| 95 | +import { createSystemProjectPlugin } from '@objectstack/runtime'; |
| 96 | + |
| 97 | +kernel.use(tenantPlugin); |
| 98 | +kernel.use(createSystemProjectPlugin()); |
| 99 | +``` |
| 100 | + |
| 101 | +The plugin is idempotent — it's safe to call `provisionSystemProject()` on every boot. Subsequent calls return the existing row. |
| 102 | + |
| 103 | +## Control-plane routes remain unscoped |
| 104 | + |
| 105 | +Control-plane endpoints always live under their unscoped paths: |
| 106 | + |
| 107 | +- `/api/v1/auth/*` |
| 108 | +- `/api/v1/cloud/projects` (CRUD on the project list itself) |
| 109 | +- `/api/v1/cloud/organizations` |
| 110 | +- `/api/v1/health` |
| 111 | +- `/api/v1/discovery` |
| 112 | +- `/.well-known/objectstack` |
| 113 | + |
| 114 | +These are not affected by `enableProjectScoping` because they operate on the control plane, not a specific project's data plane. |
| 115 | + |
| 116 | +## Migration checklist |
| 117 | + |
| 118 | +1. ✅ Upgrade to `@objectstack/core@≥4.x` with the Phase 2 runtime. |
| 119 | +2. ✅ Add `enableProjectScoping: true, projectResolution: 'auto'` to your stack config. |
| 120 | +3. ✅ Register `createSystemProjectPlugin()` in your kernel wiring. |
| 121 | +4. ⏳ Update callers that hard-code `/api/v1/data/...` to use `client.project(id)` or keep them as-is under `auto` mode. |
| 122 | +5. ⏳ Once all callers have migrated, flip `projectResolution` to `'required'` and remove the unscoped code paths. |
| 123 | + |
| 124 | +Steps 4 and 5 are intentionally separate — you can ship Step 3 today and migrate clients over weeks or months. |
| 125 | + |
| 126 | +## Related reading |
| 127 | + |
| 128 | +- [ADR-0002 — Project-per-database isolation](/docs/adr/0002-environment-database-isolation) |
| 129 | +- [Client SDK](/docs/guides/client-sdk) |
| 130 | +- [Authentication](/docs/guides/authentication) |
0 commit comments