createSumeClient custom fetch: log ratelimit-remaining as you go

Pass your own fetch to createSumeClient to warn when ratelimit-remaining runs low, without wrapping every SDK call. Works on Node 18+, Bun, Deno, Workers.

3 min readSume
All posts

The seam

createSumeClient takes three options: apiKey (required), baseUrl, and fetch. The docs describe fetch as the place to supply your own function for instrumentation, retries or tests. The default is the runtime's globalThis.fetch, and the package has no runtime dependencies, so a wrapper is all you need.

Every generated operation and every helper such as waitForJob goes through that one function, which makes it the right place to watch the rate-limit headers instead of sprinkling logging at call sites.

Headers worth logging (read 2026-10-05)

Rate-limit headers from the Sume authentication page, read 2026-10-05
HeaderMeaning
ratelimit-limitRequests permitted in the current window
ratelimit-remainingRequests left in the current window
ratelimit-resetSeconds until the window resets
retry-afterSeconds to wait, sent on 429

A logging fetch

The headers describe the budget the current request spent from, read or write, so log the method with the number. This wrapper warns under 50 remaining and passes the response through untouched, so the SDK's own retry logic still sees it.

import { createSumeClient } from "@sume-com/sdk";

const loggingFetch: typeof fetch = async (input, init) => {
  const res = await fetch(input, init);
  const left = res.headers.get("ratelimit-remaining");
  if (left !== null && Number(left) < 50) {
    console.warn(
      "sume budget low:", (init?.method ?? "GET"), left,
      "reset in", res.headers.get("ratelimit-reset"), "s",
    );
  }
  return res;
};

export const client = createSumeClient({
  apiKey: process.env.SUME_API_KEY!,
  fetch: loggingFetch,
});

Keep it a pure observer

Do not add your own retries inside this wrapper. The client already retries 408, 429 and 5xx twice with backoff and jitter, and it retries a POST only when the request carries an Idempotency-Key. Retrying a keyless POST here would start and bill a second job. Log, count, and return the response.

Sources

Related posts

More in Developers

All Developers posts

Written by Sume