diff --git a/src/content/docs/browser-rendering/how-to/rate-limiting.mdx b/src/content/docs/browser-rendering/how-to/rate-limiting.mdx new file mode 100644 index 000000000000000..263016f78d13dad --- /dev/null +++ b/src/content/docs/browser-rendering/how-to/rate-limiting.mdx @@ -0,0 +1,33 @@ +--- +pcx_content_type: how-to +title: Rate limiting REST API requests +sidebar: + order: 5 +--- + +## Example: Handling 429 Rate Limits with Retry-After Header + +When you make too many requests in a short period of time, Browser Rendering will respond with HTTP status code `429 Too Many Requests`. +The response includes a `Retry-After` header, which specifies how many seconds to wait before retrying. You can view your account's rate limits on the [Limits](/browser-rendering/platform/limits/) page. + +The example below demonstrates how to handle rate limiting gracefully by reading the `Retry-After` value and retrying the request after that delay. + +```js +const response = await fetch('https://api.cloudflare.com/client/v4/accounts//browser-rendering/content', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ', + }, + body: JSON.stringify({ url: 'https://example.com' }) +}); + +if (response.status === 429) { + const retryAfter = response.headers.get('Retry-After'); + console.log(`Rate limited. Waiting ${retryAfter} seconds...`); + await new Promise(resolve => setTimeout(resolve, retryAfter * 1000)); + + // Retry the request + const retryResponse = await fetch(/* same request as above */); +} +```