> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jelou.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Context

> Complete reference for the ctx object available in every handler: company, channel, user, trigger, environment, messaging, memory, and more.

The `ctx` object is the second parameter of every handler. It contains info about the company, channel, user, trigger type, and provides access to secrets, messaging, memory, and logging.

```typescript theme={null}
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

| Property | Type | Description |
| - | - | - |
| `ctx.functionSlug` | `string` | Function slug |
| `ctx.company` | `{ id, name }` | Company of the request |
| `ctx.bot` | `{ id, name, channel }` | Channel that originated the request |
| `ctx.user` | `{ id, names?, roomId? }` | Conversation user |
| `ctx.conversation` | `{ id?, ... }` | Conversation data |
| `ctx.operator` | `{ id?, ... }` | Assigned operator |
| `ctx.trigger` | `TriggerInfo` | Trigger type (http, cron, event) |
| `ctx.env` | `EnvAccessor` | Access to secrets |
| `ctx.params` | `Record<string, string>` | Route parameters |
| `ctx.query` | `Record<string, string>` | Query string params |
| `ctx.jelou` | `JelouSDK` | WhatsApp messaging and deferred runs |
| `ctx.memory` | `MemorySDK` | Key-value session memory |
| `ctx.templateRegistry` | `TemplateRegistry` | Approved WhatsApp template catalog |
| `ctx.guard` | `Guard` | Chain of checks to run before acting |
| `ctx.verifyStripe()` etc. | `(req) => Promise<void>` | Webhook signature verification |
| `ctx.method` | `string` | HTTP method (GET, POST, etc.) |
| `ctx.path` | `string` | Request path |
| `ctx.requestId` | `string` | Unique request UUID |
| `ctx.isCron` | `boolean` | `true` if cron trigger |
| `ctx.isEvent` | `boolean` | `true` if event trigger |
| `ctx.isHttp` | `boolean` | `true` if HTTP request |
| `ctx.isScheduledFire` | `boolean` | `true` if this is a deferred run firing |
| `ctx.skillId` | `string \| null` | Brain Studio workflow ID |
| `ctx.executionId` | `string \| null` | Brain Studio execution ID |
| `ctx.log()` | `(...args) => void` | Structured logger |

## Identity

```typescript theme={null}
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

> [tab] HTTP
>
> ```typescript theme={null}
> if (ctx.isHttp) {
>   ctx.trigger; // { type: "http" }
> }
> ```

> [tab] Cron
>
> ```typescript theme={null}
> if (ctx.isCron) {
>   ctx.trigger.cron;     // "0 9 * * *"
>   ctx.trigger.cronName; // "daily-reminder" (optional)
> }
> ```

> [tab] Event
>
> ```typescript theme={null}
> if (ctx.isEvent) {
>   ctx.trigger.event; // "payment.completed"
> }
> ```

## Request

```typescript theme={null}
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)

```typescript theme={null}
const apiKey = ctx.env.get("CRM_API_KEY"); // string | undefined
ctx.env.has("WEBHOOK_SECRET");              // boolean
ctx.env.toObject();                         // Record<string, string>
```

> [!NOTE]
>
> Internal variables with `__FN_` prefix are blocked — `ctx.env.get("__FN_COMPANY_ID")` returns `undefined`.

## Messaging (`ctx.jelou`)

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

> [card] [Messaging guide](https://docs.jelou.ai/en/guides/functions/mensajeria.md)
>
> 14 message types, HSM templates, and error handling.

## Memory (`ctx.memory`)

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

> [card] [Memory guide](https://docs.jelou.ai/en/guides/functions/memoria.md)
>
> Primitives, JSON, TTL, limits, and common patterns.

## Deferred runs (`ctx.jelou.schedule`)

Book a future run of your own function:

```typescript theme={null}
await ctx.jelou.schedule({
  in: "24h",
  path: "/send-followup",
  payload: { userId: input.userId, botId: ctx.bot.id },
  subject: `user_${input.userId}`,
});
```

> [card] [Deferred runs guide](https://docs.jelou.ai/en/guides/functions/diferidas.md)
>
> Book, look up, cancel and check before acting.

## Checks (`ctx.guard`)

Chain conditions and run the action only if all of them hold. Useful when reality may have changed since you booked something:

```typescript theme={null}
return ctx.guard
  .when("notYetPaid", async () => !(await fetchOrder(input.orderId)).paid)
  .run(async () => {
    await ctx.jelou.send({ type: "text", to: input.phone, text: "Your payment is pending" });
    return { sent: true };
  });
```

If any check returns a falsy value, the chain short-circuits and returns `{ skipped: "<name>" }` without running the action.

## Webhooks (`ctx.verify*`)

Verify a webhook signature before processing it:

```typescript theme={null}
async handler(event, ctx, request) {
  await ctx.verifyStripe(request);
  return { received: true };
}
```

> [card] [Webhooks guide](https://docs.jelou.ai/en/guides/functions/webhooks.md)
>
> Stripe, Shopify, Meta and generic HMAC signatures.

## Logging

`ctx.log()` writes structured JSON with automatic metadata:

```typescript theme={null}
ctx.log("Processing order", { phone: input.phone });
// stdout: { "requestId": "...", "function": "my-fn", "company": 42, "timestamp": "...", "args": [...] }
```

> [card] [Messaging](https://docs.jelou.ai/en/guides/functions/mensajeria.md)
>
> Send WhatsApp with ctx.jelou.

> [card] [Memory](https://docs.jelou.ai/en/guides/functions/memoria.md)
>
> Persist data with ctx.memory.

> [card] [Secrets](https://docs.jelou.ai/en/guides/functions/secrets.md)
>
> Encrypted environment variables.

> [card] [Authentication](https://docs.jelou.ai/en/guides/functions/autenticacion.md)
>
> Runtime tokens and public functions.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.