> Send each sale and refund from your own server to the Affiliate sales API, so partners earn on checkouts Plandalf doesn't see.

Source: https://plandalf.com/docs/affiliates/any-platform

Affiliates

# Track sales from any platform

If customers pay somewhere Plandalf can't see, your server tells Plandalf about each sale. Plandalf matches it to the partner, works out the commission and handles it like any other sale.

## How it works

1.  ### Create an API key
    
    Go to **Settings → API Keys** and create a key. Keep it on your server, never in the browser. See [Host and API keys](https://plandalf.com/docs/get-started/host-and-keys).
    
2.  ### Keep the referral with each order
    
    The Plandalf script on your site saves the partner a visitor came from in a cookie called `pf_rt`. Its value starts with `rt_`. Read it when the customer checks out and store it with the order. If you only have the partner's code, send that as `code` instead.
    
3.  ### Send each paid sale
    
    When a payment succeeds, post it to Plandalf (below).
    
4.  ### Send refunds
    
    When you refund, post that too, and the partner's commission goes down to match.
    

The same steps are in the app: **Affiliates → Settings → General → Track sales from your own server → How to track sales**.

## Send a sale

```bash
curl -X POST https://admin.plandalf.dev/api/v1/affiliate/sales \
  -H "Authorization: Bearer $PLANDALF_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "source": "my-billing",
    "external_id": "inv_1001",
    "currency": "USD",
    "amount_cents": 4900,
    "referral_token": "rt_FROM_THE_PF_RT_COOKIE",
    "customer": { "email": "buyer@example.com" }
  }'
```

-   `source` names where the sale came from. Use the same value every time.
-   `external_id` is your ID for the payment. Sending the same one twice is safe; Plandalf counts it once.
-   `amount_cents` is the amount paid, in cents.
-   For subscriptions, add `subscription_id`, and `"billing_reason": "renewal"` on renewals, so your renewal rules apply.
-   Add `"test": true` while you try it out. Test sales stay out of real payouts.

Plandalf answers `201` for a new sale and `200` for one it already had.

## Send a refund

```bash
curl -X POST https://admin.plandalf.dev/api/v1/affiliate/refunds \
  -H "Authorization: Bearer $PLANDALF_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "source": "my-billing",
    "sale_external_id": "inv_1001",
    "external_id": "refund_77",
    "amount_cents": 4900
  }'
```

`sale_external_id` is the `external_id` of the original sale.

## Credits and disputes

The API also takes credit notes and disputes, so commissions follow what happened to the payment. An open dispute holds the commission; a lost one reverses it.

Endpoint

Send it when

`POST /api/v1/affiliate/sales`

A payment succeeds

`POST /api/v1/affiliate/refunds`

You refund a payment

`POST /api/v1/affiliate/credits`

You issue a credit note

`POST /api/v1/affiliate/disputes`

A customer disputes a payment

`POST /api/v1/affiliate/disputes/{external_id}/won`, `/lost`, `/warning_closed`

The dispute is settled

Every field, response and error is in the [Affiliate sales API reference](https://plandalf.com/docs/api/affiliate-sales). Send an `Idempotency-Key` header to make retries safe. `occurred_at` must be within the last 30 days.

## Check it works

Open your test link from **Affiliates → Setup** in a private window, read the `pf_rt` cookie, and send a sale with that token and `"test": true`. Turn on **Test mode** in the Affiliates header and look for it under **Sales**.

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
