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

# On Geofence Leave

> Send a webhook when a field worker leaves a geofence they had entered.

**On Geofence Leave** runs when a field worker's GPS position leaves a [geofence](/pages/settings/geofence) whose entry was already sent. Use it to close a site visit in another system, measure time on site, or alert when a worker leaves a restricted area.

<Note>
  Required permission:

  * View automation
  * Create automation
  * View geofence
</Note>

Availability and constraints are the same as [On Geofence Entry](/pages/workflow/automation/events/on-geofence-entry): geofence is only turned on for selected organizations, **Webhook** is the only automation type, a **Geofence Group** is required and there are no rules.

## When it runs

1. The field app reports a position outside a geofence the worker was recorded as inside.
2. The system checks whether the **entry** for that geofence was sent.
3. If it was, the leave runs and the webhook is sent. If it wasn't, nothing is sent.

**A leave never runs without its entry.** If the worker only clipped the corner of a geofence and left before the dwell time, no entry was sent, so no leave is sent either. Every leave you receive pairs with an entry you already received for the same worker and geofence.

## Set it up

On **Workflow › Automation**, click **New**, then:

<div align="center">
  <img src="https://mintcdn.com/mileappv4/Xul_B0hUk35UiMLk/images/v4/workflow/automation/event-geofence.png?fit=max&auto=format&n=Xul_B0hUk35UiMLk&q=85&s=b21c9c9f378a9bff21e32cb498c9573e" alt="Geofence event" width="600" data-path="images/v4/workflow/automation/event-geofence.png" />
</div>

1. **Event**: **On Geofence Leave**.
2. **Geofence Group**: required. The group to watch.

Choose **Automation Type** › **Webhook**, fill in the [webhook](/pages/workflow/automation/actions/webhook) **URL** and **Headers**, and click **Submit**.

Entry and leave are two events, so each needs its own automation. Both can watch the same group and send to the same URL; tell them apart by `eventName`.

## Webhook payload

The body has the same shape as [On Geofence Entry](/pages/workflow/automation/events/on-geofence-entry#webhook-payload), with `eventName` set to `leave_geofence`:

```json theme={null}
{
  "_id": "663b2c3d4f1a2b3c4d5e6f80",
  "eventName": "leave_geofence",
  "email": "driver@example.com",
  "organizationId": "660a1b2c4f1a2b3c4d5e6f03",
  "taskId": null,
  "lat": -6.2093,
  "lon": 106.8461,
  "speed": 18,
  "createdFrom": "app",
  "isLatest": true,
  "createdTime": "2026-07-20T08:47:00.000Z",
  "geofenceId": "gf_123",
  "geofenceName": "Store A Sudirman",
  "geofenceGroupId": "66c1a4f2d3b19f0012ab77e4",
  "geofenceGroup": "Visit Store",
  "dwellMinutes": 5
}
```

| Field | Description |
| - | - |
| `eventName` | Always `leave_geofence` for this event. |
| `geofenceId`, `geofenceName` | The geofence that was left. |
| `geofenceGroupId`, `geofenceGroup` | The group's ID (used for matching) and name (for display). |
| `dwellMinutes` | The geofence's dwell time setting, not the time spent inside. |
| `lat`, `lon`, `speed`, `createdTime` | The first position reported outside the geofence. |
| `email`, `taskId`, `organizationId`, `createdFrom`, `isLatest`, `_id` | As in the entry payload. |

To measure the visit, compare `createdTime` of the leave with `createdTime` of the matching entry.

## Troubleshooting

**The leave never runs.**

* Check that you received the matching entry first. No entry means no leave.
* Check that the automation and the geofence are active, and the geofence is in the chosen group.
* The field app must keep reporting positions until the worker is outside.

**The leave arrived much later than expected.** The leave is decided from the first position reported outside. If the device lost signal inside the zone, the crossing is only detected once positions resume.

**The webhook receives nothing.** Check the URL, the authentication header, and the response in the [log](/pages/workflow/automation/log).


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