# Webhooks

> Documentation index: https://ridewithgps.com/api/v1/doc/llms.txt

Ride with GPS sends a `POST` request to your webhook URL when something changes for a user who authorized your API client: their routes and trips, their events and who RSVPs to them, their organization's members, or their authorization itself.

## Configuration

On your API client management page, enter the URL (`webhook_url`) where you want to be notified, and check what you want to be notified about:

| Setting | `item_type` | Sent when |
| --- | --- | --- |
| Sync routes | `route` | One of the user's routes is created, updated or deleted, or the user adds a route to or removes it from a collection |
| Sync trips | `trip` | The same, for trips |
| Events | `event` | One of the user's events is created, updated or deleted. Adding or removing a route counts as an update |
| Event RSVPs | `event_participant` | Someone RSVPs to one of the user's events, changes their RSVP, or withdraws it |
| Organization members | `club_member` | A member of the user's organization is invited, asks to join, is approved, changes role or leaves |
| Revoked authorizations | `authorization` | The user disconnects your app from their Ride with GPS account |

Sync routes and Sync trips also choose what the [sync endpoint](https://ridewithgps.com/api/v1/doc/endpoints/sync.md) returns. The other settings only send webhooks, starting within a minute of checking them.

You are notified about a user's data only while they have authorized your API client, with OAuth or with an authentication token. Events, RSVPs and members are reported to the owner of the event or the organization: an organization that authorizes your client gets notifications about its events, their RSVPs and its members.

## Webhook request

Ride with GPS sends `POST` notifications to your webhook URL, several at a time. Here is an example of a webhook request:

```json
// POST {webhook_url}
// content-type: application/json
// user-agent: RideWithGPS
// x-rwgps-api-key: {api_key}
// x-rwgps-signature: {signature}
// x-rwgps-datetime: {datetime}
{
  "notifications": [
    {
      "user_id": 1,
      "item_type": "route",
      "item_id": 101,
      "item_user_id": 1,
      "item_url": "https://ridewithgps.com/routes/101.json",
      "action": "deleted",
      "collection": null
    },
    {
      "user_id": 2,
      "item_type": "route",
      "item_id": 102,
      "item_user_id": 1,
      "item_url": "https://ridewithgps.com/routes/102.json",
      "action": "added",
      "collection": {
        "id": 2,
        "type": "pinned",
        "url": null
      }
    },
    {
      "user_id": 3,
      "item_type": "event_participant",
      "item_id": null,
      "item_user_id": 42,
      "item_url": null,
      "action": "created",
      "collection": null,
      "event": {
        "id": 301,
        "url": "https://ridewithgps.com/api/v1/events/301.json"
      },
      "status": "participant"
    },
    {
      "user_id": 42,
      "item_type": "authorization",
      "item_id": null,
      "item_user_id": 42,
      "item_url": null,
      "action": "deleted",
      "collection": null
    }
  ]
}
```

> No more than 100 notifications are sent per request.

For each notification in the request payload:

* `user_id` is the id of the user who authorized your API client and whose data changed. Fetch `item_url` with that user's token.
* `item_type` is one of the types in the table above.
* `item_id` is the `id` of the item, `null` for RSVPs and authorizations, which have no id of their own in the API.
* `item_user_id` is the `id` of the user the item belongs to: the owner of a route, trip or event, the rider who RSVPed, or the member. It differs from `user_id` when a user adds another user's item to a collection, and for RSVPs and members. It is `null` for a member invited by email who hasn't claimed the invitation yet.
* `item_url` is where to fetch the item, `null` for RSVPs and authorizations.
* `action` is one of the following:

| Action | Description |
| --- | --- |
| `created` | The item was created |
| `updated` | The item was updated |
| `deleted` | The item was deleted |
| `added` | The user added the item to the specified collection (routes and trips) |
| `removed` | The user removed the item from the specified collection (routes and trips) |

* For `added` and `removed` actions, `collection` is the target collection where the action was taken. It is currently always the collection of pinned items for the user, but we might expose actions on other collections in the future.
* For RSVPs, `event` is the event the RSVP is for, with its `id` and `url`, and `status` is the RSVP's status.

### Events

* `item_url` is the event in the [events endpoints](https://ridewithgps.com/api/v1/doc/endpoints/events.md).
* `updated` is sent when the event's details change, or when a route is added to it or removed from it. Changes to the routes themselves come as `route` notifications, when you also check Sync routes.
* Deleting an event sends one `deleted` notification for the event, none for its RSVPs.

### Event RSVPs

* An RSVP is identified by `event.id` and `item_user_id` (the rider). `item_id` and `item_url` are `null`.
* `status` is the RSVP's status: `participant` (going), `interested`, `declined` (not going), or `disallowed` (removed by an organizer, or expired). It is `null` on `deleted`: the RSVP is gone because the rider withdrew, an organizer removed them, or the rider deleted their account.
* `event.url`, fetched with the event owner's token, lists the participants.
* An RSVP that expires on its own, on events that limit how long an RSVP lasts, sends no notification.

To RSVP for a user, use the [event participant endpoints](https://ridewithgps.com/api/v1/doc/endpoints/events.md) with that user's own token.

### Organization members

* `item_url` is the member in the [club members endpoints](https://ridewithgps.com/api/v1/doc/organizations/club_members.md), fetched with the organization's token.
* `created` is sent when someone is invited or asks to join, `updated` when they are approved, claim their invitation, or their role or details change, and `deleted` when they leave or are removed.

### Revoked authorizations

* The action is `deleted`. `item_id` is `null`; `item_user_id` is the user's id.
* It is sent when the user disconnects your app from their account, which revokes the OAuth tokens they gave it. Delete what you stored for them.
* It is not sent when your app revokes its own token.

## Delivery

Notifications are grouped: the ones raised within 30 seconds of each other are sent together. RSVPs and revoked authorizations are sent within a few seconds, along with any notifications already waiting.

When the same item is created, updated or deleted more than once before a request is sent, you get one notification for it, with the most important action (`deleted`, then `created`, then `updated`) and the latest values: an RSVP made and then changed from `participant` to `interested` arrives as `created` with `status` `interested`. Adding to and removing from a collection (`added`, `removed`) are reported on their own.

## Webhook signature

Webhook requests include the following headers:

* `x-rwgps-api-key` lets you identify the API client responsible for the request in case you have created multiple API clients.
* `x-rwgps-signature` is a HMAC-SHA256 signature of the raw request body, using your API client's API secret as the signing key (the "API secret" on your API client's page, not its OAuth secret). Compute the signature on your end to validate that the request originated from Ride with GPS. For example:

```python
# Python example
import hashlib
import hmac
signature = request_headers['x-rwgps-signature']
valid = hmac.compare_digest(signature, hmac.new(b'{api_secret}', request_raw_body, hashlib.sha256).hexdigest())
```

```javascript
// Javascript example
const { createHmac } = require('crypto')
const signature = requestHeaders['x-rwgps-signature']
const valid = signature === createHmac('sha256', '{api_secret}').update(requestRawBody).digest('hex')
```

* `x-rwgps-datetime` is when the request was sent, in ISO 8601.

## Webhook request handling

When requesting your webhook, Ride with GPS expects a `200` response within 1 second. We strongly recommend that you acknowledge the webhook request as quickly as possible and process its content in a separate thread.

Processing a webhook request includes, for each `notification` in the `notifications` array:

* Retrieve from your system the authentication `token` (OAuth or Basic authentication) associated with the `notification.user_id`
* Fetch `item_url`, authenticating the request with the `token` (see [authentication documentation](https://ridewithgps.com/api/v1/doc/authentication.md)), unless the action is `deleted` or `item_url` is `null`
* Update your records with the new or updated item

> If you did not store the `user_id` alongside the `access_token`, you can retrieve it from [this endpoint](https://ridewithgps.com/api/v1/doc/endpoints/users.md).

<!-- -->

> **Important**
> * No retry mechanism is currently implemented. If your server misses a request, the notifications in it are lost: check for changes from time to time with the [sync endpoint](https://ridewithgps.com/api/v1/doc/endpoints/sync.md) or the list endpoints.
> * `3xx` redirect status codes in responses are NOT followed.
