Skip to main content
The ctx object is the second parameter of every handler. It contains info about the company, bot, user, trigger type, and provides access to secrets, messaging, memory, and logging.
handler: async (input, ctx, request) => {
  ctx.log("Request received", {
    company: ctx.company.name,
    bot: ctx.bot.channel,
    user: ctx.user.id,
    trigger: ctx.trigger.type,
  });
}

Reference

PropertyTypeDescription
ctx.functionSlugstringFunction slug
ctx.company{ id, name }Company of the request
ctx.bot{ id, name, channel }Bot that originated the request
ctx.user{ id, names?, roomId? }Conversation user
ctx.conversation{ id?, ... }Conversation data
ctx.operator{ id?, ... }Assigned operator
ctx.triggerTriggerInfoTrigger type (http, cron, event)
ctx.envEnvAccessorAccess to secrets
ctx.paramsRecord<string, string>Route parameters
ctx.queryRecord<string, string>Query string params
ctx.jelouJelouSDKWhatsApp messaging client
ctx.memoryMemorySDKKey-value session memory
ctx.methodstringHTTP method (GET, POST, etc.)
ctx.pathstringRequest path
ctx.requestIdstringUnique request UUID
ctx.isCronbooleantrue if cron trigger
ctx.isEventbooleantrue if event trigger
ctx.isHttpbooleantrue if HTTP request
ctx.skillIdstring | nullBrain Studio skill ID
ctx.executionIdstring | nullBrain Studio execution ID
ctx.log()(...args) => voidStructured logger

Identity

handler: async (input, ctx) => {
  ctx.company.id;    // 42
  ctx.company.name;  // "Shop ABC"
  ctx.bot.id;        // "bot-123"
  ctx.bot.channel;   // "whatsapp"
  ctx.user.id;       // 99
  ctx.user.names;    // "Maria Garcia" (optional)
}

Trigger

if (ctx.isHttp) {
  ctx.trigger; // { type: "http" }
}

Request

export default define({
  config: { path: "/users/:id" },
  handler: async (input, ctx) => {
    ctx.method;      // "GET"
    ctx.path;        // "/users/42"
    ctx.params.id;   // "42"
    ctx.query.format // "json" (from ?format=json)
    ctx.requestId;   // "a1b2c3d4-..."
  },
});

Environment (secrets)

const apiKey = ctx.env.get("CRM_API_KEY"); // string | undefined
ctx.env.has("WEBHOOK_SECRET");              // boolean
ctx.env.toObject();                         // Record<string, string>
Internal variables with __FN_ prefix are blocked — ctx.env.get("__FN_COMPANY_ID") returns undefined.

Messaging (ctx.jelou)

if (ctx.jelou.available) {
  await ctx.jelou.send({ type: "text", to: "+593987654321", text: "Hello" });
}

Messaging guide

14 message types, HSM templates, and error handling.

Memory (ctx.memory)

if (ctx.memory.available) {
  const step = await ctx.memory.get("step", "start");
  await ctx.memory.set("step", "confirm", 3600);
}

Memory guide

Primitives, JSON, TTL, limits, and common patterns.

Logging

ctx.log() writes structured JSON with automatic metadata:
ctx.log("Processing order", { phone: input.phone });
// stdout: { "requestId": "...", "function": "my-fn", "company": 42, "timestamp": "...", "args": [...] }

Messaging

Send WhatsApp with ctx.jelou.

Memory

Persist data with ctx.memory.

Secrets

Encrypted environment variables.

Authentication

Runtime tokens and public functions.