Skip to main content

Playwright

Using SurfSky with Playwright

Prerequisites

To use SurfSky, you'll need the following:

  1. API Key - A unique authentication token for accessing our services
  2. Assigned Hostname - Your dedicated Surfsky endpoint
  3. Proxies - Proxies in the format:
    protocol://username:password@host:port
    Supported protocols: HTTP, HTTPS, SOCKS5, SSH

To obtain your API key and hostname, please contact our team.

If you need proxies, please contact our team.

Code Example

First, install the required packages:

pip install playwright requests

Here's a basic example of how to use SurfSky with Playwright:

from playwright.sync_api import sync_playwright
import requests


def create_profile(api_token: str, proxy: str | None = None) -> dict:
"""Create a one-time browser profile"""
url = "https://api-public.surfsky.io/profiles/one_time"

headers = {
"Content-Type": "application/json",
"X-Cloud-Api-Token": api_token
}

data = {}
if proxy:
data["proxy"] = proxy

response = requests.post(url, headers=headers, json=data)
response.raise_for_status()

return response.json()


def start_browser(api_token: str, proxy: str | None = None):
"""Start a browser session with SurfSky"""
# Create a profile and get WebSocket URL
profile_data = create_profile(api_token, proxy)
ws_url = profile_data["ws_url"]

# Connect to the browser
playwright = sync_playwright().start()
browser = playwright.chromium.connect_over_cdp(ws_url)

return browser, playwright


def main():
API_TOKEN = "YOUR_API_TOKEN"

# Start the browser
browser, playwright = start_browser(API_TOKEN)

try:
# Get the first context and create a new page
context = browser.contexts[0]
page = context.new_page()

# Navigate to a website
page.goto('https://example.com', wait_until='networkidle')

# Perform your automation tasks here
print(page.title())

finally:
# Clean up
browser.close()
playwright.stop()


if __name__ == "__main__":
main()

Important Notes

  • Always remember to close the browser when you're done to release your session limit. Inactive sessions are automatically closed after 30 seconds. Set inactive_kill_timeout to change this value.
  • One time profile is used only once and then deleted. Use persistent profiles for long-term sessions
  • A proxy is required and must be passed to the create_profile function
  • You can run multiple sessions according to your subscription plan's session limit

For more advanced usage and error handling, check out our API Reference.