# Build an enrichment integration (https://docs.chargeflow.io/docs/merchants/recover/enrichment-integration)



You are wiring your systems to send dispute data to Chargeflow, and you want a pattern that holds up in production. The endpoints are simple; the part integrators get stuck on is the order of operations. This guide is that order: &#x2A;*map the IDs, read before you write, enrich only the gaps.**

Before you build, run a [gap analysis](https://docs.chargeflow.io/docs/merchants/recover/enrich-a-dispute#start-with-a-gap-analysis): connect your processor and billing platform first, so you only build enrichment for data Chargeflow cannot already pull.

## The flow [#the-flow]

<Mermaid
  title="Enrichment integration: map, read, enrich"
  chart="`
sequenceDiagram
participant P as Processor
participant C as Chargeflow
participant Y as Your system
P->>C: Dispute filed
C->>Y: dispute.created (id + source_id)
Y->>Y: Store id ↔ source_id mapping
loop Hourly or daily backfill
  Y->>C: GET /disputes
  C-->>Y: disputes[] (id + source_id)
  Y->>Y: Upsert any missing mappings
end
Y->>C: GET /disputes/{id}?expand=transaction&expand=order
C-->>Y: Dispute with the data Chargeflow holds
Y->>Y: Diff against your data
Y->>C: PATCH /disputes/{id} (missing fields only)
C-->>Y: 200, updated dispute
Y->>Y: Mark dispute as enriched
`"
/>

## Before you start [#before-you-start]

* An API key in the `CHARGEFLOW_API_KEY` environment variable. See [Authentication](https://docs.chargeflow.io/docs/reference/api-fundamentals/authentication).
* A `dispute.created` webhook registered to your endpoint. See [Subscribe to webhook events](https://docs.chargeflow.io/docs/merchants/recover/subscribe-to-events).
* The difference between the Chargeflow dispute ID and the processor's `source_id`. See [Dispute identifiers](https://docs.chargeflow.io/docs/merchants/recover/dispute-identifiers).

## Steps [#steps]

<Steps>
  <Step>
    ### Build the ID mapping [#build-the-id-mapping]

    Every API call takes the Chargeflow dispute ID, but your systems know the processor's ID. Store both as each dispute arrives.

    **From the webhook.** On `dispute.created`, upsert `data.id`, `data.source`, and `data.source_id`, then reply `200`:

    ```javascript title="Node.js (Express)"
    app.post('/webhooks/dispute-created', express.json(), async (req, res) => {
      const { id, source, source_id } = req.body.data;
      await db.disputeMap.upsert({
        chargeflow_dispute_id: id,
        source,
        source_dispute_id: source_id,
      });
      res.sendStatus(200);
    });
    ```

    **From a scheduled backfill.** Webhooks can be missed: your endpoint was down, or the dispute predates your registration. Run a job hourly or daily that pages through `GET /public/2025-04-01/disputes` and upserts any `id` you have not stored. The paging loop is in [Backfill the last 90 days](https://docs.chargeflow.io/docs/merchants/recover/backfill-90-days); reuse it with the same upsert.

    You should see one row per Chargeflow dispute, and the backfill job should add zero rows on a healthy day.
  </Step>

  <Step>
    ### Read before you write [#read-before-you-write]

    Fetch the dispute with `expand` to see what Chargeflow already holds. Without `expand`, `transaction` and `order` come back as IDs; with it, they come back as full objects.

    <Tabs items="['curl', 'Node.js', 'Python']">
      <Tab value="curl">
        ```bash title="Terminal"
        curl -G https://api.chargeflow.io/public/2025-04-01/disputes/DISPUTE_ID \
          -H "x-api-key: $CHARGEFLOW_API_KEY" \
          --data-urlencode "expand=transaction" \
          --data-urlencode "expand=order"
        ```
      </Tab>

      <Tab value="Node.js">
        ```javascript title="Node.js"
        const url = new URL('https://api.chargeflow.io/public/2025-04-01/disputes/DISPUTE_ID');
        url.searchParams.append('expand', 'transaction');
        url.searchParams.append('expand', 'order');

        const res = await fetch(url, {
          headers: { 'x-api-key': process.env.CHARGEFLOW_API_KEY },
        });
        const dispute = await res.json();
        ```
      </Tab>

      <Tab value="Python">
        ```python title="Python"
        import os, requests

        res = requests.get(
            "https://api.chargeflow.io/public/2025-04-01/disputes/DISPUTE_ID",
            headers={"x-api-key": os.environ["CHARGEFLOW_API_KEY"]},
            params={"expand": ["transaction", "order"]},
        )
        dispute = res.json()
        ```
      </Tab>
    </Tabs>

    ```json title="Response (excerpt)"
    {
      "id": "66e6ea9ecd94925a9f8060d9",
      "source": "stripe",
      "source_id": "du_123456789",
      "reason": "fraud",
      "status": "needs_response",
      "transaction": {
        "id": "66e6ea9ecd94925a9f8060da",
        "amount": 150,
        "currency": "USD",
        "billing_address": null
      },
      "order": {
        "id": "66e6ea9ecd94925a9f8060db",
        "customer": {
          "name": "John Doe",
          "email": "john.doe@example.com",
          "phone_number": null,
          "ip_address": null
        },
        "products": [],
        "fulfillments": []
      }
    }
    ```

    In this example Chargeflow holds the amount, name, and email from the processor, but no billing address, phone, IP address, or products. Those are your gaps. The full list of expansions (`transaction`, `order`, `subscription`, `customer_communication`) is on [Get dispute by ID](https://docs.chargeflow.io/docs/api/2025-04-01/merchants/disputes/public/2025-04-01/disputes/disputeid/get).

    A `404 "Dispute not found"` here almost always means a processor ID was passed as `DISPUTE_ID`. Resolve it through your mapping table from step 1.
  </Step>

  <Step>
    ### Enrich only the gaps [#enrich-only-the-gaps]

    Send the missing fields, and nothing else, to `PATCH /public/2025-04-01/disputes/{disputeId}`:

    <Tabs items="['curl', 'Node.js', 'Python']">
      <Tab value="curl">
        ```bash title="Terminal"
        curl -X PATCH https://api.chargeflow.io/public/2025-04-01/disputes/DISPUTE_ID \
          -H "x-api-key: $CHARGEFLOW_API_KEY" \
          -H "Content-Type: application/json" \
          -d '{
            "order": {
              "customer": {
                "phone_number": "1(123)4567890",
                "ip_address": "203.0.113.42"
              }
            },
            "transaction": {
              "billing_address": {
                "name": "John Doe",
                "line1": "123 Main St",
                "city": "New York",
                "state": "NY",
                "zip_code": "10001",
                "country": "US"
              }
            }
          }'
        ```
      </Tab>

      <Tab value="Node.js">
        ```javascript title="Node.js"
        const res = await fetch('https://api.chargeflow.io/public/2025-04-01/disputes/DISPUTE_ID', {
          method: 'PATCH',
          headers: {
            'x-api-key': process.env.CHARGEFLOW_API_KEY,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            order: {
              customer: { phone_number: '1(123)4567890', ip_address: '203.0.113.42' },
            },
            transaction: {
              billing_address: {
                name: 'John Doe',
                line1: '123 Main St',
                city: 'New York',
                state: 'NY',
                zip_code: '10001',
                country: 'US',
              },
            },
          }),
        });
        const updated = await res.json();
        ```
      </Tab>

      <Tab value="Python">
        ```python title="Python"
        import os, requests

        res = requests.patch(
            "https://api.chargeflow.io/public/2025-04-01/disputes/DISPUTE_ID",
            headers={"x-api-key": os.environ["CHARGEFLOW_API_KEY"]},
            json={
                "order": {
                    "customer": {"phone_number": "1(123)4567890", "ip_address": "203.0.113.42"}
                },
                "transaction": {
                    "billing_address": {
                        "name": "John Doe",
                        "line1": "123 Main St",
                        "city": "New York",
                        "state": "NY",
                        "zip_code": "10001",
                        "country": "US",
                    }
                },
            },
        )
        updated = res.json()
        ```
      </Tab>
    </Tabs>

    You should get `200` with the updated dispute, now carrying the fields you sent.

    <Callout type="warn" title="Arrays are replaced, not merged">
      If you send `products` or `fulfillments`, send the complete array. Items you leave out are
      removed. Details in [Enrich a dispute](https://docs.chargeflow.io/docs/merchants/recover/enrich-a-dispute#important-array-merge-behavior).
    </Callout>

    Files and free-form data (usage reports, screenshots, CRM notes) do not go here. See [where does my data go](https://docs.chargeflow.io/docs/merchants/recover/upload-evidence#where-does-my-data-go).
  </Step>
</Steps>

## Track enrichment coverage [#track-enrichment-coverage]

Record the outcome of each dispute in your own system, for example an `enriched_at` timestamp and the list of fields sent, next to the ID mapping. Then a daily query finds the disputes you did not enrich (no `enriched_at`, or a failed PATCH) so you can retry them while they are still inside the response window (`due_by`).

## Next step [#next-step]

<Cards>
  <Card title="Go-live checklist" href="/docs/reference/api-fundamentals/go-live-checklist">
    Confirm your field mapping with the Chargeflow team and monitor your first enriched disputes before you scale up.
  </Card>
</Cards>

Testing first? See [Test your integration](https://docs.chargeflow.io/docs/merchants/testing).
