> ## 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, bot, user, trigger, environment, messaging, memory, and more.

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.

```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 }`   | Bot 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 client        |
| `ctx.memory`       | `MemorySDK`               | Key-value session memory         |
| `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.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

<Tabs>
  <Tab title="HTTP">
    ```typescript theme={null}
    if (ctx.isHttp) {
      ctx.trigger; // { type: "http" }
    }
    ```
  </Tab>

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

  <Tab title="Event">
    ```typescript theme={null}
    if (ctx.isEvent) {
      ctx.trigger.event; // "payment.completed"
    }
    ```
  </Tab>
</Tabs>

## 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`.
</Note>

## Messaging (`ctx.jelou`)

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

<Card title="Messaging guide" icon="message" href="/en/guides/functions/mensajeria">
  14 message types, HSM templates, and error handling.
</Card>

## 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 title="Memory guide" icon="database" href="/en/guides/functions/memoria">
  Primitives, JSON, TTL, limits, and common patterns.
</Card>

## 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": [...] }
```

<CardGroup cols={2}>
  <Card title="Messaging" icon="message" href="/en/guides/functions/mensajeria">
    Send WhatsApp with ctx.jelou.
  </Card>

  <Card title="Memory" icon="database" href="/en/guides/functions/memoria">
    Persist data with ctx.memory.
  </Card>

  <Card title="Secrets" icon="key" href="/en/guides/functions/secrets">
    Encrypted environment variables.
  </Card>

  <Card title="Authentication" icon="lock" href="/en/guides/functions/autenticacion">
    Runtime tokens and public functions.
  </Card>
</CardGroup>
