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

# Run an agent when something happens in an app

> Connect an app such as Gmail or Google Calendar and start an agent each time an event happens there

This guide starts an agent each time something happens in an app you use, such as a new email in Gmail or a new event in Google Calendar. You connect your account once, then set the agent's trigger to the event.

Before you begin, you need:

* An agent to start. See [Create an agent](https://docs.servflow.io/guides/create-an-agent.md) or [Describe an agent with Assist](https://docs.servflow.io/guides/describe-an-agent-with-assist.md).
* An account in the app, and permission to sign in to it.

The apps that offer events include Gmail, Google Calendar, Slack, Notion, Jira, Confluence, HubSpot, Discord, WhatsApp and MailerLite.

## Connect the app

**1. Open Integrations**

In the sidebar under **Connect and tools**, click **Integrations**, then **New Integration**.

**2. Choose the app**

Search for the app, for example `Gmail`, and choose it. The app is marked **Guided setup**.

**3. Name it and sign in**

Enter a **Name**, for example `Work Gmail`. This is what you pick later when an agent uses this account.

Click **Connect to** and the app's name. The app's sign-in page opens. Sign in and approve the connection.

![The Connect dialog for an app with the Name field and the Connect button](https://mintcdn.com/servflow/S3r-o0vV2glvw94z/images/guides/run-an-agent-on-an-app-event/01-app-sign-in.png?fit=max\&auto=format\&n=S3r-o0vV2glvw94z\&q=85\&s=6abb515953414da4d49e1d4996a69e6b)

**4. Check the integration**

The sign-in sends you back to **Integrations**. The account is in the list with the status **Enabled**.

If you leave the sign-in before approving, the account shows **Not connected**. Open the row's actions and click **Connect integration** to try again.

## Set the trigger to App event

**1. Open the Entry panel**

Open the agent in the editor and click the trigger node at the top of the canvas. The **Entry** panel opens.

**2. Choose App event**

Under **Entry type**, choose **App event**.

**3. Pick the app, the event and the account**

Under **Event**:

1. Under **App**, choose the app, for example **Gmail**.
2. Under **Event**, choose what starts the agent, for example **New Gmail Message Received Trigger**. The help under the field says what the event is.
3. Under **Account**, choose the account you connected.

![The Entry panel set to App event with an app, an event and an account chosen](https://mintcdn.com/servflow/S3r-o0vV2glvw94z/images/guides/run-an-agent-on-an-app-event/02-app-event-trigger.png?fit=max\&auto=format\&n=S3r-o0vV2glvw94z\&q=85\&s=1890d0903f7ab624caab78558153af6a)

Some apps are checked for new events about every 15 minutes, so a run can start a few minutes after the event. When that applies, the help under **Event** says so.

**4. Adjust the settings**

If the event has settings, they appear under **Settings**. A Gmail message, for example, can be narrowed with **Label Ids** or **Query**. Leave a setting empty to use the event's default.

## Read the event in the prompt

Under **Variables**, the **Entry** panel lists what the agent can read about the event. Each one reads as `{{ .event.data.<name> }}`. For a new Gmail message, set the first sub agent's **User prompt** to something like:

```
From: {{ .event.data.sender }}
Subject: {{ .event.data.subject }}

{{ .event.data.message_text }}
```

To give the agent the whole event, use `{{ .event.json }}`.

The agent does not reply in the app on its own. To reply or act there, give the agent a tool from the same app on the same account. See [Add a tool](https://docs.servflow.io/guides/add-a-tool.md).

## Enable and test

**1. Test in the editor**

Click **Run**. The dialog asks for the event's fields, such as the sender and the subject. Fill in the ones the prompt reads and click **Run**. The run panel shows the agent's answer.

**2. Save and enable**

Click **Save**, then turn on the **Enabled** switch in the top bar. The agent starts watching the account for the event.

**3. Make the event happen**

Make the event happen in the app, for example send yourself an email. The run appears on the **Runs** page with the trigger **App event**. See [Check what your agent did](https://docs.servflow.io/guides/check-what-your-agent-did.md).

## Related

> [card] [Add an integration](https://docs.servflow.io/guides/add-an-integration.md)
>
> Connect other accounts.

> [card] [Add a tool](https://docs.servflow.io/guides/add-a-tool.md)
>
> Let the agent reply or act in the app.

> [card] [Connect an agent to Slack](https://docs.servflow.io/guides/connect-an-agent-to-slack.md)
>
> Answer messages as the Servflow bot.

> [card] [Check what your agent did](https://docs.servflow.io/guides/check-what-your-agent-did.md)
>
> Read the runs an event started.


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