Overview
Cookies provide a flexible way to authenticate your sessions in Notte. While we recommend using the secure vault for credential management, cookies offer an alternative approach that might be more convenient for certain use cases.
Uploading Cookies to Your Session
Here’s how to upload cookies to your Notte session:
from notte_sdk import NotteClient
from notte_sdk.types import Cookie
# Upload cookies for github.com to automatically login
cookies = [
Cookie(
name="sb-db-auth-token",
value="base64-cookie-value",
domain="github.com",
path="/",
expires=9778363203.913704,
httpOnly=False,
secure=False,
sameSite="Lax"
)
]
# create a new session
notte = NotteClient()
with notte.Session() as session:
_ = session.set_cookies(cookies=cookies) # can also set cookie_file="path/to/cookies.json"
# Use the cookies in your session
agent = notte.Agent(session=session, max_steps=5)
res = agent.run(
task="go to nottelabs/notte get repo info. Fail if you are not logged in",
url="https://github.com/nottelabs/notte",
)
# or get the cookies from the session
cookies_resp = session.get_cookies()
Important Notes
- The cookies file must be a valid JSON file
- Cookies are available for all sessions started after upload
- You need to manage cookie expiration manually
- Upload new cookies when they expire
Here’s a step-by-step guide to extract cookies from your browser:
import json
from pathlib import Path
from patchright.sync_api import sync_playwright
cookie_path = Path("github_cookies.json")
# Initialize Playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
# Navigate to login page
github_login_url = "https://github.com/login"
page.goto(github_login_url)
print("Please log into GitHub in the browser window...")
input("Press Enter after you've logged in...")
# Save cookies to file
print("Login successful. Saving cookies...")
cookies = context.cookies(urls=["https://github.com"])
if cookies:
cookie_path.write_text(json.dumps(cookies, indent=4))
print(f"Cookies saved to {cookie_path}")
else:
print("No cookies found to save.")
Of course, you can also get them from your Notte session:
from notte_sdk import NotteClient
from notte_sdk.types import Cookie
notte = NotteClient()
with notte.Session() as session:
# Observe the state of a webpage
obs = session.observe(url="https://google.com/travel/flights")
# Get and save cookies from the session
cookies = session.get_cookies()
_ = Cookie.dump_json(cookies, "cookies.json")
Best Practices
-
Security
- Store cookie files securely
- Don’t commit cookie files to version control
- Regularly rotate cookies for sensitive services
-
Maintenance
- Monitor cookie expiration dates
- Set up reminders to refresh cookies
- Keep backup copies of valid cookies
-
Troubleshooting
- If a session fails, try uploading fresh cookies
- Check if cookies are still valid
- Verify the cookie file format is correct