Server API

POST /api/v0/conversions — sales and refunds from your server with an API key, which sales a refund can name, and every response.

POST /api/v0/conversions is how your server reports sales and refunds. Every request carries a secret API key, which makes it one of the two trusted routes. The other is the Shopify webhook.

It is v0, and not yet a stable public contract. We tell the people using it before it changes, except where a security or legal fix can't wait.

Authentication#

Send Authorization: Bearer yonto_sk_…. Keys are created in Settings → API keys. The secret is shown once and stored only as a hash. A key belongs to one business, and reports only against that business's campaigns and orders.

The endpoint sends no CORS headers, so a browser can't call it. A missing or wrong key is a 401. The limit is 120 requests a minute per business; see Rate limits.

Report a sale#

bash
curl -X POST https://yontosales.com/api/v0/conversions \
  -H 'Authorization: Bearer yonto_sk_your_key' \
  -H 'Content-Type: application/json' \
  -d '{
    "tracking_code": "YOUR_TRACKING_CODE",
    "order_id": "ORDER-1234",
    "order_value": 49.90,
    "currency": "GBP",
    "click_id": "the ref value from the tracked link"
  }'
ParameterTypeDescription
tracking_coderequiredstringThe campaign. A code belonging to another business is a 404, the same as an unknown one.
order_idrequiredstringYour id for the order. A retry with the same order_id counts once. A refund names its sale by this id. Required for a sale; optional for a signup or lead.
order_valuenumber | stringRequired for a sale campaign: greater than 0, at most 1,000,000. Discarded for a signup or lead.
currencystringISO-4217, e.g. GBP. Without it, the campaign's currency is used.
click_iduuidThe ref value the tracked link added to the address. Stored when it is a click of this campaign and the sale reaches us within the business's click window after it: 30 days unless it is changed in Settings, at most 365. A click id of any age can be sent; the window decides whether it still credits the sale. A click of this campaign's older than the window is refused with a 422, and nothing is recorded. See how long a click counts. A click id we don't recognise is not stored, and the sale is recorded with no click. A sale sent with no click id is recorded as your own statement that it is this campaign's, and the campaign's report counts it as reported by the business itself.
kind"sale"Optional. A sale is the default.
Note.always send the order id, with the same value you'll send with its refund. A refund finds its sale by that id and nothing else. This holds for sales recorded by the pixel too: the id in proof('conversion', …) is the order_id a refund names.

Report a refund#

bash
curl -X POST https://yontosales.com/api/v0/conversions \
  -H 'Authorization: Bearer yonto_sk_your_key' \
  -H 'Content-Type: application/json' \
  -d '{
    "kind": "refund",
    "order_id": "ORDER-1234",
    "refund_id": "REFUND-5678",
    "amount": 20.00,
    "currency": "GBP"
  }'
ParameterTypeDescription
kindrequired"refund"Marks the request as a refund.
order_idrequiredstringThe order_id the sale was reported with, through this API or by the pixel.
refund_idrequiredstringYour id for this refund. A retry with the same refund_id counts once. One order can have several refunds, each with its own id.
amountnumber | stringGreater than 0. Without it, the refund is whatever of the sale has not been refunded yet.
currencystringOptional. When sent, it must be the sale's currency.

tracking_code is not needed: the order already belongs to a campaign. A refund is recorded as its own entry with a negative value, linked to its sale. The sale itself is never changed, so both stay visible on the campaign page and in the CSV export.

Which sales a refund can name#

  • Sales reported through this API, and sales recorded by the pixel. A refund comes from your server either way, whichever route reported the sale.
  • Not Shopify orders. Their refunds arrive automatically from Shopify's refund webhook, and a second report would count them twice. A refund naming one, by Shopify's order id or its name such as #1001, is refused with 409 shopify order.
  • Not sales added by hand on the campaign page. They have no order id, so no request can name one.

When the pixel reports the same order#

A store running both the pixel and this API reports each order twice. When the two reports agree, they are one sale. They agree when they share the order id, the campaign, the currency and the value to the cent, and arrive within 30 days of each other.

  • This API's record stands. Its value, currency and campaign are kept. If it sent no click_id and the pixel did, the pixel's click is kept, so the sale is still credited through the tracked link.
  • Whichever arrives first. If the pixel's report is already recorded, this API's report takes it over; any refund already recorded against it stays with it. If this API's report is first, the pixel's answers duplicate: true.
  • When they disagree on the campaign, the currency, the value or the timing, both are kept and the order counts twice. A refund naming that order is refused with 409 order recorded twice, because it can't say which to reverse.

Matching depends on the two routes sending the same order id. On WooCommerce, the pixel sends get_order_number(); with an order-numbering plugin, that differs from the order's internal id. The snippet below sends get_order_number() as well.

Never more than the sale#

A refund larger than what is left of the sale is refused, not reduced. The response carries remaining. Once a sale is refunded in full, a further refund is a 409.

Two refunds of the same sale sent at the same moment are checked against each other after they are written. If together they exceed the sale, the one that finds out removes itself and answers 422 refund exceeds remaining. Sending it again gets the answer for the sale as it then stands.

WooCommerce#

Orders and refunds from a WooCommerce store, sent by its own server. It reads the click id the pixel keeps in the proof_ref cookie at checkout, and sends the same order id, total and currency the pixel sends.

WooCommerce (PHP)
<?php
// Yonto: report each order, and each refund, from your WooCommerce server.
// In a small plugin or your child theme's functions.php.

const YONTO_API_KEY = 'yonto_sk_your_key';         // Settings → API keys. Never in a template.
const YONTO_TRACKING_CODE = 'YOUR_TRACKING_CODE';  // The same code as the pixel tag's data-proof.

function yonto_post($body) {
  wp_remote_post('https://yontosales.com/api/v0/conversions', [
    'timeout' => 5,
    'headers' => [
      'Authorization' => 'Bearer ' . YONTO_API_KEY,
      'Content-Type'  => 'application/json',
    ],
    'body' => wp_json_encode($body),
  ]);
}

// At checkout: keep the click id our pixel stored in the proof_ref cookie.
add_action('woocommerce_checkout_create_order', function ($order) {
  if (!empty($_COOKIE['proof_ref'])) {
    $order->update_meta_data('_yonto_click_id', sanitize_text_field(wp_unslash($_COOKIE['proof_ref'])));
  }
});

// When the order is paid. get_order_number(), get_total() and get_currency()
// are what the pixel sends too, so the two reports are one order.
// Both statuses: woocommerce_payment_complete does not fire for bank
// transfer, cheque or cash on delivery. A repeat is counted once.
foreach (['processing', 'completed'] as $status) {
  add_action('woocommerce_order_status_' . $status, function ($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return;
    yonto_post([
      'tracking_code' => YONTO_TRACKING_CODE,
      'order_id'      => (string) $order->get_order_number(),
      'order_value'   => (float) $order->get_total(),
      'currency'      => $order->get_currency(),
      'click_id'      => $order->get_meta('_yonto_click_id') ?: null,
    ]);
  });
}

// Each refund, partial or full.
add_action('woocommerce_order_refunded', function ($order_id, $refund_id) {
  $order  = wc_get_order($order_id);
  $refund = wc_get_order($refund_id);
  if (!$order || !$refund) return;
  yonto_post([
    'kind'      => 'refund',
    'order_id'  => (string) $order->get_order_number(),
    'refund_id' => (string) $refund_id,
    'amount'    => (float) $refund->get_amount(),
    'currency'  => $order->get_currency(),
  ]);
}, 10, 2);

Responses#

Every response is JSON. A duplicate is a 200 with ok: true: what you are reporting is on file. Errors carry a detail sentence.

200 — sale recorded
{ "ok": true }
200 — refund recorded
{ "ok": true, "refunded": 20, "remaining": 29.9, "currency": "GBP" }
200 — already recorded, nothing changed
{ "ok": true, "duplicate": true }
422 — more than is left of the sale
{
  "ok": false,
  "error": "refund exceeds remaining",
  "detail": "Only 29.9 GBP of this sale is left to refund. Nothing was recorded.",
  "remaining": 29.9,
  "currency": "GBP"
}
ParameterTypeDescription
400 bad jsonerrorThe body did not parse as JSON.
400 unsupported kinderrorkind was neither "sale" nor "refund".
400 invaliderrorA sale with no tracking_code.
400 value requirederrorA sale with a missing, zero, negative or over-1,000,000 order_value.
400 order id requirederrorA sale or a refund with no order_id.
400 refund id requirederrorA refund with no refund_id.
400 bad amounterrorA refund amount that is not a number greater than 0, or is over 1,000,000.
400 bad currencyerrorA currency that is not an ISO-4217 code.
400 bad click_iderrorA click_id that is not a UUID.
422 click older than N dayserrorThe click_id is a click of this campaign recorded more than N days before the sale reached us, N being the business's click window (30 unless changed). Nothing was recorded; a sale sent without click_id is recorded as your own statement.
401 unauthorizederrorNo key, or one that doesn't match a live key.
404 unknown campaignerrorNo campaign of this business has that tracking code.
404 unknown ordererrorNo sale of this business was reported with that order_id, through this API or the pixel.
409 shopify ordererrorThe order is a Shopify order. Its refunds arrive automatically from Shopify.
409 order recorded twiceerrorA refund named an order the pixel and this API reported with different details, so both are counted. Nothing was recorded.
409 refund id in useerrorThe refund_id was already used for a different order.
409 already fully refundederrorNothing is left of the sale to refund.
422 refund exceeds remainingerrorMore than is left of the sale, or another refund of it landed at the same moment. Carries remaining and currency. Nothing was recorded.
422 currency mismatcherrorThe refund's currency is not the sale's. Carries the sale's currency.
422 nothing to refunderrorThe order is a conversion with no value, such as a signup.
429 rate_limitederrorOver 120 requests a minute. Carries a Retry-After header in seconds.
500 dberrorA read or write failed. Nothing was recorded. Safe to retry.
500 refund not reconcilederrorThe refund was written, then found to exceed the sale, and its removal could not be confirmed. Sending it again with the same refund_id retries the removal.