Skip to content

Commit bc9496e

Browse files
daisyfaithaumakodster28hyperlint-ai[bot]meddullakathayl
authored
[Browser Rendering] quick actions API (#20221)
* Guardails docs * Initial commit * quick actions endpoints * Fix * change to bullets * moved get started to a file * summary * summar of endpoints * code samples * code samples and title change * Update src/content/docs/browser-rendering/quick-actions-rest-api/scrape-endpoint.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/browser-rendering/quick-actions-rest-api/scrape-endpoint.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * director listing * added code sample * index descriptions * redirects * Update src/content/docs/browser-rendering/quick-actions-rest-api/content-endpoint.mdx Co-authored-by: Sofia <[email protected]> * delete custom css * fixes from responses * Update src/content/docs/browser-rendering/quick-actions-rest-api/scrape-endpoint.mdx Co-authored-by: Sofia <[email protected]> * code fixes * parameters added * Update get-started.mdx updated where link to quick actions api lives * Update src/content/docs/browser-rendering/quick-actions-rest-api/content-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/content-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/index.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/content-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/pdf-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/scrape-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/scrape-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Sofia <[email protected]> * minor fixes * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/content-endpoint.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/workers-binding-api/index.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/snapshot.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/pdf-endpoint.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Maddy <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/content-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/pdf-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/pdf-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/pdf-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/pdf-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * Update src/content/docs/browser-rendering/quick-actions-rest-api/screenshot-endpoint.mdx Co-authored-by: Sofia <[email protected]> * minor fixes * links fix * Update src/content/docs/browser-rendering/workers-binding-api/reuse-sessions.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * code fix * removed link * Fix error * Added beta badge * Update public/_redirects * Small edit --------- Co-authored-by: kodster28 <[email protected]> Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: Sofia <[email protected]> Co-authored-by: Kathy <[email protected]> Co-authored-by: Maddy <[email protected]>
1 parent 29d3a9f commit bc9496e

File tree

15 files changed

+461
-17
lines changed

15 files changed

+461
-17
lines changed

public/_redirects

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@
179179
/bots/reference/verified-bot-categories/ /bots/concepts/bot/verified-bots/categories/ 301
180180
/bots/reference/verified-bot-policy/ /bots/concepts/bot/verified-bots/policy/ 301
181181

182+
#browser-rendering
183+
/browser-rendering/get-started/browser-rendering-with-do/ /browser-rendering/workers-binding-api/browser-rendering-with-do/ 301
184+
/browser-rendering/get-started/reuse-sessions/ /browser-rendering/workers-binding-api/reuse-sessions/ 301
185+
/browser-rendering/get-started/screenshots/ /browser-rendering/workers-binding-api/screenshots/ 301
186+
182187
# byoip
183188
/byoip/about/dynamic-advertisement/ /byoip/concepts/dynamic-advertisement/ 301
184189
/byoip/best-practices/dynamic-advertisement/ /byoip/concepts/dynamic-advertisement/best-practices/ 301
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Get started
4+
sidebar:
5+
order: 2
6+
---
7+
8+
Browser rendering can be used in two ways:
9+
10+
- [Workers Binding API](/browser-rendering/workers-binding-api) for complex scripts.
11+
- [Quick Actions REST API](/browser-rendering/quick-actions-rest-api/) for simple actions.

src/content/docs/browser-rendering/get-started/index.mdx

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/content/docs/browser-rendering/how-to/pdf-generation.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar:
77

88
import { Aside, WranglerConfig } from "~/components";
99

10-
As seen in the [Getting Started guide](/browser-rendering/get-started/screenshots/), Browser Rendering can be used to generate screenshots for any given URL. Alongside screenshots, you can also generate full PDF documents for a given webpage, and can also provide the webpage markup and style ourselves.
10+
As seen in the [Getting Started guide](/browser-rendering/workers-binding-api/screenshots/), Browser Rendering can be used to generate screenshots for any given URL. Alongside screenshots, you can also generate full PDF documents for a given webpage, and can also provide the webpage markup and style ourselves.
1111

1212
## Prerequisites
1313

@@ -26,7 +26,8 @@ npm install @cloudflare/puppeteer --save-dev
2626
3. Add your Browser Rendering binding to your new Wrangler configuration:
2727

2828
<WranglerConfig>
29-
```toml
29+
30+
```toml title="wrangler.toml"
3031
browser = { binding = "BROWSER" }
3132
```
3233
</WranglerConfig>

src/content/docs/browser-rendering/platform/puppeteer.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { TabItem, Tabs } from "~/components";
1212

1313
Puppeteer typically connects to a local Chrome or Chromium browser using the DevTools port. Refer to the [Puppeteer API documentation on the `Puppeteer.connect()` method](https://pptr.dev/api/puppeteer.puppeteer.connect) for more information.
1414

15-
The Workers team forked a version of Puppeteer and patched it to connect to the Workers Browser Rendering API instead. The [changes between Workers Puppeteer fork and the Puppeteer core](https://github.com/cloudflare/puppeteer/blob/main/src/puppeteer-core.ts) are minimal. After connecting, the developers can then use the full [Puppeteer API](https://github.com/cloudflare/puppeteer/blob/main/docs/api/index.md) as they would on a standard setup.
15+
The Workers team forked a version of Puppeteer and patched it to connect to the Workers Browser Rendering API instead. After connecting, the developers can then use the full [Puppeteer API](https://github.com/cloudflare/puppeteer/blob/main/docs/api/index.md) as they would on a standard setup.
1616

1717
Our version is open sourced and can be found in [Cloudflare's fork of Puppeteer](https://github.com/cloudflare/puppeteer). The npm can be installed from [npmjs](https://www.npmjs.com/) as [@cloudflare/puppeteer](https://www.npmjs.com/package/@cloudflare/puppeteer):
1818

@@ -68,7 +68,7 @@ This script [launches](https://pptr.dev/api/puppeteer.puppeteernode.launch) the
6868

6969
### Keep Alive
7070

71-
If users omit the `browser.close()` statement, it will stay open, ready to be connected to again and [re-used](/browser-rendering/get-started/reuse-sessions/) but it will, by default, close automatically after 1 minute of inactivity. Users can optionally extend this idle time up to 10 minutes, by using the `keep_alive` option, set in milliseconds:
71+
If users omit the `browser.close()` statement, it will stay open, ready to be connected to again and [re-used](/browser-rendering/workers-binding-api/reuse-sessions/) but it will, by default, close automatically after 1 minute of inactivity. Users can optionally extend this idle time up to 10 minutes, by using the `keep_alive` option, set in milliseconds:
7272

7373
```js
7474
const browser = await puppeteer.launch(env.MYBROWSER, { keep_alive: 600000 });
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Fetch HTML
4+
sidebar:
5+
order: 2
6+
---
7+
8+
The `/content` endpoint instructs the browser to navigate to a website and capture the fully rendered HTML of a page, including the `head` section, after JavaScript execution. This is ideal for capturing content from JavaScript-heavy or interactive websites.
9+
10+
## Basic usage
11+
12+
Go to `https://example.com` and return the rendered HTML.
13+
14+
```bash
15+
curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/content' \
16+
-H 'Content-Type: application/json' \
17+
-H 'Authorization: Bearer <apiToken>' \
18+
-d '{"url": "https://example.com"}'
19+
```
20+
21+
## Advanced usage
22+
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`).
24+
25+
```bash
26+
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/content' \
27+
-H 'Authorization: Bearer <apiToken>' \
28+
-H 'Content-Type: application/json' \
29+
-d '{
30+
"url": "https://cloudflare.com/",
31+
"rejectResourceTypes": ["image"],
32+
"rejectRequestPattern": ["/^.*\\.(css)"]
33+
}
34+
35+
```
36+
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.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Quick Actions REST API
4+
sidebar:
5+
order: 2
6+
group:
7+
badge: Beta
8+
---
9+
10+
The Quick Actions API is a RESTful interface that provides endpoints for common browser actions such as capturing screenshots, extracting HTML content, generating PDFs, and more.
11+
The following are the available options:
12+
13+
import { DirectoryListing } from "~/components";
14+
15+
<DirectoryListing />
16+
17+
Use the Quick Actions API when you need a fast, simple way to perform common browser tasks such as capturing screenshots, extracting HTML, or generating PDFs without writing complex scripts. If you require more advanced automation, custom workflows, or persistent browser sessions, the [Workers Binding API](/browser-rendering/workers-binding-api/) is the better choice.
18+
19+
## Before you begin
20+
21+
Before you begin, make sure you [create a custom API Token](/fundamentals/api/get-started/create-token/) with the following permissions:
22+
23+
- `Browser Rendering - Edit`
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Render PDF
4+
sidebar:
5+
order: 5
6+
---
7+
8+
The `/pdf` endpoint instructs the browser to render the webpage as a PDF document.
9+
10+
## Basic usage
11+
12+
Navigate to `https://example.com/` and inject custom CSS and an external stylesheet. Then return the rendered page as a PDF.
13+
14+
```bash
15+
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/pdf' \
16+
-H 'Authorization: Bearer <apiToken>' \
17+
-H 'Content-Type: application/json' \
18+
-d '{
19+
"url": "https://example.com/",
20+
"addStyleTag": [
21+
{ "content": "body { font-family: Arial; }" },
22+
{ "url": "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" }
23+
]
24+
}' \
25+
--output "output.pdf"
26+
```
27+
28+
## Advanced usage
29+
30+
Navigate to `https://example.com`, first setting an additional HTTP request header and configuring the page size (`viewport`). Then, wait until there are no more than 2 network connections for at least 500 ms, or until the maximum timeout of 4500 ms is reached, before considering the page loaded and returning the rendered PDF document.
31+
32+
The `goToOptions` parameter exposes most of [Puppeteer'd API](https://pptr.dev/api/puppeteer.gotooptions).
33+
34+
```bash
35+
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/pdf' \
36+
-H 'Authorization: Bearer <apiToken>' \
37+
-H 'Content-Type: application/json' \
38+
-d '{
39+
"url": "https://example.com/",
40+
"setExtraHTTPHeaders": {
41+
"X-Custom-Header": "value"
42+
},
43+
"viewport": {
44+
"width": 1200,
45+
"height": 800
46+
},
47+
"gotoOptions": {
48+
"waitUntil": "networkidle2",
49+
"timeout": 45000
50+
}
51+
}' \
52+
--output "advanced-output.pdf"
53+
```
54+
55+
## PDF with no images or CSS
56+
57+
Use PDF with no images or CSS if you want to accelerate the scanning process and you do not need the images.
58+
59+
```bash
60+
curl -X POST https://api.cloudflare.com/client/v4/accounts/<acccountID>/browser-rendering/pdf \
61+
-H 'Authorization: Bearer <apiToken>' \
62+
-H 'Content-Type: application/json' \
63+
-d '{
64+
"url": "https://cloudflare.com/",
65+
"rejectResourceTypes": ["image"],
66+
"rejectRequestPattern": ["/^.*\\.(css)"]
67+
}' \
68+
--output "cloudflare.pdf"
69+
```
70+
71+
## Parameters
72+
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.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Scrape HTML elements
4+
sidebar:
5+
order: 7
6+
---
7+
8+
The `/scrape` endpoint extracts structured data from specific elements on a webpage, returning details such as element dimensions and inner HTML.
9+
10+
## Basic usage
11+
12+
```bash
13+
curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-rendering/scrape' \
14+
-H 'Authorization: Bearer <apiToken>' \
15+
-H 'Content-Type: application/json' \
16+
-d '{
17+
"url": "https://example.com/",
18+
"elements": [{
19+
"selector": "h1"
20+
},
21+
{
22+
"selector": "a"
23+
}]
24+
}
25+
```
26+
27+
### JSON response
28+
29+
```json title="json response"
30+
{
31+
"success": true,
32+
"result": [
33+
{
34+
"results": [
35+
{
36+
"attributes": [],
37+
"height": 39,
38+
"html": "Example Domain",
39+
"left": 100,
40+
"text": "Example Domain",
41+
"top": 133.4375,
42+
"width": 600
43+
}
44+
],
45+
"selector": "h1"
46+
},
47+
{
48+
"results": [
49+
{
50+
"attributes": [
51+
{ "name": "href", "value": "https://www.iana.org/domains/example" }
52+
],
53+
"height": 20,
54+
"html": "More information...",
55+
"left": 100,
56+
"text": "More information...",
57+
"top": 249.875,
58+
"width": 142
59+
}
60+
],
61+
"selector": "a"
62+
}
63+
]
64+
}
65+
```
66+
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"`).
72+
73+
### Response fields
74+
75+
- `results` _(array of objects)_ - Contains extracted data for each selector.
76+
- `selector` _(string)_ - The CSS selector used.
77+
- `results` _(array of objects)_ - List of extracted elements matching the selector.
78+
- `text` _(string)_ - Inner text of the element.
79+
- `html` _(string)_ - Inner HTML of the element.
80+
- `attributes` _(array of objects)_ - List of extracted attributes such as `href` for links.
81+
- `height`, `width`, `top`, `left` _(number)_ - Position and dimensions of the element.

0 commit comments

Comments
 (0)