{
  "openapi": "3.1.0",
  "info": {
    "title": "OSD Affiliates — Server Connect API",
    "version": "2.0.0",
    "description": "Product servers report registrations, sales, refunds and chargebacks of referred customers. Each request carries the integration key in the `x-api-key` header (`x_api_key` is accepted too) and must come from the IP address or CIDR range set for the product. Commission is calculated server-side from the product's rate for the affiliate's level."
  },
  "servers": [
    {
      "url": "https://affiliate.getosd.com/partner-events",
      "description": "Partner events"
    }
  ],
  "tags": [
    {
      "name": "Events",
      "description": "Partner event ingestion"
    }
  ],
  "paths": {
    "/registrations": {
      "post": {
        "tags": [
          "Events"
        ],
        "summary": "Report a registration",
        "description": "Analytics only: creates no sale and no commission.",
        "security": [
          {
            "XApiKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegistrationRequest"
              },
              "example": {
                "affiliate_ref": "6a4eb2e9dc5877df7f44",
                "external_event_id": "reg-2026-00001",
                "user_email": "user@example.com",
                "registered_at": "2026-05-08T21:00:00Z",
                "status": "approved",
                "metadata_json": {
                  "campaign": "summer_launch"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted, or a duplicate (`duplicate_ignored: true`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EventAccepted"
                }
              }
            }
          },
          "400": {
            "description": "Invalid payload: `invalid_json`, `invalid_affiliate`, `invalid_external_event_id`, field-specific codes, `unknown_sale`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "`forbidden`: no key, unknown or inactive key, or the source IP is not allowed for this key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "413": {
            "description": "Body larger than 64 KB."
          },
          "429": {
            "description": "`rate_limited`: too many requests from this IP; retry after `Retry-After` seconds.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "`internal_error`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/sales": {
      "post": {
        "tags": [
          "Events"
        ],
        "summary": "Report a sale",
        "description": "Send one event for every successful payment, renewals included. The commission is held for the product's hold period.",
        "security": [
          {
            "XApiKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SaleRequest"
              },
              "example": {
                "affiliate_ref": "6a4eb2e9dc5877df7f44",
                "external_event_id": "sale-2026-00001",
                "user_email": "buyer@example.com",
                "amount": 159.99,
                "currency": "USD",
                "sold_at": "2026-05-08T21:10:00Z"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted, or a duplicate (`duplicate_ignored: true`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EventAccepted"
                }
              }
            }
          },
          "400": {
            "description": "Invalid payload: `invalid_json`, `invalid_affiliate`, `invalid_external_event_id`, field-specific codes, `unknown_sale`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "`forbidden`: no key, unknown or inactive key, or the source IP is not allowed for this key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "413": {
            "description": "Body larger than 64 KB."
          },
          "429": {
            "description": "`rate_limited`: too many requests from this IP; retry after `Retry-After` seconds.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "`internal_error`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/refunds": {
      "post": {
        "tags": [
          "Events"
        ],
        "summary": "Report a refund",
        "description": "Names the original sale of this product. The commission stops being payable; if it was already paid out, the reply says so (`commission_already_paid`).",
        "security": [
          {
            "XApiKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ReversalRequest"
              },
              "example": {
                "sale_external_event_id": "sale-2026-00001",
                "occurred_at": "2026-06-01T09:00:00Z",
                "reason": "customer refund"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted, or a duplicate (`duplicate_ignored: true`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ReversalAccepted"
                }
              }
            }
          },
          "400": {
            "description": "Invalid payload: `invalid_json`, `invalid_affiliate`, `invalid_external_event_id`, field-specific codes, `unknown_sale`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "`forbidden`: no key, unknown or inactive key, or the source IP is not allowed for this key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "413": {
            "description": "Body larger than 64 KB."
          },
          "429": {
            "description": "`rate_limited`: too many requests from this IP; retry after `Retry-After` seconds.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "`internal_error`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/chargebacks": {
      "post": {
        "tags": [
          "Events"
        ],
        "summary": "Report a chargeback",
        "description": "Same shape and behaviour as a refund.",
        "security": [
          {
            "XApiKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ReversalRequest"
              },
              "example": {
                "sale_external_event_id": "sale-2026-00001",
                "occurred_at": "2026-06-01T09:00:00Z",
                "reason": "issuer dispute"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted, or a duplicate (`duplicate_ignored: true`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ReversalAccepted"
                }
              }
            }
          },
          "400": {
            "description": "Invalid payload: `invalid_json`, `invalid_affiliate`, `invalid_external_event_id`, field-specific codes, `unknown_sale`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "`forbidden`: no key, unknown or inactive key, or the source IP is not allowed for this key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "413": {
            "description": "Body larger than 64 KB."
          },
          "429": {
            "description": "`rate_limited`: too many requests from this IP; retry after `Retry-After` seconds.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "`internal_error`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "XApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key"
      }
    },
    "schemas": {
      "RegistrationRequest": {
        "type": "object",
        "required": [
          "external_event_id",
          "status"
        ],
        "properties": {
          "affiliate_ref": {
            "type": "string",
            "description": "Affiliate ID from the referral link (`?ref=`). Either this or `affiliate_user_id`."
          },
          "affiliate_user_id": {
            "type": "string",
            "description": "Affiliate's user ID, when `affiliate_ref` is not known."
          },
          "external_event_id": {
            "type": "string",
            "maxLength": 128,
            "description": "Your unique ID of this event; repeats are ignored."
          },
          "user_email": {
            "type": "string",
            "description": "Customer email (visible to the programme administrator only)."
          },
          "registered_at": {
            "type": "string",
            "description": "RFC 3339 (`2026-05-08T21:10:00Z`) or `2026-05-08 21:10:00` (UTC)."
          },
          "occurred_at": {
            "type": "string",
            "description": "Used when `registered_at` is absent. RFC 3339 (`2026-05-08T21:10:00Z`) or `2026-05-08 21:10:00` (UTC)."
          },
          "status": {
            "type": "string",
            "enum": [
              "approved",
              "rejected",
              "duplicate"
            ]
          },
          "metadata_json": {
            "description": "Any JSON value, stored as is."
          }
        }
      },
      "SaleRequest": {
        "type": "object",
        "required": [
          "external_event_id",
          "amount",
          "currency"
        ],
        "properties": {
          "affiliate_ref": {
            "type": "string",
            "description": "Affiliate ID from the referral link (`?ref=`). Either this or `affiliate_user_id`."
          },
          "affiliate_user_id": {
            "type": "string"
          },
          "external_event_id": {
            "type": "string",
            "maxLength": 128
          },
          "user_email": {
            "type": "string"
          },
          "amount": {
            "type": "number",
            "description": "Payment amount, not 0. A JSON number."
          },
          "currency": {
            "type": "string",
            "enum": [
              "USD",
              "EUR",
              "GBP",
              "ILS",
              "NIS"
            ],
            "description": "NIS is stored as ILS."
          },
          "sold_at": {
            "type": "string",
            "description": "RFC 3339 (`2026-05-08T21:10:00Z`) or `2026-05-08 21:10:00` (UTC)."
          },
          "occurred_at": {
            "type": "string",
            "description": "Used when `sold_at` is absent. RFC 3339 (`2026-05-08T21:10:00Z`) or `2026-05-08 21:10:00` (UTC)."
          }
        }
      },
      "ReversalRequest": {
        "type": "object",
        "required": [
          "sale_external_event_id"
        ],
        "properties": {
          "external_event_id": {
            "type": "string",
            "description": "Your ID of the refund event (optional, for the event log)."
          },
          "sale_external_event_id": {
            "type": "string",
            "description": "`external_event_id` of the original sale."
          },
          "occurred_at": {
            "type": "string",
            "description": "When the refund happened; now if absent. RFC 3339 (`2026-05-08T21:10:00Z`) or `2026-05-08 21:10:00` (UTC)."
          },
          "reason": {
            "type": "string",
            "maxLength": 500
          }
        }
      },
      "EventAccepted": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "const": true
          },
          "duplicate_ignored": {
            "type": "boolean",
            "description": "Present on a repeated `external_event_id`."
          },
          "event_type": {
            "type": "string",
            "enum": [
              "registration",
              "sale"
            ]
          },
          "event_id": {
            "type": "string",
            "description": "ID of the created record (absent on a duplicate)."
          },
          "external_event_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "created",
              "already_exists"
            ]
          }
        }
      },
      "ReversalAccepted": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "const": true
          },
          "duplicate_ignored": {
            "type": "boolean"
          },
          "event_type": {
            "type": "string",
            "enum": [
              "refund",
              "chargeback"
            ]
          },
          "sale_id": {
            "type": "string"
          },
          "sale_external_event_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "reversed",
              "already_refund",
              "already_chargeback"
            ]
          },
          "commission_already_paid": {
            "type": "boolean"
          },
          "note": {
            "type": "string"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": [
          "ok",
          "error",
          "message"
        ],
        "properties": {
          "ok": {
            "type": "boolean",
            "const": false
          },
          "error": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}
