Puppeteer
Using SurfSky with Puppeteer
Prerequisites
To use SurfSky, you'll need the following:
- API Key - A unique authentication token for accessing our services
- Assigned Hostname - Your dedicated Surfsky endpoint
- Proxies - Proxies in the format:Supported protocols: HTTP, HTTPS, SOCKS5, SSH
protocol://username:password@host:port
To obtain your API key and hostname, please contact our team.
If you need proxies, please contact our team.
Code Example
- Python
- JavaScript
First, install the required packages:
pip install pyppeteer requests
Here's a basic example of how to use SurfSky with Pyppeteer:
import asyncio
from pyppeteer import connect
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()
async 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
browser = await connect(browserWSEndpoint=ws_url)
return browser
async def main():
API_TOKEN = "YOUR_API_TOKEN"
try:
# Start the browser
browser = await start_browser(API_TOKEN)
# Create a new page
page = await browser.newPage()
# Navigate to a website
await page.goto('https://example.com', {'waitUntil': 'networkidle0'})
# Perform your automation tasks here
title = await page.title()
print(title)
# Clean up
await browser.close()
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
asyncio.run(main())
First, install the required packages:
npm install puppeteer-core axios
Here's a basic example of how to use SurfSky with Puppeteer:
const puppeteer = require('puppeteer-core');
const axios = require('axios');
async function createProfile(apiToken, proxy = null) {
const url = 'https://api-public.surfsky.io/profiles/one_time';
const headers = {
'Content-Type': 'application/json',
'X-Cloud-Api-Token': apiToken
};
const data = {}
if (proxy) {
data.proxy = proxy;
}
const response = await axios.post(url, data, { headers });
return response.data;
}
async function startBrowser(apiToken, proxy = null) {
// Create a profile and get WebSocket URL
const profileData = await createProfile(apiToken, proxy);
const wsUrl = profileData.ws_url;
// Connect to the browser
const browser = await puppeteer.connect({
browserWSEndpoint: wsUrl
});
return browser;
}
async function main() {
const API_TOKEN = 'YOUR_API_TOKEN';
try {
// Start the browser
const browser = await startBrowser(API_TOKEN);
// Create a new page
const page = await browser.newPage();
// Navigate to a website
await page.goto('https://example.com', {
waitUntil: 'networkidle0'
});
// Perform your automation tasks here
const title = await page.title();
console.log(title);
// Clean up
await browser.close();
} catch (error) {
console.error('Error:', error);
}
}
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.