Skip to content

Commit a96a764

Browse files
[BRAPI]typescript SDK tab (#21473)
* typescript api * more sdk * json typescript * Update json-endpoint.mdx * update * Update json-endpoint.mdx * removed json * typescript SDK * labelling
1 parent 2c67326 commit a96a764

File tree

8 files changed

+194
-18
lines changed

8 files changed

+194
-18
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ sidebar:
55
order: 2
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
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.
911

1012
## Basic usage
1113

14+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
15+
1216
Go to `https://example.com` and return the rendered HTML.
1317

1418
```bash
@@ -18,6 +22,25 @@ curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browse
1822
-d '{"url": "https://example.com"}'
1923
```
2024

25+
</TabItem> <TabItem label="TypeScript SDK">
26+
27+
```typescript
28+
import Cloudflare from "cloudflare";
29+
30+
const client = new Cloudflare({
31+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
32+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
33+
});
34+
35+
const content = await client.browserRendering.content.create({
36+
account_id: "account_id",
37+
});
38+
39+
console.log(content);
40+
```
41+
42+
</TabItem> </Tabs>
43+
2144
## Advanced usage
2245

2346
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`.

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar:
55
order: 9
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
The `/json` endpoint extracts structured data from a webpage. You can specify the expected output using either a `prompt` or a `response_format` parameter which accepts a JSON schema. The endpoint returns the extracted data in JSON format.
911

1012
:::note[Note]
@@ -15,6 +17,8 @@ The `/json` endpoint leverages [Workers AI](/workers-ai/) for data extraction. U
1517

1618
## Basic Usage
1719

20+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
21+
1822
### With a Prompt and JSON schema
1923

2024
This example captures webpage data by providing both a prompt and a JSON schema. The prompt guides the extraction process, while the JSON schema defines the expected structure of the output.
@@ -54,7 +58,6 @@ curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID
5458
}'
5559
```
5660

57-
5861
```json output collapse={15-48}
5962
{
6063
"success": true,
@@ -100,7 +103,6 @@ curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID
100103
}'
101104
```
102105

103-
104106
```json output
105107

106108
"success": true,
@@ -151,7 +153,6 @@ curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID
151153
}'
152154
```
153155

154-
155156
```json output collapse={13-68}
156157
{
157158
"success": true,
@@ -221,3 +222,24 @@ curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID
221222
}
222223
}
223224
```
225+
226+
</TabItem> <TabItem label="TypeScript SDK">
227+
228+
Below is an example using the TypeScript SDK:
229+
230+
```typescript
231+
import Cloudflare from "cloudflare";
232+
233+
const client = new Cloudflare({
234+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
235+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
236+
});
237+
238+
const json = await client.browserRendering.json.create({
239+
account_id: "account_id",
240+
});
241+
242+
console.log(json);
243+
```
244+
245+
</TabItem> </Tabs>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ sidebar:
55
order: 10
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
The `/links` endpoint retrieves all links from a webpage. It can be used to extract all links from a page, including those that are hidden.
911

1012
## Basic usage
1113

14+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
15+
1216
This example grabs all links from the Cloudflare Developers homepage.
1317
The response will be a JSON array containing the links found on the page.
1418

@@ -100,6 +104,24 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
100104
}
101105
```
102106

107+
</TabItem> <TabItem label="TypeScript SDK">
108+
109+
```typescript
110+
import Cloudflare from "cloudflare";
111+
112+
const client = new Cloudflare({
113+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
114+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
115+
});
116+
117+
const links = await client.browserRendering.links.create({
118+
account_id: "account_id",
119+
});
120+
121+
console.log(links);
122+
```
123+
124+
</TabItem> </Tabs>
103125
## Advanced usage
104126

105127
In this example we can pass a `visibleLinksOnly` parameter to only return links that are visible on the page.

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ sidebar:
55
order: 10
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
The `/markdown` endpoint retrieves a webpage's content and converts it into Markdown format. You can specify a URL and optional parameters to refine the extraction process.
911

1012
## Basic usage
1113

1214
### Using a URL
1315

16+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
17+
1418
This example fetches the Markdown representation of a webpage.
1519

1620
```bash
@@ -22,15 +26,32 @@ curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browse
2226
}'
2327
```
2428

25-
### JSON response
29+
```json output
2630

27-
```json title="json response"
28-
{
2931
"success": true,
3032
"result": "# Example Domain\n\nThis domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.\n\n[More information...](https://www.iana.org/domains/example)"
3133
}
3234
```
3335

36+
</TabItem> <TabItem label="TypeScript SDK">
37+
38+
```typescript
39+
import Cloudflare from "cloudflare";
40+
41+
const client = new Cloudflare({
42+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
43+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
44+
});
45+
46+
const markdown = await client.browserRendering.markdown.create({
47+
account_id: "account_id",
48+
});
49+
50+
console.log(markdown);
51+
```
52+
53+
</TabItem> </Tabs>
54+
3455
### Use raw HTML
3556

3657
Instead of fetching the content by specifying the URL, you can provide raw HTML content directly.
@@ -44,9 +65,7 @@ curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browse
4465
}'
4566
```
4667

47-
### JSON response
48-
49-
```json title="json response"
68+
```json output
5069
{
5170
"success": true,
5271
"result": "Hello World"
@@ -67,9 +86,7 @@ curl -X 'POST' 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browse
6786
}'
6887
```
6988

70-
### JSON response
71-
72-
```json title="json response"
89+
```json output
7390
{
7491
"success": true,
7592
"result": "# Example Domain\n\nThis domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.\n\n[More information...](https://www.iana.org/domains/example)"

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ sidebar:
55
order: 5
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
The `/pdf` endpoint instructs the browser to render the webpage as a PDF document.
911

1012
## Basic usage
1113

14+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
15+
1216
Navigate to `https://example.com/` and inject custom CSS and an external stylesheet. Then return the rendered page as a PDF.
1317

1418
```bash
@@ -25,6 +29,28 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
2529
--output "output.pdf"
2630
```
2731

32+
</TabItem> <TabItem label="TypeScript SDK">
33+
34+
```typescript
35+
import Cloudflare from "cloudflare";
36+
37+
const client = new Cloudflare({
38+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
39+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
40+
});
41+
42+
const pdf = await client.browserRendering.pdf.create({
43+
account_id: "account_id",
44+
});
45+
46+
console.log(pdf);
47+
48+
const content = await pdf.blob();
49+
console.log(content);
50+
```
51+
52+
</TabItem> </Tabs>
53+
2854
## Advanced usage
2955

3056
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.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ sidebar:
55
order: 7
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
The `/scrape` endpoint extracts structured data from specific elements on a webpage, returning details such as element dimensions and inner HTML.
911

1012
## Basic usage
1113

14+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
15+
1216
Go to `https://example.com` and extract metadata from all `h1` and `a` elements in the DOM.
1317

1418
```bash
@@ -64,6 +68,26 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
6468
}
6569
```
6670

71+
</TabItem> <TabItem label="TypeScript SDK">
72+
73+
```typescript
74+
import Cloudflare from "cloudflare";
75+
76+
const client = new Cloudflare({
77+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
78+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
79+
});
80+
81+
const scrapes = await client.browserRendering.scrape.create({
82+
account_id: "account_id",
83+
elements: [{ selector: "selector" }],
84+
});
85+
86+
console.log(scrapes);
87+
```
88+
89+
</TabItem> </Tabs>
90+
6791
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.
6892

6993
### Response fields

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ sidebar:
55
order: 3
66
---
77

8+
import { Tabs, TabItem } from "~/components";
9+
810
The `/screenshot` endpoint renders the webpage by processing its HTML and JavaScript, then captures a screenshot of the fully rendered page.
911

1012
## Basic usage
1113

14+
<Tabs syncKey="workersExamples"> <TabItem label="curl">
15+
1216
Sets the HTML content of the page to `Hello World!` and then takes a screenshot. The option `omitBackground` hides the default white background and allows capturing screenshots with transparency.
1317

1418
```bash
@@ -24,6 +28,25 @@ curl -X POST 'https://api.cloudflare.com/client/v4/accounts/<accountId>/browser-
2428
--output "screenshot.png"
2529
```
2630

31+
</TabItem> <TabItem label="TypeScript SDK">
32+
33+
```typescript
34+
import Cloudflare from "cloudflare";
35+
36+
const client = new Cloudflare({
37+
apiEmail: process.env["CLOUDFLARE_EMAIL"], // This is the default and can be omitted
38+
apiKey: process.env["CLOUDFLARE_API_KEY"], // This is the default and can be omitted
39+
});
40+
41+
const screenshot = await client.browserRendering.screenshot.create({
42+
account_id: "account_id",
43+
});
44+
45+
console.log(screenshot.status);
46+
```
47+
48+
</TabItem> </Tabs>
49+
2750
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/).
2851

2952
## Advanced usage

0 commit comments

Comments
 (0)