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

# Sync businesses and customers

> Mirror the businesses and customers in your system into Pocketflows without creating duplicates

Use the sync endpoints to keep Pocketflows matched to your own records. You
identify each record with your own identifier, the `external_id`, and
Pocketflows creates the record if it doesn't exist or replaces it if it does.
For the model behind this, see [How syncing works](https://docs.pocketflows.com/concepts/sync.md).

| To sync | Endpoint |
| - | - |
| One business | `POST /sync/businesses` |
| Many businesses | `POST /sync/businesses/batch` |
| One customer of a business | `POST /sync/businesses/:business_id/customers` |
| Many customers of a business | `POST /sync/businesses/:business_id/customers/batch` |

> [!NOTE]
>
> A sync request replaces the whole record. Properties you leave out are blanked out, so pass every
> property in every sync request.

## 1. Sync a business

Pass the unique identifier you already have for the business as `external_id`,
along with its `name`. `metadata` is optional.

```
POST /sync/businesses

Body
{
  "external_id": "your_business_id",
  "name": "Test Business",
  "metadata": {}
}
```

The response is the business, including its Pocketflows `id`. Save this `id` to
use as the `business_id` in the customer endpoints below.

## 2. Sync a customer of that business

Pass the unique identifier you already have for the customer as `external_id`,
along with the customer's properties: `email_address`, `phone_number`,
`full_name`, `first_name`, `last_name`, and `metadata`. Every request must
include at least one of `email_address` or `phone_number`.

```
POST /sync/businesses/:business_id/customers

Body
{
  "external_id": "your_customer_id",
  "email_address": "customer@example.com",
  "first_name": "Test",
  "last_name": "Customer"
}
```

The response is the customer, including its Pocketflows `id`.

To change one property on an existing customer, send the full record again with
the new value. This request clears the phone number and keeps the email address
and name, because they are sent again:

```
POST /sync/businesses/:business_id/customers

Body
{
  "external_id": "your_customer_id",
  "email_address": "customer@example.com",
  "phone_number": null,
  "first_name": "Test",
  "last_name": "Customer"
}
```

## 3. Sync in batches

To sync many records in one request, send them as an array. Each record takes
the same properties as the single-record request.

```
POST /sync/businesses/batch

Body
{
  "businesses": [
    {
      "external_id": "your_business_id",
      "name": "Test Business",
      "metadata": {}
    }
  ]
}
```

```
POST /sync/businesses/:business_id/customers/batch

Body
{
  "customers": [
    {
      "external_id": "your_customer_id",
      "email_address": "customer@example.com",
      "first_name": "Test",
      "last_name": "Customer"
    }
  ]
}
```

The customer batch endpoint allows for partial success: when a customer is
invalid, the rest of the request still processes. In the response, the
`customers` array contains `null` for the customers that had errors, and the
`errors` array says what went wrong with each one. A request can contain at most
15,000 customers.

The business batch endpoint is all or nothing: if any business is invalid, for
example an `id` that does not match one of your businesses, the request fails
and no businesses are synced.

## Keep records in sync over time

After the first sync, send changes as they happen in your system:

* **A property changes,** such as a new phone number: sync the full record again, as in step 2.
* **A customer opts out of marketing:** see
  [Respect customer consent](https://docs.pocketflows.com/how-to/manage-customer-consent.md). Sync doesn't change a customer's
  unsubscribes.
* **A customer is deleted:** call `DELETE /customers/:id` with the customer's Pocketflows `id`. There
  is no way to delete or look up a customer by `external_id`, so store the `id` from each sync
  response alongside your own record.

## Re-running a sync

Sending the same `external_id` again finds the existing record and replaces it,
so repeating a sync does not create a second record.

To retry an individual request safely, see
[Idempotency keys](https://docs.pocketflows.com/reference/idempotency-keys.md).

## Related

* [Let businesses build segments from your data](https://docs.pocketflows.com/how-to/set-up-segment-filters.md)
  to turn the `metadata` you sync into filters in the embed.
* [Migrate to the sync endpoints](https://docs.pocketflows.com/how-to/migrate-to-sync-endpoints.md) if you
  already create businesses and customers with the previous endpoints.


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