Skip to content

Commit 2fe7819

Browse files
authored
[Browser Rendering] Add api reference and improve docs (#20419)
* [Browser Rendering] Add api reference and improve docs Remove inline parameters and points to api reference instead (new ones will automatically be added to the reference and this way we dont have to update here as well) * [Browser Rendering] Fix commands --------- Co-authored-by: Sofia Cardita <[email protected]>
1 parent d534f62 commit 2fe7819

File tree

8 files changed

+66
-85
lines changed

8 files changed

+66
-85
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Build a web crawler with Queues and Browser Rendering
4+
external_link: /queues/tutorials/web-crawler-with-browser-rendering/
5+
sidebar:
6+
order: 2
7+
---
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Reference
4+
external_link: /api/resources/browser_rendering/
5+
sidebar:
6+
order: 8
7+
---

src/content/docs/browser-rendering/rest-api/content-endpoint.mdx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,18 @@ curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browse
2020

2121
## Advanced usage
2222

23-
Navigate to `https://cloudflare.com/` but block images and stylesheets from loading. Undesired requests can be blocked by resource type (`rejectResourceTypes`) or by using a regex pattern (`rejectRequestPattern`).
23+
Navigate to `https://cloudflare.com/` but block images and stylesheets from loading. Undesired requests can be blocked by resource type (`rejectResourceTypes`) or by using a regex pattern (`rejectRequestPattern`). The opposite can also be done, only allow requests that match `allowRequestPattern` or `allowResourceTypes`.
2424

2525
```bash
2626
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/content' \
2727
-H 'Authorization: Bearer <apiToken>' \
2828
-H 'Content-Type: application/json' \
2929
-d '{
3030
"url": "https://cloudflare.com/",
31-
"rejectResourceTypes": ["image"],
32-
"rejectRequestPattern": ["/^.*\\.(css)"]
33-
}
31+
"rejectResourceTypes": ["image"],
32+
"rejectRequestPattern": ["/^.*\\.(css)"]
33+
}'
3434

3535
```
3636

37-
### Parameters
38-
39-
- `url` _(string)_ - The URL of the webpage to extract content from.
40-
- `rejectResourceTypes` _(array)_ - Blocks specific resource types such as images, fonts from loading to improve performance.
41-
- `rejectRequestPattern` _(array of regex patterns)_ - Prevents loading of resources matching specified patterns such as CSS files.
37+
Many more options exist, like setting HTTP headers using `setExtraHTTPHeaders`, setting `cookies`, and using `gotoOptions` to control page load behaviour - check the endpoint [reference](/api/resources/browser_rendering/subresources/screenshot/methods/create/) for all available parameters.

src/content/docs/browser-rendering/rest-api/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pcx_content_type: navigation
33
title: REST API
44
sidebar:
5-
order: 2
5+
order: 3
66
group:
77
badge: Beta
88
---

src/content/docs/browser-rendering/rest-api/pdf-endpoint.mdx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
1919
"url": "https://example.com/",
2020
"addStyleTag": [
2121
{ "content": "body { font-family: Arial; }" },
22-
{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" }
22+
{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" }
2323
]
2424
}' \
2525
--output "output.pdf"
@@ -52,9 +52,9 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
5252
--output "advanced-output.pdf"
5353
```
5454

55-
## PDF with no images or CSS
55+
## Blocking images and styles when generating a PDF
5656

57-
Use PDF with no images or CSS if you want to accelerate the scanning process and you do not need the images.
57+
The options `rejectResourceTypes` and `rejectRequestPattern` can be used to block requests. The opposite can also be done, _only_ allow certain requests using `allowResourceTypes` and `allowRequestPattern`.
5858

5959
```bash
6060
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \
@@ -68,19 +68,22 @@ curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-
6868
--output "cloudflare.pdf"
6969
```
7070

71-
## Parameters
71+
## Generate PDF from custom HTML
7272

73-
- `url` _(string)_ - The webpage URL to render as a PDF.
74-
- `addStyleTag` _(array of objects)_ - Injects custom CSS before generating the PDF.
75-
- `content` _(string)_ - Inline CSS styles.
76-
- `url` _(string)_ - URL of an external stylesheet.
77-
- `setExtraHTTPHeaders` _(object)_ - Adds custom HTTP headers when making the request.
78-
- `X-Custom-Header` _(string)_ - Example of a custom header.
79-
- `viewport` _(object)_ - Defines the browser viewport size.
80-
- `width` _(number)_ - Viewport width in pixels.
81-
- `height` _(number)_ - Viewport height in pixels.
82-
- `gotoOptions` _(object)_ - Configures page navigation settings.
83-
- `waitUntil` _(string)_ - Defines when the browser considers the page fully loaded.
84-
- `timeout` _(number)_ - Maximum wait time before failing the request.
85-
- `rejectResourceTypes` _(array)_ - Blocks specific resource types to improve rendering performance.
86-
- `rejectRequestPattern` _(array of regex patterns)_ - Prevents loading of resources matching certain patterns.
73+
If you have HTML you'd like to generate a PDF from, the `html` option can be used. The option `addStyleTag` can be used to add custom styles.
74+
75+
```bash
76+
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \
77+
-H 'Authorization: Bearer <apiToken>' \
78+
-H 'Content-Type: application/json' \
79+
-d '{
80+
"html": "<html><body>Advanced Snapshot</body></html>",
81+
"addStyleTag": [
82+
{ "content": "body { font-family: Arial; }" },
83+
{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" }
84+
]
85+
}' \
86+
--output "invoice.pdf"
87+
```
88+
89+
Many more options exist, like setting HTTP credentials using `authenticate`, setting `cookies`, and using `gotoOptions` to control page load behaviour - check the endpoint [reference](/api/resources/browser_rendering/subresources/pdf/methods/create/) for all available parameters.

src/content/docs/browser-rendering/rest-api/scrape-endpoint.mdx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ The `/scrape` endpoint extracts structured data from specific elements on a webp
99

1010
## Basic usage
1111

12+
Go to `https://example.com` and and extract metadata from all `h1` and `a` elements in the DOM.
13+
1214
```bash
1315
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/scrape' \
1416
-H 'Authorization: Bearer <apiToken>' \
@@ -21,7 +23,7 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
2123
{
2224
"selector": "a"
2325
}]
24-
}
26+
}'
2527
```
2628

2729
### JSON response
@@ -64,11 +66,7 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
6466
}
6567
```
6668

67-
## Parameters
68-
69-
- `url` _(string)_ - The webpage to extract data from.
70-
- `elements` _(object)_ - Defines the elements to extract from the page.
71-
- `selectors` _(array of strings)_ - List of CSS selectors identifying elements to scrape (e.g., `"h1"`, `".article"`).
69+
Many more options exist, like setting HTTP credentials using `authenticate`, setting `cookies`, and using `gotoOptions` to control page load behaviour - check the endpoint [reference](/api/resources/browser_rendering/subresources/scrape/methods/create/) for all available parameters.
7270

7371
### Response fields
7472

src/content/docs/browser-rendering/rest-api/screenshot-endpoint.mdx

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,23 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
2121
"omitBackground": true
2222
}
2323
}' \
24-
--output "screenshot.webp"
24+
--output "screenshot.png"
2525
```
2626

27+
For more options to control the final screenshot, like `clip`, `captureBeyondViewport`, `fullPage` and others, check the endpoint [reference](/api/resources/browser_rendering/subresources/screenshot/methods/create/).
28+
2729
## Advanced usage
2830

29-
Navigate to `https://cloudflare.com/`, changing the page size and waiting until there are no active network connections or up to a maximum of `4500ms`. Then take a `fullPage` screenshot.
31+
Navigate to `https://cloudflare.com/`, changing the page size (`viewport`) and waiting until there are no active network connections (`waitUntil`) or up to a maximum of `4500ms` (`timeout`). Then take a `fullPage` screenshot.
3032

3133
```bash
3234
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/screenshot' \
3335
-H 'Authorization: Bearer <apiToken>' \
3436
-H 'Content-Type: application/json' \
3537
-d '{
36-
"url": "https://cloudflare.com/",
38+
"url": "https://cnn.com/",
3739
"screenshotOptions": {
38-
"fullPage": true,
39-
"omitBackground": true,
40+
"fullPage": true
4041
},
4142
"viewport": {
4243
"width": 1280,
@@ -47,19 +48,20 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
4748
"timeout": 45000
4849
}
4950
}' \
50-
--output "advanced-screenshot.webp"
51+
--output "advanced-screenshot.png"
5152
```
5253

5354
## Customize CSS and embed custom JavaScript
5455

56+
Instruct the browser to go to `https://example.com`, embed custom JavaScript (`addScriptTag`) and add extra styles (`addStyleTag`), both inline (`addStyleTag.content`) and by loading an external stylesheet (`addStyleTag.url`).
57+
5558
```bash
5659
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/screenshot' \
5760
-H 'Authorization: Bearer <apiToken>' \
5861
-H 'Content-Type: application/json' \
5962
-d '{
6063
"url": "https://example.com/",
6164
"addScriptTag": [
62-
6365
{ "content": "document.querySelector(`h1`).innerText = `Hello World!!!`" }
6466
],
6567
"addStyleTag": [
@@ -71,25 +73,7 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
7173
}
7274
]
7375
}' \
74-
--output "screenshot.webp"
76+
--output "screenshot.png"
7577
```
7678

77-
## Parameters
78-
79-
- `url` _(string)_ - The webpage URL to take a screenshot of.
80-
- `html` _(string)_ - Instead of a URL, allows rendering custom HTML for the screenshot.
81-
- `screenshotOptions` _(object)_ - Configures the screenshot format and quality.
82-
- `omitBackground` _(boolean)_ - Removes the default white background when taking a screenshot.
83-
- `viewport` _(object)_ - Sets the browser viewport dimensions for rendering.
84-
- `width` _(number)_ - Viewport width in pixels.
85-
- `height` _(number)_ - Viewport height in pixels.
86-
- `gotoOptions` _(object)_ - Configures how and when the page is considered fully loaded.
87-
- `waitUntil` _(string)_ - Defines when the browser considers navigation complete (`networkidle0`, `domcontentloaded`).
88-
- `networkidle0` - Waits until there are no more than 0 network connections for at least 500 ms before taking a screenshot.
89-
- `timeout` _(number)_ - Maximum wait time (in milliseconds) before navigation times out.
90-
- `addScriptTag` _(array of objects)_ - Injects JavaScript code before taking a screenshot.
91-
- `url` _(string)_ - Loads an external script file before rendering.
92-
- `content` _(string)_ - Runs inline JavaScript before rendering.
93-
- `addStyleTag` _(array of objects)_ - Injects CSS styles before rendering.
94-
- `content` _(string)_ - Defines inline CSS rules.
95-
- `url` _(string)_ - Loads external stylesheets before rendering.
79+
Many more options exist, like setting HTTP credentials using `authenticate`, setting `cookies`, and using `gotoOptions` to control page load behaviour - check the endpoint [reference](/api/resources/browser_rendering/subresources/screenshot/methods/create/) for all available parameters.

src/content/docs/browser-rendering/rest-api/snapshot.mdx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
3030

3131
```json title="json response"
3232
{
33-
"status": true,
33+
"success": true,
3434
"result": {
3535
"screenshot": "Base64EncodedScreenshotString",
3636
"content": "<html>...</html>"
@@ -43,9 +43,10 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
4343
The `html` property in the JSON payload, it sets the html to `<html><body>Advanced Snapshot</body></html>` then does the following steps:
4444

4545
1. Disable JavaScript.
46-
2. Changes the page size `(viewport)`.
47-
3. Waits up to `30000ms` or until the `DOMContentLoaded` event starts.
48-
4. Returns the rendered HTML content and a base-64 encoded screenshot of the page.
46+
2. Sets the screenshot to `fullPage`.
47+
3. Changes the page size `(viewport)`.
48+
4. Waits up to `30000ms` or until the `DOMContentLoaded` event fires.
49+
5. Returns the rendered HTML content and a base-64 encoded screenshot of the page.
4950

5051
```bash
5152
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/snapshot' \
@@ -54,6 +55,9 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
5455
-d '{
5556
"html": "<html><body>Advanced Snapshot</body></html>",
5657
"setJavaScriptEnabled": false,
58+
"screenshotOptions": {
59+
"fullPage": true
60+
},
5761
"viewport": {
5862
"width": 1200,
5963
"height": 800
@@ -69,30 +73,12 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
6973

7074
```json title="json response"
7175
{
72-
"status": true,
73-
"errors": [],
76+
"success": true,
7477
"result": {
7578
"screenshot": "AdvancedBase64Screenshot",
7679
"content": "<html><body>Advanced Snapshot</body></html>"
7780
}
7881
}
7982
```
8083

81-
## Parameters
82-
83-
- `url` _(string)_ - The URL of the page to snapshot.
84-
- `html` _(string)_ - Allows passing custom HTML instead of a URL.
85-
- `setJavaScriptEnabled` _(boolean)_ - Enables or disables JavaScript execution on the page.
86-
- `viewport` \*(object)- Sets the rendering viewport dimensions.
87-
- `width` _(number)_ - Width in pixels.
88-
- `height` _(number)_ - Height in pixels.
89-
- `gotoOptions` _(object)_ - Determines when the page is fully loaded.
90-
- `waitUntil` _(string)_ - Defines the loading strategy (`domcontentloaded`, `networkidle2`).
91-
- `timeout` _(number)_ - Timeout duration in milliseconds.
92-
- `allowResourceTypes` _(array of strings)_ - Restricts the types of resources allowed to load.
93-
- Example: [`document`, `script`] - Only allows HTML documents and scripts to load, preventing images, stylesheets, and other resources.
94-
95-
### Response fields
96-
97-
- `screenshot` _(string)_ - Base64-encoded image of the rendered page.
98-
- `content` _(string)_ - Fully rendered HTML of the page.
84+
Many more options exist, like setting HTTP credentials using `authenticate`, setting `cookies`, and using `gotoOptions` to control page load behaviour - check the endpoint [reference](/api/resources/browser_rendering/subresources/screenshot/methods/create/) for all available parameters.

0 commit comments

Comments
 (0)