Official Python SDK for the Grasp browser automation platform
Project description
Grasp Python SDK
Official Python SDK for the Grasp browser automation platform.
Features
- Dual Client Architecture: Both synchronous and asynchronous clients
- Type Safety: Full type hints and Pydantic models for validation
- Browser Automation: Seamless integration with Playwright and Puppeteer
- Proxy Support: Built-in proxy configuration for different use cases
- Error Handling: Comprehensive exception hierarchy
- Auto-retry: Automatic retry with exponential backoff
Installation
pip install grasp-sdk
Quick Start
Synchronous Usage
from grasp import Grasp
# Initialize client
client = Grasp(api_key="your-api-key") # or set GRASP_API_KEY env var
# Create a container
container = client.create(
idle_timeout=30000, # 30 seconds
proxy={
"enabled": True,
"type": "residential",
"country": "US"
}
)
# Access browser information
print(f"Container ID: {container.id}")
print(f"CDP WebSocket: {container.browser.ws_endpoint}")
print(f"Live View: {container.browser.live_url}")
# Shutdown when done
container.shutdown()
Asynchronous Usage
import asyncio
from grasp import AsyncGrasp
async def main():
# Use context manager for automatic cleanup
async with AsyncGrasp(api_key="your-api-key") as client:
# Create container
container = await client.create(idle_timeout=30000)
# Use the container
print(f"Container ID: {container.id}")
# Shutdown
await container.shutdown()
asyncio.run(main())
Playwright Integration
from grasp import Grasp
from playwright.sync_api import sync_playwright
# Create Grasp container
grasp = Grasp()
container = grasp.create()
# Connect Playwright
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(container.browser.ws_endpoint)
page = browser.new_page()
page.goto("https://example.com")
# Your automation code here
title = page.title()
print(f"Page title: {title}")
browser.close()
# Cleanup
container.shutdown()
Configuration
Environment Variables
GRASP_API_KEY: Your API key (alternative to passing in code)GRASP_BASE_URL: API base URL (defaults tohttps://api.getgrasp.ai)
Client Options
client = Grasp(
api_key="your-api-key",
base_url="https://api.getgrasp.ai", # optional
timeout=60.0, # request timeout in seconds
max_retries=2 # maximum retry attempts
)
Container Options
container = client.create(
idle_timeout=30000, # milliseconds before container sleeps
proxy={
"enabled": True,
"type": "residential", # mobile, residential, isp, datacenter, custom
"country": "US", # optional: country code
"state": "CA", # optional: state code
"city": "LA" # optional: city name
}
)
API Reference
Main Classes
Grasp / AsyncGrasp
Main client classes for interacting with the API.
Methods:
create(**options): Create a new containerconnect(container_id): Connect to existing container
GraspContainer / AsyncGraspContainer
Represents a browser automation container.
Properties:
id: Container identifierstatus: Current status (running, stopped, sleeping)created_at: Creation timestampbrowser: Browser session information
Methods:
shutdown(): Shut down the container
BrowserSession
Browser session information.
Properties:
ws_endpoint: Chrome DevTools Protocol WebSocket endpointlive_url: Live view URL for observing the browser
Exception Handling
from grasp import (
GraspError,
AuthenticationError,
NotFoundError,
RateLimitError
)
try:
container = client.create()
except AuthenticationError:
print("Invalid API key")
except RateLimitError as e:
print(f"Rate limited. Retry after: {e.retry_after}")
except GraspError as e:
print(f"API error: {e}")
Exception Hierarchy
GraspError: Base exceptionAPIError: API-related errorsAPIStatusError: HTTP status errorsAuthenticationError: 401 UnauthorizedNotFoundError: 404 Not FoundRateLimitError: 429 Rate LimitedInternalServerError: 500 Server Error
APIConnectionError: Network issuesAPITimeoutError: Request timeout
Examples
See the examples directory for more detailed examples:
- basic_usage.py: Simple synchronous usage
- async_usage.py: Async with multiple containers
- playwright_integration.py: Full Playwright integration
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/getgrasp-ai/grasp.git
cd grasp/packages/python-sdk
# Install dependencies
pip install -e ".[dev]"
Run Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=grasp
# Run specific test file
pytest tests/test_client.py
Code Quality
# Format code
black src/ tests/
# Lint
ruff check src/ tests/
# Type check
mypy src/
Requirements
- Python 3.8+
- httpx >= 0.24.0
- pydantic >= 2.0
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grasp_sdk-0.3.1.tar.gz.
File metadata
- Download URL: grasp_sdk-0.3.1.tar.gz
- Upload date:
- Size: 27.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df6ca8002d5a98b8c0875ce65f9f761c4f4198556da9498742fcbfe1024d48c6
|
|
| MD5 |
dca730a2de9f5b6760049b7c1e935978
|
|
| BLAKE2b-256 |
f81b566d4158eb683185545658c4dd24b50b925ecdc5461dba5ce68629d72ce2
|
File details
Details for the file grasp_sdk-0.3.1-py3-none-any.whl.
File metadata
- Download URL: grasp_sdk-0.3.1-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf0fc6267cde9539a0010d0758a8c341a0cbdf12e9957d81a11c206b74b345a9
|
|
| MD5 |
59d235727c45dff9a3d9635483a6a8b6
|
|
| BLAKE2b-256 |
04ca6734ac47c2020f3c57e6a37007ab31024325173e3848c27ca1c4de2447df
|