Rate limiting

You can make up to 6,000 calls every 300 seconds. Go over that and the call answers 429 Too Many Requests, after which every endpoint refuses your calls for the next 10 seconds.

Sending requests again

Give each call a connect timeout of 5 seconds and a read timeout of 15 seconds, and treat a call that times out as one that got no answer.

When a call answers 429 or 5xx, or gets no answer at all, retry it with a backoff and give up after 3 retries, as the samples below do. Correct any other 4xx before you send it again.

Wait a random time of up to 1, 2 and then 4 seconds before each retry, so that your workers do not all retry at the same moment. After a 429, add 10 seconds to the wait, because every endpoint refuses your calls until then.

Creating a transfer is safe to retry because Notabene never creates two transfers with the same ref, and Errors shows what such a retry answers.

import random
import time
import requests

def call(method, url, attempts=3, **kwargs):
    for attempt in range(attempts + 1):
        try:
            response = requests.request(method, url, timeout=(5, 15), **kwargs)
        except (requests.ConnectionError, requests.Timeout):
            response = None
        status = response.status_code if response is not None else None
        retry = status is None or status == 429 or status >= 500
        if not retry or attempt == attempts:
            return response
        wait = 10 if status == 429 else 0
        time.sleep(wait + random.uniform(0, 2 ** attempt))
async function call(url, options, attempts = 3) {
  for (let attempt = 0; ; attempt++) {
    let response = null;
    try {
      response = await fetch(url, { ...options, signal: AbortSignal.timeout(15000) });
    } catch (error) {}
    const status = response ? response.status : null;
    const retry = status === null || status === 429 || status >= 500;
    if (!retry || attempt === attempts) return response;
    const wait = (status === 429 ? 10000 : 0) + Math.random() * 1000 * 2 ** attempt;
    await new Promise(done => setTimeout(done, wait));
  }
}
import random
import time
import requests

def call(method, url, attempts=3, **kwargs):
    for attempt in range(attempts + 1):
        try:
            response = requests.request(method, url, timeout=(5, 15), **kwargs)
        except (requests.ConnectionError, requests.Timeout):
            response = None
        status = response.status_code if response is not None else None
        retry = status is None or status == 429 or status >= 500
        if not retry or attempt == attempts:
            return response
        wait = 10 if status == 429 else 0
        time.sleep(wait + random.uniform(0, 2 ** attempt))
async function call(url, options, attempts = 3) {
  for (let attempt = 0; ; attempt++) {
    let response = null;
    try {
      response = await fetch(url, { ...options, signal: AbortSignal.timeout(15000) });
    } catch (error) {}
    const status = response ? response.status : null;
    const retry = status === null || status === 429 || status >= 500;
    if (!retry || attempt === attempts) return response;
    const wait = (status === 429 ? 10000 : 0) + Math.random() * 1000 * 2 ** attempt;
    await new Promise(done => setTimeout(done, wait));
  }
}

Did this page help you?