Skip to main content

Overview

The Lua API provides access to request-level runtime information. Use it in your tools, tool conditions, preprocessors, and postprocessors to access context about the current request.

Import

Availability

The Lua API is available in:

Lua.request

The request object contains information about the current request context.

channel

The channel through which the current request originated.
Type: Channel
Example - Channel-specific behavior:

webhook

The webhook object contains information about the incoming webhook request from channel integrations. This provides access to the original data sent by the channel provider (WhatsApp, Slack, Teams, etc.).
Type: { payload: any } | undefined The webhook object is only available when the request originated from a webhook-based channel. For direct API calls or the web chat widget, this will be undefined.

webhook.payload

The raw, unmodified webhook payload from the channel provider. Available for channels:
  • whatsapp - Full WhatsApp Cloud API webhook payload
  • slack - Slack Events API payload
  • teams - Microsoft Teams activity object
  • front - Front webhook body
  • facebook - Facebook Messenger webhook event
  • instagram - Instagram Messaging webhook event
  • messagebird - MessageBird webhook body
  • email - JMAP-aligned parsed email (headers + envelope; not raw MIME — see note below)
Example - Accessing WhatsApp webhook data:
Example - Slack event data:
Example - Teams activity:
Example - Email metadata:
The email channel’s payload differs from JSON-over-HTTP channels (WhatsApp, Slack, etc.). Email arrives over SMTP as RFC 5322 MIME — there is no JSON envelope from a provider to forward. Lua parses the message and exposes it as a JMAP-aligned object: typed common headers plus a headerLines array preserving the full RFC 5322 header order.
If your email channel is backed by an AgentMail inbox, webhook.payload follows AgentMail’s native event shape (message_id, thread_id, inbox_id, …) rather than the JMAP shape above. Branch on the presence of messageId (Lua-native) vs message_id (AgentMail) until the shapes are normalized.

Complete Examples

Conditional Tool Availability

Make a tool available only on certain channels:

Channel-Aware Tool Logic

Adjust tool behavior based on channel:

Preprocessor with Channel Context

Use channel in preprocessors:

Postprocessor with Channel-Specific Formatting

Format responses based on channel:

Future Expansion

The Lua API will be expanded to include additional runtime information, such as:
  • User profile data
  • Session information
  • More request metadata
This API is designed to grow. Check back for new properties as they become available.

Best Practices

Always import the Lua API from lua-cli:
The channel type includes unknown for unrecognized values:
Use channel for minor adjustments, not completely different flows:
For channel-specific tools, use the condition function:
Always check if webhook exists before accessing it:
For channel-specific webhook handling, always check both:

Skills & Tools

Learn about creating tools with conditions

Preprocessors

Intercept and modify incoming messages

Postprocessors

Transform agent responses

Channels

Overview of available channels