Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ahallora
Last active July 18, 2022 08:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahallora/07977f0b79785a0602872f5fdba881e4 to your computer and use it in GitHub Desktop.
Save ahallora/07977f0b79785a0602872f5fdba881e4 to your computer and use it in GitHub Desktop.
Webflow API rate limiter workaround (ES6 / Nodejs)

Webflow API rate limit workaround

This is a naive work-around to the glitchy rate limiter of the current Webflow API. Any suggestions to optimizations is much appreciated.

How to use

It's a wrapper to your existing webflow-api functions, where you define two parameters:

  1. a collection ID and
  2. the function you want to call

If you have reached the rate limit this wrapper will handle the error and try to reset the rate limiter count by basically spamming until a new pool of requests has been granted.

Pro: easy to setup and integrate into existing solution Con: your script might time-out if the Webflow API does not provide a new pool fast enough

Example

This was your function before:

const getCollection = async ({ collectionId }) =>
    await webflow.items({ collectionId });

... and this is your function with this wrapper:

const getCollection = async ({ collectionId }) =>
    await webflowRateLimiterWorkaround({
        collectionId,
        callback: async () => await webflow.items({ collectionId }),
    });
import { webflowRateLimiterWorkaround } from "webflowAPI-ratelimit-workaround.js";
/* don't forget to also import and configure webflow-api */
const getCollection = async ({collectionId}) => {
return await webflowRateLimiterWorkaround({
collectionId,
callback: async () =>
await webflow.items(
{ collectionId },
{
limit: 100,
offset: 0,
}
),
});
}
const _debugFill = async ({collectionId}) => {
const hitIt = async () => {
return await webflow.items(
{ collectionId },
{
limit: 1,
offset: 0,
}
);
};
for (let i = 0; i < 100; i++) {
const res = await webflowRateLimiterWorkaround({
collectionId,
callback: async () => await hitIt(),
});
}
};
const sleep = async ms => await new Promise(r => setTimeout(r, ms));
export const webflowRateLimiterWorkaround = async ({ collectionId, callback }) => {
while (true) {
const hitIt = async () => {
const x = await webflow.items(
{ collectionId },
{
limit: 1,
offset: 0,
}
);
return x["_meta"].rateLimit.remaining;
};
try {
const response = await callback();
let webflowRateLimitRemaining = response["_meta"].rateLimit.remaining;
console.log({ webflowRateLimitRemaining });
if (webflowRateLimitRemaining === 0) {
console.log( "webflow rate limit. hit it again until reset *sigh*");
let hasReset = false;
while (!hasReset) {
const result = await hitIt();
if (result > webflowRateLimitRemaining) {
hasReset = true;
return await callback();
}
}
} else {
return response;
}
} catch (e) {
console.log(e.code || e);
await sleep(1000);
continue;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment