Skip to content

Commit ac70e57

Browse files
author
stlc-bot
committed
feat(scrape): support custom screenshot headers (#1169)
Stainless-Generated-From: 6bcee363efb01db65999bc98644082358e2f590b
1 parent a270ff4 commit ac70e57

8 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎src/ServiceContracts/WebContract.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public function extractStyleguide(
210210
* @param string $domain Domain name to take screenshot of (e.g., 'example.com', 'google.com'). The domain will be automatically normalized and validated. You must provide either 'domain' or 'directUrl', but not both.
211211
* @param FullScreenshot|value-of<FullScreenshot> $fullScreenshot Optional parameter to determine screenshot type. If 'true', takes a full page screenshot capturing all content. If 'false' or not provided, takes a viewport screenshot (standard browser view).
212212
* @param bool $handleCookiePopup Optional parameter to control cookie/consent popup handling. If 'true', we dismiss cookie banner before capture. If 'false' or not provided, captures the page without that step.
213+
* @param array<string,string> $headers Optional outbound HTTP headers, using the same JSON object or deep-object query format as other scrape endpoints (for example headers[Authorization]=Bearer token). Headers are scoped to the target origin during capture. For domain/page requests, discovery receives no custom headers and only pages on the resolved origin are eligible. Non-empty headers bypass screenshot caching and return an in-memory data URL; no screenshot is uploaded. Empty objects behave like omitted headers.
213214
* @param int|null $maxAgeMs Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.
214215
* @param Page|value-of<Page> $page Optional parameter to specify which page type to screenshot. If provided, the system will scrape the domain's links and use heuristics to find the most appropriate URL for the specified page type (30 supported languages). If not provided, screenshots the main domain landing page. Only applicable when using 'domain', not 'directUrl'.
215216
* @param int|null $scrollOffset Optional vertical scroll offset in pixels for capturing a long page in viewport-sized chunks. When provided, the full page is captured once and the returned image is the viewport-sized slice that begins at this Y offset (e.g. request scrollOffset=0, then 1080, then 2160 to walk a 1920x1080 landing page top to bottom). The final slice may be shorter than the viewport height. Takes precedence over fullScreenshot. Max: 100000.
@@ -230,6 +231,7 @@ public function screenshot(
230231
?string $domain = null,
231232
FullScreenshot|string|null $fullScreenshot = null,
232233
bool $handleCookiePopup = false,
234+
?array $headers = null,
233235
?int $maxAgeMs = 86400000,
234236
Page|string|null $page = null,
235237
?int $scrollOffset = null,
@@ -489,6 +491,7 @@ public function webScrapeMd(
489491
* @param \ContextDev\Web\WebWebScrapeScreenshotParams\Country|value-of<\ContextDev\Web\WebWebScrapeScreenshotParams\Country> $country fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2)
490492
* @param \ContextDev\Web\WebWebScrapeScreenshotParams\FullScreenshot|value-of<\ContextDev\Web\WebWebScrapeScreenshotParams\FullScreenshot> $fullScreenshot Optional parameter to determine screenshot type. If 'true', takes a full page screenshot capturing all content. If 'false' or not provided, takes a viewport screenshot (standard browser view).
491493
* @param bool $handleCookiePopup Optional parameter to control cookie/consent popup handling. If 'true', we dismiss cookie banner before capture. If 'false' or not provided, captures the page without that step.
494+
* @param array<string,string> $headers Optional outbound HTTP headers, using the same JSON object or deep-object query format as other scrape endpoints (for example headers[Authorization]=Bearer token). Headers are scoped to the target origin during capture. For domain/page requests, discovery receives no custom headers and only pages on the resolved origin are eligible. Non-empty headers bypass screenshot caching and return an in-memory data URL; no screenshot is uploaded. Empty objects behave like omitted headers.
492495
* @param int|null $maxAgeMs Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.
493496
* @param int|null $scrollOffset Optional vertical scroll offset in pixels for capturing a long page in viewport-sized chunks. When provided, the full page is captured once and the returned image is the viewport-sized slice that begins at this Y offset (e.g. request scrollOffset=0, then 1080, then 2160 to walk a 1920x1080 landing page top to bottom). The final slice may be shorter than the viewport height. Takes precedence over fullScreenshot. Max: 100000.
494497
* @param list<string> $tags Comma-separated tags for tracking request usage. Up to 20 tags, each 1-50 characters.
@@ -507,6 +510,7 @@ public function webScrapeScreenshot(
507510
\ContextDev\Web\WebWebScrapeScreenshotParams\Country|string|null $country = null,
508511
\ContextDev\Web\WebWebScrapeScreenshotParams\FullScreenshot|string|null $fullScreenshot = null,
509512
bool $handleCookiePopup = false,
513+
?array $headers = null,
510514
?int $maxAgeMs = 86400000,
511515
?int $scrollOffset = null,
512516
?array $tags = null,

‎src/Services/WebRawService.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public function extractStyleguide(
302302
* domain?: string,
303303
* fullScreenshot?: FullScreenshot|value-of<FullScreenshot>,
304304
* handleCookiePopup?: bool,
305+
* headers?: array<string,string>,
305306
* maxAgeMs?: int|null,
306307
* page?: Page|value-of<Page>,
307308
* scrollOffset?: int|null,
@@ -642,6 +643,7 @@ public function webScrapeMd(
642643
* country?: value-of<WebWebScrapeScreenshotParams\Country>,
643644
* fullScreenshot?: WebWebScrapeScreenshotParams\FullScreenshot|value-of<WebWebScrapeScreenshotParams\FullScreenshot>,
644645
* handleCookiePopup?: bool,
646+
* headers?: array<string,string>,
645647
* maxAgeMs?: int|null,
646648
* scrollOffset?: int|null,
647649
* tags?: list<string>,

‎src/Services/WebService.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public function extractStyleguide(
328328
* @param string $domain Domain name to take screenshot of (e.g., 'example.com', 'google.com'). The domain will be automatically normalized and validated. You must provide either 'domain' or 'directUrl', but not both.
329329
* @param FullScreenshot|value-of<FullScreenshot> $fullScreenshot Optional parameter to determine screenshot type. If 'true', takes a full page screenshot capturing all content. If 'false' or not provided, takes a viewport screenshot (standard browser view).
330330
* @param bool $handleCookiePopup Optional parameter to control cookie/consent popup handling. If 'true', we dismiss cookie banner before capture. If 'false' or not provided, captures the page without that step.
331+
* @param array<string,string> $headers Optional outbound HTTP headers, using the same JSON object or deep-object query format as other scrape endpoints (for example headers[Authorization]=Bearer token). Headers are scoped to the target origin during capture. For domain/page requests, discovery receives no custom headers and only pages on the resolved origin are eligible. Non-empty headers bypass screenshot caching and return an in-memory data URL; no screenshot is uploaded. Empty objects behave like omitted headers.
331332
* @param int|null $maxAgeMs Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.
332333
* @param Page|value-of<Page> $page Optional parameter to specify which page type to screenshot. If provided, the system will scrape the domain's links and use heuristics to find the most appropriate URL for the specified page type (30 supported languages). If not provided, screenshots the main domain landing page. Only applicable when using 'domain', not 'directUrl'.
333334
* @param int|null $scrollOffset Optional vertical scroll offset in pixels for capturing a long page in viewport-sized chunks. When provided, the full page is captured once and the returned image is the viewport-sized slice that begins at this Y offset (e.g. request scrollOffset=0, then 1080, then 2160 to walk a 1920x1080 landing page top to bottom). The final slice may be shorter than the viewport height. Takes precedence over fullScreenshot. Max: 100000.
@@ -348,6 +349,7 @@ public function screenshot(
348349
?string $domain = null,
349350
FullScreenshot|string|null $fullScreenshot = null,
350351
bool $handleCookiePopup = false,
352+
?array $headers = null,
351353
?int $maxAgeMs = 86400000,
352354
Page|string|null $page = null,
353355
?int $scrollOffset = null,
@@ -367,6 +369,7 @@ public function screenshot(
367369
'domain' => $domain,
368370
'fullScreenshot' => $fullScreenshot,
369371
'handleCookiePopup' => $handleCookiePopup,
372+
'headers' => $headers,
370373
'maxAgeMs' => $maxAgeMs,
371374
'page' => $page,
372375
'scrollOffset' => $scrollOffset,
@@ -808,6 +811,7 @@ public function webScrapeMd(
808811
* @param \ContextDev\Web\WebWebScrapeScreenshotParams\Country|value-of<\ContextDev\Web\WebWebScrapeScreenshotParams\Country> $country fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2)
809812
* @param \ContextDev\Web\WebWebScrapeScreenshotParams\FullScreenshot|value-of<\ContextDev\Web\WebWebScrapeScreenshotParams\FullScreenshot> $fullScreenshot Optional parameter to determine screenshot type. If 'true', takes a full page screenshot capturing all content. If 'false' or not provided, takes a viewport screenshot (standard browser view).
810813
* @param bool $handleCookiePopup Optional parameter to control cookie/consent popup handling. If 'true', we dismiss cookie banner before capture. If 'false' or not provided, captures the page without that step.
814+
* @param array<string,string> $headers Optional outbound HTTP headers, using the same JSON object or deep-object query format as other scrape endpoints (for example headers[Authorization]=Bearer token). Headers are scoped to the target origin during capture. For domain/page requests, discovery receives no custom headers and only pages on the resolved origin are eligible. Non-empty headers bypass screenshot caching and return an in-memory data URL; no screenshot is uploaded. Empty objects behave like omitted headers.
811815
* @param int|null $maxAgeMs Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.
812816
* @param int|null $scrollOffset Optional vertical scroll offset in pixels for capturing a long page in viewport-sized chunks. When provided, the full page is captured once and the returned image is the viewport-sized slice that begins at this Y offset (e.g. request scrollOffset=0, then 1080, then 2160 to walk a 1920x1080 landing page top to bottom). The final slice may be shorter than the viewport height. Takes precedence over fullScreenshot. Max: 100000.
813817
* @param list<string> $tags Comma-separated tags for tracking request usage. Up to 20 tags, each 1-50 characters.
@@ -826,6 +830,7 @@ public function webScrapeScreenshot(
826830
\ContextDev\Web\WebWebScrapeScreenshotParams\Country|string|null $country = null,
827831
\ContextDev\Web\WebWebScrapeScreenshotParams\FullScreenshot|string|null $fullScreenshot = null,
828832
bool $handleCookiePopup = false,
833+
?array $headers = null,
829834
?int $maxAgeMs = 86400000,
830835
?int $scrollOffset = null,
831836
?array $tags = null,
@@ -845,6 +850,7 @@ public function webScrapeScreenshot(
845850
'country' => $country,
846851
'fullScreenshot' => $fullScreenshot,
847852
'handleCookiePopup' => $handleCookiePopup,
853+
'headers' => $headers,
848854
'maxAgeMs' => $maxAgeMs,
849855
'scrollOffset' => $scrollOffset,
850856
'tags' => $tags,

‎src/Web/WebScreenshotParams.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* domain?: string|null,
3333
* fullScreenshot?: null|FullScreenshot|value-of<FullScreenshot>,
3434
* handleCookiePopup?: bool|null,
35+
* headers?: array<string,string>|null,
3536
* maxAgeMs?: int|null,
3637
* page?: null|Page|value-of<Page>,
3738
* scrollOffset?: int|null,
@@ -96,6 +97,14 @@ final class WebScreenshotParams implements BaseModel
9697
#[Optional]
9798
public ?bool $handleCookiePopup;
9899

100+
/**
101+
* Optional outbound HTTP headers, using the same JSON object or deep-object query format as other scrape endpoints (for example headers[Authorization]=Bearer token). Headers are scoped to the target origin during capture. For domain/page requests, discovery receives no custom headers and only pages on the resolved origin are eligible. Non-empty headers bypass screenshot caching and return an in-memory data URL; no screenshot is uploaded. Empty objects behave like omitted headers.
102+
*
103+
* @var array<string,string>|null $headers
104+
*/
105+
#[Optional(map: 'string')]
106+
public ?array $headers;
107+
99108
/**
100109
* Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.
101110
*/
@@ -163,6 +172,7 @@ public function __construct()
163172
* @param ColorScheme|value-of<ColorScheme>|null $colorScheme
164173
* @param Country|value-of<Country>|null $country
165174
* @param FullScreenshot|value-of<FullScreenshot>|null $fullScreenshot
175+
* @param array<string,string>|null $headers
166176
* @param Page|value-of<Page>|null $page
167177
* @param list<string>|null $tags
168178
* @param TimeoutOpts|TimeoutOptsShape|null $timeoutOpts
@@ -177,6 +187,7 @@ public static function with(
177187
?string $domain = null,
178188
FullScreenshot|string|null $fullScreenshot = null,
179189
?bool $handleCookiePopup = null,
190+
?array $headers = null,
180191
?int $maxAgeMs = null,
181192
Page|string|null $page = null,
182193
?int $scrollOffset = null,
@@ -195,6 +206,7 @@ public static function with(
195206
null !== $domain && $self['domain'] = $domain;
196207
null !== $fullScreenshot && $self['fullScreenshot'] = $fullScreenshot;
197208
null !== $handleCookiePopup && $self['handleCookiePopup'] = $handleCookiePopup;
209+
null !== $headers && $self['headers'] = $headers;
198210
null !== $maxAgeMs && $self['maxAgeMs'] = $maxAgeMs;
199211
null !== $page && $self['page'] = $page;
200212
null !== $scrollOffset && $self['scrollOffset'] = $scrollOffset;
@@ -291,6 +303,19 @@ public function withHandleCookiePopup(bool $handleCookiePopup): self
291303
return $self;
292304
}
293305

306+
/**
307+
* Optional outbound HTTP headers, using the same JSON object or deep-object query format as other scrape endpoints (for example headers[Authorization]=Bearer token). Headers are scoped to the target origin during capture. For domain/page requests, discovery receives no custom headers and only pages on the resolved origin are eligible. Non-empty headers bypass screenshot caching and return an in-memory data URL; no screenshot is uploaded. Empty objects behave like omitted headers.
308+
*
309+
* @param array<string,string> $headers
310+
*/
311+
public function withHeaders(array $headers): self
312+
{
313+
$self = clone $this;
314+
$self['headers'] = $headers;
315+
316+
return $self;
317+
}
318+
294319
/**
295320
* Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.
296321
*/

‎src/Web/WebScreenshotResponse.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ final class WebScreenshotResponse implements BaseModel
8181
public ?KeyMetadata $keyMetadata;
8282

8383
/**
84-
* Public image URL for standard requests, or an in-memory data URL when ZDR is enabled.
84+
* Public image URL for standard requests, or an in-memory data URL when ZDR or non-empty custom headers are supplied.
8585
*/
8686
#[Optional]
8787
public ?string $screenshot;
@@ -250,7 +250,7 @@ public function withKeyMetadata(KeyMetadata|array $keyMetadata): self
250250
}
251251

252252
/**
253-
* Public image URL for standard requests, or an in-memory data URL when ZDR is enabled.
253+
* Public image URL for standard requests, or an in-memory data URL when ZDR or non-empty custom headers are supplied.
254254
*/
255255
public function withScreenshot(string $screenshot): self
256256
{

0 commit comments

Comments
 (0)