Low-level requests to a Supabase project.

Per-call credentials. Every API-calling function in this package takes an optional trailing opts Map, resolved by resolve: opts first, then context, then the environment. Nothing from opts is written to ctx, so one run can reach many projects and parallel flows don't share credentials.

  • url: the project URL
  • anon-key (or api-key): the anon/public key
  • service-key (or service-role-key): the service role key

A url or key in opts means a different project or role, so context and environment keys are ignored for that call: a project's key is never sent to another project's URL. A url left out of opts still comes from context. One of those keys present but blank or null is an error, never a fallback to context.

Values

OPTS_KEYS

OPTS_KEYS: Vec [
  "url",
  "anon-key",
  "api-key",
  "service-key",
  "service-role-key"
]

Functions

blank-credential

fn (opts: Map, names: Vec): Str?

The first of names that opts has but with a blank or null value, else null. Such a key is an error, never a fallback to context: a tenant whose key is missing must not run on the project's credentials.

Example

opts {url: "https://x.supabase.co", anon-key: ""}
::supabase::api/blank-credential(opts, ["url", "anon-key"])  // "anon-key"
::supabase::api/blank-credential(opts, ["url"])  // null

body-or-error

fn (response: Result): Any

The body of a response. A non-2xx status becomes an Err carrying an HttpError, and an Err passes through. Pair it with the request-raw family when you want both.

Example

::api ::supabase::api

found ::hot::http/HttpResponse({status: 200, headers: {}, body: [{id: 1}]})
::api/body-or-error(found)  // [{id: 1}]

missing ::hot::http/HttpResponse({status: 404, headers: {}, body: {message: "Not found"}})
is-err(::api/body-or-error(missing))  // true

build-request

fn (method: Str, path: Str, additional-headers: Map, body: Any, apikey: Str, bearer-token: Str, base-url: Str): ::hot::http/HttpRequest

The outgoing HttpRequest: apikey and Authorization: Bearer headers, additional-headers on top, and Content-Type only with a body. Pure; exposed for testing.

Example

::api ::supabase::api

req ::api/build-request("POST", "/rest/v1/todos", {Prefer: "return=minimal"}, {title: "Ship"}, "anon", "user-jwt", "https://x.supabase.co")
req.url  // "https://x.supabase.co/rest/v1/todos"
req.headers.Authorization  // "Bearer user-jwt"
req.body  // "{\"title\":\"Ship\"}"

credentials-for

fn (resolved: Map, role: Str): Map

The {url, key} a role sends, from resolved credentials (pure): role is "anon" or "service". An Err when that role's key is missing.

Example

::api ::supabase::api

resolved {url: "https://x.supabase.co", anon-key: "anon"}
::api/credentials-for(resolved, "anon")  // {url: "https://x.supabase.co", key: "anon"}
is-err(::api/credentials-for(resolved, "service"))  // true

request

fn (method: Str, path: Str): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): Any

Make an authenticated request to a Supabase project API. Sends the anon key as both the apikey header and the default Bearer token.

Use request-as instead when you need to act as a specific user (with their access token).

Example

::api ::supabase::api

// GET request
response ::api/request("GET", "/rest/v1/users?select=id,name")

// POST request with body
response ::api/request("POST", "/rest/v1/users", {Prefer: "return=representation"}, {name: "Alice"})

// Per-call credentials: another project, for this call only
tenant {url: "https://tenant-b.supabase.co", anon-key: "tenant-b-anon"}
response ::api/request("GET", "/rest/v1/users?select=id", {}, {}, tenant)
Context Vars: supabase.url, supabase.anon.key

request-as

fn (method: Str, path: Str, additional-headers: Map, body: Any, access-token: Str): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any, access-token: Str, opts: Map): Any

Make a request as a specific authenticated user. The anon key is still sent as the apikey header, but the user's access token is used as the Bearer token.

Example

::api ::supabase::api

token ::hot::ctx/get("supabase.user.token")

// Get the current user's profile
response ::api/request-as("GET", "/rest/v1/profiles?select=*", {}, {}, token)

request-raw

fn (method: Str, path: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse

Make an anon Supabase project API request and return the full HttpResponse including status and headers.

Example

::api ::supabase::api

response ::api/request-raw("GET", "/rest/v1/users?select=id")
response.status

// With headers and a body
created ::api/request-raw("POST", "/rest/v1/users", {Prefer: "return=representation"}, {name: "Alice"})
created.body
Context Vars: supabase.url, supabase.anon.key

request-raw-as

fn (method: Str, path: Str, additional-headers: Map, body: Any, access-token: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, access-token: Str, opts: Map): ::hot::http/HttpResponse

Make a request as a specific user and return the full HttpResponse.

Example

::api ::supabase::api

token ::hot::ctx/get("supabase.user.token")
response ::api/request-raw-as("GET", "/rest/v1/profiles?select=*", {}, {}, token)
response.status
response.body

request-raw-body

fn (method: Str, path: Str, additional-headers: Map, body-raw: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body-raw: Str, opts: Map): ::hot::http/HttpResponse

Make an anon request with an already-encoded raw body. Use for storage uploads and non-JSON payloads.

Example

::api ::supabase::api

response ::api/request-raw-body("POST", "/storage/v1/object/notes/todo.txt", {Content-Type: "text/plain"}, "Buy milk")
response.status

request-raw-body-as

fn (method: Str, path: Str, additional-headers: Map, body-raw: Str, access-token: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body-raw: Str, access-token: Str, opts: Map): ::hot::http/HttpResponse

Make a user-authenticated request with an already-encoded raw body.

Example

::api ::supabase::api

token ::hot::ctx/get("supabase.user.token")
response ::api/request-raw-body-as("POST", "/storage/v1/object/notes/todo.txt", {Content-Type: "text/plain"}, "Buy milk", token)
response.status

request-raw-body-service

fn (method: Str, path: Str, additional-headers: Map, body-raw: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body-raw: Str, opts: Map): ::hot::http/HttpResponse

Make a service-role request with an already-encoded raw body.

Example

::api ::supabase::api

response ::api/request-raw-body-service("POST", "/storage/v1/object/notes/todo.txt", {Content-Type: "text/plain"}, "Buy milk")
response.status

request-raw-service

fn (method: Str, path: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse

Make a service-role request and return the full HttpResponse.

Example

::api ::supabase::api

response ::api/request-raw-service("GET", "/auth/v1/admin/users")
response.status
response.body
Context Vars: supabase.url, supabase.service.key

request-raw-with-keys

fn (method: Str, path: Str, additional-headers: Map, body: Any, apikey: Str, bearer-token: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, apikey: Str, bearer-token: Str, project-url: Str): ::hot::http/HttpResponse

Make a Supabase request with explicit API and bearer keys and return the full HttpResponse.

Example

::api ::supabase::api

anon ::hot::ctx/get("tenant.anon.key")

response ::api/request-raw-with-keys("GET", "/rest/v1/users?select=id", {}, {}, anon, anon, "https://tenant-b.supabase.co")
response.status

request-service

fn (method: Str, path: Str): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): Any

Make a request using the service role key (bypasses RLS). Used internally by auth-admin and other admin modules.

Example

::api ::supabase::api

// Every row, regardless of row level security
users ::api/request-service("GET", "/rest/v1/users?select=id,email")

// Another project, for this call only
tenant {url: "https://tenant-b.supabase.co", service-key: ::hot::ctx/get("tenant.service.key")}
tenant-users ::api/request-service("GET", "/rest/v1/users?select=id", {}, {}, tenant)
Context Vars: supabase.url, supabase.service.key

request-with-keys

fn (method: Str, path: Str, additional-headers: Map, body: Any, apikey: Str, bearer-token: Str): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any, apikey: Str, bearer-token: Str, project-url: Str): Any

Make a Supabase request with explicit API and bearer keys. Useful for tests and scripts that do not use context.

Example

::api ::supabase::api

anon ::hot::ctx/get("tenant.anon.key")

// Project URL from context
users ::api/request-with-keys("GET", "/rest/v1/users?select=id", {}, {}, anon, anon)

// Explicit project URL
tenant-users ::api/request-with-keys("GET", "/rest/v1/users?select=id", {}, {}, anon, anon, "https://tenant-b.supabase.co")

resolve

fn (opts: Map): Map

Credentials for one call (see resolve-from). opts values are never written to ctx. A credential from opts is masked in the run's call records.

Example

tenant {url: "https://tenant-b.supabase.co", anon-key: "tenant-b-anon"}
::supabase::api/resolve(tenant).url  // "https://tenant-b.supabase.co"
Context Vars: supabase.url, supabase.anon.key, supabase.api.key, supabase.service.key, supabase.service.role.key

resolve-from

fn (opts: Map, sources: Map): Map

The one resolver, pure: {url, anon-key?, service-key?} for a call from opts first, then sources (see ::supabase::core/sources-from). A url, anon-key, api-key, service-key or service-role-key in opts means no key comes from sources. An Err when no URL is set, or when one of those keys is in opts but blank or null: that never falls back to context, or a tenant could run on the project's credentials. Exposed for testing; resolve feeds it the context and environment.

Example

::api ::supabase::api

sources {url: "https://main.supabase.co", anon-key: "main-anon", service-key: "main-service"}

::api/resolve-from({}, sources).anon-key  // "main-anon"

// A url in opts means no key comes from sources
::api/resolve-from({url: "https://other.supabase.co"}, sources)  // {url: "https://other.supabase.co"}

is-err(::api/resolve-from({anon-key: ""}, sources))  // true

Types

AuthMode

AuthMode type "anon" | "user" | "service"

HttpError

HttpError type {
    status: Int,
    headers: Map,
    body: Any
}