Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/content/docs/browser-rendering/how-to/rate-limiting.mdx
Original file line number Diff line number Diff line change
@@ -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/<accountId>/browser-rendering/content', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <your-token>',
},
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 */);
}
```
Loading