Skip to main content

Data API

Every project has a Data API: a PostgREST service in front of the project database, reachable from the app's own origin at /_tc/db/*. PostgREST is the engine under Supabase's REST API, so Supabase query syntax works unchanged, and agents already know it.

Use it from the SDK​

import { tc } from '@tenantcloud/sdk'

// Read with filters, ordering and embedded relations
const { data, error } = await tc.db.from('expenses')
.select('id, amount, note, approver(name)')
.eq('status', 'open')
.gte('amount', 100)
.order('created_at', { ascending: false })
.limit(50)

// Write
await tc.db.from('expenses').insert({ amount: 120, note: 'Team lunch' })
await tc.db.from('expenses').update({ status: 'approved' }).eq('id', 7)
await tc.db.from('expenses').delete().eq('id', 7)

Errors keep postgrest-js's { data, error } shape. error.hint carries a fix, for example which policy is missing.

Use it over HTTP​

The SDK is a convenience; the API is plain HTTP on the app's origin:

GET /_tc/db/expenses?select=id,amount,note&status=eq.open&order=created_at.desc&limit=50
POST /_tc/db/expenses
Content-Type: application/json

{ "amount": 120, "note": "Team lunch" }

Anything PostgREST supports works here, including filters, embedding, and calling SQL functions under /_tc/db/rpc/<function>.

Security model​

PropertyDetail
AuthenticationEvery request needs a signed-in session. Without one, browsers are redirected to sign-in and API calls get 401
Access checkThe person must be allowed to open the app; others get 403
IdentityThe Edge sends the person's signed token to PostgREST, which verifies it against the Hub's keys with audience tc:app:<project>
RoleRequests run as tc_user; row-level security policies apply
Anonymous accessNone. There is no anonymous role and no anon key
Keys in the browserNone. The browser calls its own origin; the Edge adds identity

Server-side​

On the server, use tc.db.asUser(req).from(...) (or install tc.middleware() so the current request is implicit). The SDK reaches the Data API at TC_POSTGREST_URL with the person's identity. Calling tc.db.from() on the server without a request answers NO_REQUEST.

Limits​

  • The Data API exists once the project has its first deployment.
  • Schema changes are picked up automatically after migrations and Studio changes.
  • The Data API is stopped while a project is archived or disabled.
Was this page helpful?