# Sync

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

OpenAPI specification for these endpoints: https://ridewithgps.com/api/v1/openapi/openapi_sync.yaml

## Returns items for sync

`GET /api/v1/sync.json`

Returns a list of items (routes and/or trips) that the user has interacted with since the given datetime.

The sync endpoint is convenient to maintain a remote copy of a user's library of trips and/or routes.

### Actions

Items can have the following actions:
* `created` - The user created the item
* `updated` - The user updated the item
* `deleted` - The user deleted the item
* `added` - The user added the item to the specified collection (e.g., pinned)
* `removed` - The user removed the item from the specified collection

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 authenticated user, but we might expose other collections in the future.

### Sync Workflow

This endpoint is optimized for performance (we use a similar setup for syncing our own mobile apps), and can return the entire library for the user by using a `since` value well in the past (for example `1970-01-01`).

The following workflow is suitable to maintain your copy of a user's library:

1. When the user finalizes the OAuth workflow, get the current content of their library at `GET https://ridewithgps.com/api/v1/sync.json?since=1970-01-01`
2. Download and integrate in your system every item in the response and store the value of the `meta.rwgps_datetime` key in the response
3. Upon reception of a webhook for the user, get and integrate the items that have changed from a new sync request at `GET https://ridewithgps.com/api/v1/sync.json?since=[rwgps_datetime]`. Store the new value of `meta.rwgps_datetime` for the next sync request.

Doing a sync request on webhook reception instead of processing the items in the body of the webhook leads to the same result and provides a recovery mechanism for previous webhooks that might have failed because of network or systems availability issues.

### Headers

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `x-rwgps-api-key` | string | yes | API key for identifying the client application |
| `x-rwgps-auth-token` | string | yes | Authentication token for the user |

### Parameters

| Name | Type | In | Required | Description |
| --- | --- | --- | --- | --- |
| `since` | string | query | yes | ISO 8601 datetime string for last sync. Use 1970-01-01 for initial sync to get entire library. |
| `assets` | string | query | no | Comma-separated list of asset types to return (routes, trips). Defaults to your API client setting. One of: `routes`, `trips`, `routes,trips`. |

### Response `200`

| Name | Type | Description |
| --- | --- | --- |
| `items` | array of objects | Array of sync items representing changes since the specified datetime |
| `items[].item_type` | string | Type of item. One of: `route`, `trip`. |
| `items[].item_id` | integer | ID of the item |
| `items[].item_user_id` | integer | ID of the user who owns the item. It will be the ID of the authenticated user for created, updated and deleted actions. It might point to another user for added and removed actions, when the authenticated user pins an item from another user. |
| `items[].item_url` | string | API URL for the item |
| `items[].action` | string | Action performed on the item. One of: `created`, `updated`, `deleted`, `added`, `removed`. |
| `items[].datetime` | string | When the action occurred |
| `items[].collection` | object or null | Collection context for added/removed actions (null for other actions) |
| `items[].collection.id` | integer |  |
| `items[].collection.type` | string |  |
| `items[].collection.name` | string |  |
| `items[].collection.url` | string |  |
| `meta` | object |  |
| `meta.rwgps_datetime` | string | Current server timestamp - use this value for the next sync request |
| `meta.next_sync_url` | string | Pre-formatted URL for the next sync request with proper datetime |

### Errors

- `401` Unauthorized

### Example request

```bash
curl \
  -H "x-rwgps-api-key: $API_KEY" \
  -H "x-rwgps-auth-token: $AUTH_TOKEN" \
  https://ridewithgps.com/api/v1/sync.json
```

### Example response

```json
{
  "items": [
    {
      "item_type": "route",
      "item_id": 123,
      "item_user_id": 123,
      "item_url": "https://ridewithgps.com/api/v1/routes/123.json",
      "action": "created",
      "datetime": "2000-01-01T11:45:00+00:00",
      "collection": {
        "id": 123,
        "type": "pinned",
        "name": "Pinned",
        "url": "https://ridewithgps.com/api/v1/collections/123.json"
      }
    }
  ],
  "meta": {
    "rwgps_datetime": "2000-01-01T12:00:00+00:00",
    "next_sync_url": "https://ridewithgps.com/api/v1/sync.json?assets=routes%2Ctrips&since=2000-01-01T12%3A00%3A00%2B00%3A00"
  }
}
```
