Ready-made integration examples

Node.js

A Node.js site needs no code — the TypeScript client.

const API = 'https://api.opotopo.com';

async function sync(items) {
  const response = await fetch(`${API}/rest/v1/advertisement/seller/ads/sync`, {
    method: 'POST',
    headers: {
      'X-API-Key': process.env.OPOTOPO_API_KEY,
      'Idempotency-Key': crypto.randomUUID(),
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ publish: true, items }),
  });
  if (response.status === 429) {
    const retryAfter = Number(response.headers.get('Retry-After') ?? 60);
    await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000));
    return sync(items);
  }
  if (!response.ok) throw new Error(await response.text());
  const result = await response.json();
  return result.results.filter((row) => row.error);
}

PHP

A PHP site needs no code — the PHP client, a Laravel site — the Laravel client, a WooCommerce store — the WooCommerce client, a 1C-Bitrix store — the 1C-Bitrix client.

function opotopoSync(array $items): array
{
    $retryAfter = 60;
    $curl = curl_init('https://api.opotopo.com/rest/v1/advertisement/seller/ads/sync');
    curl_setopt_array($curl, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'X-API-Key: ' . getenv('OPOTOPO_API_KEY'),
            'Idempotency-Key: ' . bin2hex(random_bytes(16)),
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => json_encode(['publish' => true, 'items' => $items]),
        CURLOPT_HEADERFUNCTION => function ($curl, string $header) use (&$retryAfter): int {
            if (stripos($header, 'Retry-After:') === 0) {
                $retryAfter = (int) trim(substr($header, strlen('Retry-After:')));
            }
            return strlen($header);
        },
    ]);
    $body = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);

    if ($status === 429) {
        sleep($retryAfter);
        return opotopoSync($items);
    }
    if ($status < 200 || $status >= 300) {
        throw new RuntimeException((string) $body);
    }

    return array_values(array_filter(
        json_decode($body, true)['results'],
        fn (array $row) => $row['error'] ?? null,
    ));
}

The working order

  1. Map your categories onto the platform categories once and store the mapping.
  2. Submit items in batches within the batch ceiling from the operation reference, not the whole catalog in a single request.
  3. Read the per-item response: remember the successful ones by the listing identifier, fix and resubmit the rejected ones.
  4. Resubmit only the items that changed — that keeps you clear of the rate limit.
  5. Put the run on a schedule of your own — the application scheduler or a cron job at your hosting provider; a catalog that changes rarely is fine with a daily run.
Was this article helpful?

Did not find the answer to your question?

Create a request