Skip to main content
ctx is the entire platform surface a server-side handler gets. There is no raw database client, no Supabase, no request object. Every field is scoped and authorized before your code runs.

The context object

Storage

Access to schema-validated per-account and per-user state:
All three are async. get returns null for missing keys and expired entries. User scope throws for machine actors.

Secrets

Named credentials resolved by the host across its tiers:
set() writes only to the account tier for this {account, product}. Source- and global-tier credentials are admin-issued and throw SecretAccessError if you try to write them.

Fetch

HTTP client that refuses anything outside your egress allowlist:
Denied calls throw EgressDeniedError synchronously, before opening a socket. Redirect targets are also checked.

Logger

Structured logging with pre-bound context:
The first argument is a context object (pino-style), the second is the message. The host pre-binds {integration, accountId, actor} to every line.

Actor

Who is calling — a human user or a machine (a team workflow). Personal workflows and agents run as the user:
Machine actors cannot use user-scoped storage — every ctx.storage.user call throws.

Gating writes on the caller’s role

The host runs every action with the account’s credential, so without a check any member can do whatever that credential allows. A user actor carries role: { name, hierarchyLevel } | null, the caller’s role in the account (1 owner, 2 admin, 3 member). Gate a write with actorIsAtLeast:
The role is present on every user-initiated call: actions, the assistant, MCP, and workflows or agents that run on behalf of a user. Scheduled and account-mode workflow calls run as a machine actor with no role, so actorIsAtLeast refuses them. It also refuses a user whose role the host could not resolve. In tests, createMockContext() defaults to an owner; pass actor with a lower role to exercise the gate.

Identity fields

All read-only and already authorized:
No id parameters to get wrong, and none to lie in. The platform fills all of these before your code runs. ctx.connection.owner is 'user' when the context is one person’s own tier: an experience invoker call, or the onTokens / onActivate of a user-level credential being connected. There ctx.secrets is that person’s tier, plus the app’s admin-issued client credentials. ctx.connection.owner is 'source' only when a provided agent runs on a connection a Source admin made for the whole Source. Then ctx.routing names the routing key and the values routed to the team the call is for, and your tools must stay inside them. See Routing and Source connections.

Principal and identity

ctx.principal says who the call runs for. The host sets it from the session, a run’s stored invoker or a personal workflow’s owner, never from a request body:
displayName and avatarUrl are for attribution and may be null. There is no email. Key your data on userId. ctx.identity is the calling person’s own credential tier, { userId, secrets }. It is set only when the principal is a person who has connected a user-level credential and the Source serves the level. It is never set for a machine principal (team workflow, schedule, webhook, health check) or for a call made for someone else (a workflow in agent mode, a managed agent). identity.secrets holds only that person’s values, and set writes back to it.
ctx.secrets stays the team tier. When you act as the team for a person, attribute the write to ctx.principal where the vendor allows it. See Credential levels.

Common patterns

Logging with context

Always include relevant IDs in the context object:

Handling missing credentials

Not configured is an expected state — log and skip, don’t fail:

Error handling

Log before throwing so errors reach your observability:

Where ctx is available

  • Tools — tools[].run(ctx, args)
  • Webhooks — webhooks[].handler(ctx, event)
  • Schedules — schedules[].handler(ctx, invocation)
  • Activation hooks — onActivate(ctx)
  • Actions — actions[name].handler(ctx, input)
  • OAuth — oauth.onTokens(ctx, tokens)
Every handler receives the same shape, scoped to the same {account, integration}.

Next steps

  • Read about Storage to understand schema validation and expiry
  • Learn Secrets for credential management
  • Explore Tools for assistant-callable functions