# Functions overview

Source: https://cubibase.com/docs/functions

Also available: [Markdown version](https://cubibase.com/docs/functions.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

TypeScript and JavaScript functions that run in an isolated managed runtime — never inside the CubiBase engine. Four trigger types, one execution pipeline, one set of quotas and logs.

## What CubiBase Functions are

A CubiBase Function is your code, deployed as an immutable version to a managed Node.js 22 runtime (AWS Lambda in production; an isolated child process in local development). The engine manages deployments, triggers, secrets and metering, but never executes your code in its own process.

> **Not SQL Functions**: PostgreSQL SQL/PLpgSQL functions run inside your project database and are managed under Database → SQL Functions. CubiBase Functions are separate: TypeScript/JavaScript in the managed runtime, managed under Functions. Database Event Triggers connect the two worlds — they invoke a CubiBase Function after a committed database change.

| Trigger | When it runs | Delivery |
| --- | --- | --- |
| HTTP | A request hits https://{slug}.api.cubibase.com/functions/{name} | Synchronous request/response |
| Database | After an INSERT, UPDATE or DELETE commits on a subscribed table | Async, at-least-once, retried |
| Storage | After an object is created, updated or deleted in a subscribed bucket | Async, at-least-once, retried |
| Scheduled | A cron expression fires (UTC or a configured timezone) | Async, at-least-once, retried |

All four categories produce the same invocation envelope and pass through the same authorization, quota reservation, concurrency control, metering and logging. There is no per-trigger side channel that skips enforcement.

## The programming model

Default-export an async handler `(request, ctx)`. Return a web `Response`, a `{ status, headers, body }` object, a plain object (serialized as JSON), a string, or nothing (204).

**index.ts**

```text
// index.ts — default-export an async (request, ctx) handler.
export default async function handler(request: any, ctx: any) {
  const name = request.query.name ?? 'world';
  console.log('Saying hello to', name);
  return new Response(JSON.stringify({
    message: `Hello, ${name}!`,
    invocationId: ctx.invocationId,
  }), { status: 200, headers: { 'content-type': 'application/json' } });
}
```

| On `request` | Meaning |
| --- | --- |
| `method`, `url`, `headers`, `query` | The HTTP request as received by the gateway |
| `body` | A Buffer with the EXACT raw request bytes (HTTP triggers) — safe for signature verification |
| `json()` / `text()` | Parse helpers over the raw body |
| `event` | The structured payload for database, storage and scheduled triggers |
| `trigger` | `http`, `database`, `storage`, `schedule` or `test` |
| `auth` | The verified caller context (kind and user id, when authenticated) |
| `invocationId`, `correlationId` | Identifiers that appear in Executions & Logs |

| On `ctx` | Meaning |
| --- | --- |
| `ctx.db.from(table)` | Scoped select / insert / update / delete with equality filters, through the project’s own roles and policies. JWT-authenticated HTTP calls run under the caller’s row-level security; trusted database, storage and scheduled invocations run with the project’s developer-level access |
| `ctx.db.query(sql, params)` | Read-only parameterized SQL through the project’s SQL role |
| `ctx.storage.from(bucket)` | upload, download, getMetadata, remove, list — through the same Storage quotas and events |
| `ctx.env` | Your environment variables and secrets for this deployment (frozen snapshot) |
| `ctx.log`, `console.*` | Captured into the bounded, redacted execution log |
| `ctx.waitUntil(promise)` | Background work with at most 5 extra seconds after the response |

> **note**: The runtime holds no AWS credentials, no database passwords and no other project’s data. The only platform credential is a token scoped to this single invocation, valid for minutes. Database and storage access goes through scoped CubiBase service APIs.

## Deployments and versions

- Editing source in the Console changes the working copy only. Deploy builds and publishes an immutable version.
- Bundling uses esbuild to transform your code — build scripts are never executed and no per-deploy npm install runs.
- Imports are limited to Node built-ins and a pinned dependency allowlist (currently the official Stripe SDK). Other imports fail the build with a clear error.
- Artifacts are stored in a private S3 bucket; the database keeps only references and checksums.
- A successful deploy atomically switches the active pointer. A failed deploy never interrupts the version that is serving.
- Activate any earlier ready version to roll back.

## In the Console

Open a project and choose Functions. My Functions lists every function with its triggers, deployment state and error rate. The editor has tabs for Source, Triggers, Security & Config, Secrets & Env, Deployments, Test and Logs. Executions & Logs shows every invocation across all trigger types; Usage & Quotas shows consumption against your plan.

- [HTTP functions](https://cubibase.com/docs/functions/http) Endpoints, auth modes and calling conventions.
- [Database events](https://cubibase.com/docs/functions/database-events) After-commit triggers with an outbox.
- [Storage events](https://cubibase.com/docs/functions/storage-events) React to object lifecycle changes.
- [Scheduled functions](https://cubibase.com/docs/functions/schedules) Cron without a scheduler of your own.

## Additional resources

- [Functions feature page](https://cubibase.com/features/functions) The product-level story.
- [Templates](https://cubibase.com/docs/functions/templates) Eight working starting points.
- [Quotas and metering](https://cubibase.com/docs/functions/quotas) What is counted and what happens at a limit.
