Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs(api): clarify that page.request shares cookies with BrowserContext (#40233)

Yury Semikhatskyf7d2b55ca9ed
diff --git a/docs/src/api/class-apirequestcontext.md b/docs/src/api/class-apirequestcontext.md
index 3525fe3ee7..2ddc3815f6 100644
--- a/docs/src/api/class-apirequestcontext.md
+++ b/docs/src/api/class-apirequestcontext.md
@@ -4,21 +4,25 @@
This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare
environment or the service to your e2e test.
-Each Playwright browser context has associated with it [APIRequestContext] instance which shares cookie storage with
-the browser context and can be accessed via [`property: BrowserContext.request`] or [`property: Page.request`].
-It is also possible to create a new APIRequestContext instance manually by calling [`method: APIRequest.newContext`].
+Each Playwright browser context has an associated [APIRequestContext], accessible via
+[`property: BrowserContext.request`] or [`property: Page.request`] (these return the
+**same instance** — `page.request` is a shortcut for `page.context().request`).
+You can also create a standalone, isolated instance with [`method: APIRequest.newContext`].
**Cookie management**
-[APIRequestContext] returned by [`property: BrowserContext.request`] and [`property: Page.request`] shares cookie
-storage with the corresponding [BrowserContext]. Each API request will have `Cookie` header populated with the
-values from the browser context. If the API response contains `Set-Cookie` header it will automatically update
-[BrowserContext] cookies and requests made from the page will pick them up. This means that if you log in using
-this API, your e2e test will be logged in and vice versa.
+The [APIRequestContext] returned by [`property: BrowserContext.request`] and
+[`property: Page.request`] uses the same cookie jar as its [BrowserContext]:
-If you want API requests to not interfere with the browser cookies you should create a new [APIRequestContext] by
-calling [`method: APIRequest.newContext`]. Such `APIRequestContext` object will have its own isolated cookie
-storage.
+- Every outgoing API request **automatically includes** the context's cookies in the
+ `Cookie` header — you do not need to read cookies from the context and attach them manually.
+- Any `Set-Cookie` headers in API responses are written back to the [BrowserContext],
+ so subsequent page navigations and API calls pick them up.
+- Logging in via the API logs in the browser, and vice versa.
+
+If you want API requests that do **not** share cookies with the browser, create an
+isolated context via [`method: APIRequest.newContext`]. Such `APIRequestContext`
+object will have its own isolated cookie storage.
```python async
import os
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index a8cd0cf0dc..deb6d9c9c5 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -18220,27 +18220,24 @@ export interface APIRequest {
* This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services,
* prepare environment or the service to your e2e test.
*
- * Each Playwright browser context has associated with it
- * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) instance which shares cookie storage
- * with the browser context and can be accessed via
+ * Each Playwright browser context has an associated
+ * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext), accessible via
* [browserContext.request](https://playwright.dev/docs/api/class-browsercontext#browser-context-request) or
- * [page.request](https://playwright.dev/docs/api/class-page#page-request). It is also possible to create a new
- * APIRequestContext instance manually by calling
+ * [page.request](https://playwright.dev/docs/api/class-page#page-request) (these return the
+ *
+ * **same instance** — `page.request` is a shortcut for `page.context().request`). You can also create a standalone,
+ * isolated instance with
* [apiRequest.newContext([options])](https://playwright.dev/docs/api/class-apirequest#api-request-new-context).
*
* **Cookie management**
*
- * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) returned by
+ * The [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) returned by
* [browserContext.request](https://playwright.dev/docs/api/class-browsercontext#browser-context-request) and
- * [page.request](https://playwright.dev/docs/api/class-page#page-request) shares cookie storage with the
- * corresponding [BrowserContext](https://playwright.dev/docs/api/class-browsercontext). Each API request will have
- * `Cookie` header populated with the values from the browser context. If the API response contains `Set-Cookie`
- * header it will automatically update [BrowserContext](https://playwright.dev/docs/api/class-browsercontext) cookies
- * and requests made from the page will pick them up. This means that if you log in using this API, your e2e test will
- * be logged in and vice versa.
*
- * If you want API requests to not interfere with the browser cookies you should create a new
- * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) by calling
+ * [page.request](https://playwright.dev/docs/api/class-page#page-request) uses the same cookie jar as its
+ * [BrowserContext](https://playwright.dev/docs/api/class-browsercontext):
+ *
+ * If you want API requests that do **not** share cookies with the browser, create an isolated context via
* [apiRequest.newContext([options])](https://playwright.dev/docs/api/class-apirequest#api-request-new-context). Such
* `APIRequestContext` object will have its own isolated cookie storage.
*/
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index a8cd0cf0dc..deb6d9c9c5 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -18220,27 +18220,24 @@ export interface APIRequest {
* This API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services,
* prepare environment or the service to your e2e test.
*
- * Each Playwright browser context has associated with it
- * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) instance which shares cookie storage
- * with the browser context and can be accessed via
+ * Each Playwright browser context has an associated
+ * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext), accessible via
* [browserContext.request](https://playwright.dev/docs/api/class-browsercontext#browser-context-request) or
- * [page.request](https://playwright.dev/docs/api/class-page#page-request). It is also possible to create a new
- * APIRequestContext instance manually by calling
+ * [page.request](https://playwright.dev/docs/api/class-page#page-request) (these return the
+ *
+ * **same instance** — `page.request` is a shortcut for `page.context().request`). You can also create a standalone,
+ * isolated instance with
* [apiRequest.newContext([options])](https://playwright.dev/docs/api/class-apirequest#api-request-new-context).
*
* **Cookie management**
*
- * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) returned by
+ * The [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) returned by
* [browserContext.request](https://playwright.dev/docs/api/class-browsercontext#browser-context-request) and
- * [page.request](https://playwright.dev/docs/api/class-page#page-request) shares cookie storage with the
- * corresponding [BrowserContext](https://playwright.dev/docs/api/class-browsercontext). Each API request will have
- * `Cookie` header populated with the values from the browser context. If the API response contains `Set-Cookie`
- * header it will automatically update [BrowserContext](https://playwright.dev/docs/api/class-browsercontext) cookies
- * and requests made from the page will pick them up. This means that if you log in using this API, your e2e test will
- * be logged in and vice versa.
*
- * If you want API requests to not interfere with the browser cookies you should create a new
- * [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) by calling
+ * [page.request](https://playwright.dev/docs/api/class-page#page-request) uses the same cookie jar as its
+ * [BrowserContext](https://playwright.dev/docs/api/class-browsercontext):
+ *
+ * If you want API requests that do **not** share cookies with the browser, create an isolated context via
* [apiRequest.newContext([options])](https://playwright.dev/docs/api/class-apirequest#api-request-new-context). Such
* `APIRequestContext` object will have its own isolated cookie storage.
*/