> ## 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

> Referência completa do objeto ctx disponível em cada handler: empresa, bot, usuário, trigger, ambiente, mensagens, memória e mais.

O objeto `ctx` é o segundo parâmetro de todo handler. Contém informações da empresa, bot, usuário, tipo de trigger, e dá acesso a secrets, mensagens, memória e logging.

```typescript theme={null}
handler: async (input, ctx, request) => {
  ctx.log("Requisição recebida", {
    company: ctx.company.name,
    bot: ctx.bot.channel,
    user: ctx.user.id,
  });
}
```

## Referência

| Propriedade        | Tipo                      | Descrição                           |
| ------------------ | ------------------------- | ----------------------------------- |
| `ctx.functionSlug` | `string`                  | Slug da função                      |
| `ctx.company`      | `{ id, name }`            | Empresa da requisição               |
| `ctx.bot`          | `{ id, name, channel }`   | Bot que originou a requisição       |
| `ctx.user`         | `{ id, names?, roomId? }` | Usuário da conversa                 |
| `ctx.trigger`      | `TriggerInfo`             | Tipo de trigger (http, cron, event) |
| `ctx.env`          | `EnvAccessor`             | Acesso a secrets                    |
| `ctx.params`       | `Record<string, string>`  | Parâmetros de rota                  |
| `ctx.query`        | `Record<string, string>`  | Query string params                 |
| `ctx.jelou`        | `JelouSDK`                | Cliente de mensagens WhatsApp       |
| `ctx.memory`       | `MemorySDK`               | Memória de sessão key-value         |
| `ctx.method`       | `string`                  | Método HTTP                         |
| `ctx.path`         | `string`                  | Path da requisição                  |
| `ctx.requestId`    | `string`                  | UUID único                          |
| `ctx.isCron`       | `boolean`                 | `true` se trigger cron              |
| `ctx.isEvent`      | `boolean`                 | `true` se trigger event             |
| `ctx.isHttp`       | `boolean`                 | `true` se requisição HTTP           |
| `ctx.skillId`      | `string \| null`          | ID do fluxo do Brain Studio         |
| `ctx.executionId`  | `string \| null`          | ID de execução do Brain Studio      |
| `ctx.log()`        | `(...args) => void`       | Logger estruturado                  |

## Identidade

```typescript theme={null}
ctx.company.id;    // 42
ctx.company.name;  // "Loja ABC"
ctx.bot.id;        // "bot-123"
ctx.bot.channel;   // "whatsapp"
ctx.user.id;       // 99
```

## 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 * * *"
    }
    ```
  </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.params.id;   // "42"
    ctx.query.format // "json"
    ctx.requestId;   // "a1b2c3d4-..."
  },
});
```

## Ambiente (secrets)

```typescript theme={null}
const apiKey = ctx.env.get("CRM_API_KEY");
ctx.env.has("WEBHOOK_SECRET");
```

<Note>
  Variáveis internas com prefixo `__FN_` são bloqueadas.
</Note>

## Logging

```typescript theme={null}
ctx.log("Processando pedido", { telefone: input.telefone });
// stdout: { "requestId": "...", "function": "...", "timestamp": "...", "args": [...] }
```

<CardGroup cols={2}>
  <Card title="Mensagens" icon="message" href="/pt/guias/funcoes/mensajeria">
    Enviar WhatsApp com ctx.jelou.
  </Card>

  <Card title="Memória" icon="database" href="/pt/guias/funcoes/memoria">
    Persistir dados com ctx.memory.
  </Card>
</CardGroup>
