feat: add Node.js and Python SDKs
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 9m1s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 9m1s
- Node.js SDK: TypeScript, ESM+CJS, zero deps (uses native fetch) - Python SDK: zero deps (uses urllib), Python 3.8+ - Both fully documented with examples and type hints - Ready for npm/PyPI publishing
This commit is contained in:
parent
db1fa8d506
commit
66ecc471cf
8 changed files with 709 additions and 0 deletions
126
sdk/node/README.md
Normal file
126
sdk/node/README.md
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
# SnapAPI Node.js SDK
|
||||
|
||||
Official Node.js client for [SnapAPI](https://snapapi.eu) — the EU-hosted screenshot API.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install snapapi
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
```typescript
|
||||
import { SnapAPI } from 'snapapi';
|
||||
import fs from 'fs';
|
||||
|
||||
const snap = new SnapAPI('your-api-key');
|
||||
|
||||
// Simple screenshot
|
||||
const png = await snap.capture('https://example.com');
|
||||
fs.writeFileSync('screenshot.png', png);
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Screenshot
|
||||
|
||||
```typescript
|
||||
const screenshot = await snap.capture('https://example.com');
|
||||
```
|
||||
|
||||
### With Options
|
||||
|
||||
```typescript
|
||||
const screenshot = await snap.capture('https://example.com', {
|
||||
format: 'jpeg',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
quality: 90,
|
||||
});
|
||||
```
|
||||
|
||||
### Full-Page Capture
|
||||
|
||||
```typescript
|
||||
const screenshot = await snap.capture({
|
||||
url: 'https://example.com/blog',
|
||||
fullPage: true,
|
||||
format: 'png',
|
||||
deviceScale: 2, // Retina
|
||||
});
|
||||
```
|
||||
|
||||
### Mobile Viewport
|
||||
|
||||
```typescript
|
||||
const screenshot = await snap.capture({
|
||||
url: 'https://example.com',
|
||||
width: 375,
|
||||
height: 812,
|
||||
deviceScale: 2,
|
||||
});
|
||||
```
|
||||
|
||||
### Wait for Dynamic Content
|
||||
|
||||
```typescript
|
||||
const screenshot = await snap.capture({
|
||||
url: 'https://example.com/dashboard',
|
||||
waitForSelector: '#chart-loaded',
|
||||
waitUntil: 'networkidle2',
|
||||
});
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### `new SnapAPI(apiKey, config?)`
|
||||
|
||||
| Parameter | Type | Description |
|
||||
|-----------|------|-------------|
|
||||
| `apiKey` | `string` | Your SnapAPI API key (required) |
|
||||
| `config.baseUrl` | `string` | API base URL (default: `https://snapapi.eu`) |
|
||||
| `config.timeout` | `number` | Request timeout in ms (default: `30000`) |
|
||||
|
||||
### `snap.capture(url, options?)` / `snap.capture(options)`
|
||||
|
||||
Returns a `Promise<Buffer>` containing the screenshot image.
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `url` | `string` | — | URL to capture (required) |
|
||||
| `format` | `'png' \| 'jpeg' \| 'webp'` | `'png'` | Output format |
|
||||
| `width` | `number` | `1280` | Viewport width (320–3840) |
|
||||
| `height` | `number` | `800` | Viewport height (200–2160) |
|
||||
| `fullPage` | `boolean` | `false` | Capture full scrollable page |
|
||||
| `quality` | `number` | `80` | JPEG/WebP quality (1–100) |
|
||||
| `waitForSelector` | `string` | — | CSS selector to wait for |
|
||||
| `deviceScale` | `number` | `1` | Device pixel ratio (1–3) |
|
||||
| `delay` | `number` | `0` | Extra delay in ms (0–5000) |
|
||||
| `waitUntil` | `string` | `'domcontentloaded'` | Load event to wait for |
|
||||
|
||||
### `snap.health()`
|
||||
|
||||
Returns API health status.
|
||||
|
||||
### Error Handling
|
||||
|
||||
```typescript
|
||||
import { SnapAPI, SnapAPIError } from 'snapapi';
|
||||
|
||||
try {
|
||||
const screenshot = await snap.capture('https://example.com');
|
||||
} catch (err) {
|
||||
if (err instanceof SnapAPIError) {
|
||||
console.error(`API error ${err.status}: ${err.detail}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## EU-Hosted & GDPR Compliant
|
||||
|
||||
SnapAPI runs entirely on EU infrastructure (Germany). Your data never leaves the EU. [Learn more](https://snapapi.eu).
|
||||
|
||||
## License
|
||||
|
||||
MIT — [Cloonar Technologies GmbH](https://snapapi.eu)
|
||||
34
sdk/node/package.json
Normal file
34
sdk/node/package.json
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"name": "snapapi",
|
||||
"version": "1.0.0",
|
||||
"description": "Official Node.js SDK for SnapAPI — EU-hosted screenshot API",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"files": ["dist"],
|
||||
"scripts": {
|
||||
"build": "tsc && tsc --module commonjs --outDir dist/cjs && mv dist/cjs/index.js dist/index.cjs && rm -rf dist/cjs",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"keywords": ["screenshot", "api", "webpage", "capture", "puppeteer", "headless", "eu", "gdpr"],
|
||||
"author": "Cloonar Technologies GmbH",
|
||||
"license": "MIT",
|
||||
"homepage": "https://snapapi.eu",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.cloonar.com/openclawd/SnapAPI"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
184
sdk/node/src/index.ts
Normal file
184
sdk/node/src/index.ts
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
/**
|
||||
* SnapAPI Node.js SDK
|
||||
* Official client for https://snapapi.eu — EU-hosted screenshot API
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { SnapAPI } from 'snapapi';
|
||||
*
|
||||
* const snap = new SnapAPI('your-api-key');
|
||||
* const screenshot = await snap.capture('https://example.com');
|
||||
* fs.writeFileSync('screenshot.png', screenshot);
|
||||
* ```
|
||||
*/
|
||||
|
||||
export interface ScreenshotOptions {
|
||||
/** URL to capture (required) */
|
||||
url: string;
|
||||
/** Output format: png, jpeg, or webp (default: png) */
|
||||
format?: "png" | "jpeg" | "webp";
|
||||
/** Viewport width in pixels, 320-3840 (default: 1280) */
|
||||
width?: number;
|
||||
/** Viewport height in pixels, 200-2160 (default: 800) */
|
||||
height?: number;
|
||||
/** Capture full scrollable page (default: false) */
|
||||
fullPage?: boolean;
|
||||
/** JPEG/WebP quality 1-100 (default: 80, ignored for PNG) */
|
||||
quality?: number;
|
||||
/** CSS selector to wait for before capturing */
|
||||
waitForSelector?: string;
|
||||
/** Device scale factor 1-3 (default: 1, use 2 for Retina) */
|
||||
deviceScale?: number;
|
||||
/** Extra delay in ms after page load, 0-5000 (default: 0) */
|
||||
delay?: number;
|
||||
/** Page load event to wait for (default: domcontentloaded) */
|
||||
waitUntil?: "load" | "domcontentloaded" | "networkidle0" | "networkidle2";
|
||||
}
|
||||
|
||||
export interface SnapAPIConfig {
|
||||
/** API base URL (default: https://snapapi.eu) */
|
||||
baseUrl?: string;
|
||||
/** Request timeout in ms (default: 30000) */
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export class SnapAPIError extends Error {
|
||||
/** HTTP status code */
|
||||
status: number;
|
||||
/** Error message from the API */
|
||||
detail: string;
|
||||
|
||||
constructor(status: number, detail: string) {
|
||||
super(`SnapAPI error ${status}: ${detail}`);
|
||||
this.name = "SnapAPIError";
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
}
|
||||
}
|
||||
|
||||
export class SnapAPI {
|
||||
private apiKey: string;
|
||||
private baseUrl: string;
|
||||
private timeout: number;
|
||||
|
||||
/**
|
||||
* Create a new SnapAPI client.
|
||||
*
|
||||
* @param apiKey - Your SnapAPI API key
|
||||
* @param config - Optional configuration
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const snap = new SnapAPI('sk_live_...');
|
||||
* ```
|
||||
*/
|
||||
constructor(apiKey: string, config?: SnapAPIConfig) {
|
||||
if (!apiKey) throw new Error("SnapAPI: apiKey is required");
|
||||
this.apiKey = apiKey;
|
||||
this.baseUrl = (config?.baseUrl ?? "https://snapapi.eu").replace(/\/$/, "");
|
||||
this.timeout = config?.timeout ?? 30000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture a screenshot of a URL.
|
||||
*
|
||||
* Returns the screenshot as a Buffer (Node.js) or Uint8Array.
|
||||
*
|
||||
* @param urlOrOptions - URL string or full ScreenshotOptions object
|
||||
* @param options - Additional options when first arg is a URL string
|
||||
* @returns Screenshot image as Buffer
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Simple usage
|
||||
* const png = await snap.capture('https://example.com');
|
||||
*
|
||||
* // With options
|
||||
* const jpg = await snap.capture('https://example.com', {
|
||||
* format: 'jpeg',
|
||||
* width: 1920,
|
||||
* height: 1080,
|
||||
* quality: 90
|
||||
* });
|
||||
*
|
||||
* // Full-page capture
|
||||
* const full = await snap.capture({
|
||||
* url: 'https://example.com',
|
||||
* fullPage: true,
|
||||
* format: 'png',
|
||||
* deviceScale: 2
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @throws {SnapAPIError} When the API returns an error response
|
||||
*/
|
||||
async capture(
|
||||
urlOrOptions: string | ScreenshotOptions,
|
||||
options?: Omit<ScreenshotOptions, "url">
|
||||
): Promise<Buffer> {
|
||||
const body: ScreenshotOptions =
|
||||
typeof urlOrOptions === "string"
|
||||
? { url: urlOrOptions, ...options }
|
||||
: urlOrOptions;
|
||||
|
||||
if (!body.url) throw new Error("SnapAPI: url is required");
|
||||
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), this.timeout);
|
||||
|
||||
try {
|
||||
const res = await fetch(`${this.baseUrl}/v1/screenshot`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${this.apiKey}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
let detail = `HTTP ${res.status}`;
|
||||
try {
|
||||
const json = (await res.json()) as { error?: string };
|
||||
detail = json.error ?? detail;
|
||||
} catch {}
|
||||
throw new SnapAPIError(res.status, detail);
|
||||
}
|
||||
|
||||
const arrayBuffer = await res.arrayBuffer();
|
||||
return Buffer.from(arrayBuffer);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check API health status.
|
||||
*
|
||||
* @returns Health check response
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const health = await snap.health();
|
||||
* console.log(health.status); // "ok"
|
||||
* ```
|
||||
*/
|
||||
async health(): Promise<{
|
||||
status: string;
|
||||
version: string;
|
||||
uptime: number;
|
||||
browser: {
|
||||
browsers: number;
|
||||
totalPages: number;
|
||||
availablePages: number;
|
||||
queueDepth: number;
|
||||
};
|
||||
}> {
|
||||
const res = await fetch(`${this.baseUrl}/health`);
|
||||
if (!res.ok) throw new SnapAPIError(res.status, "Health check failed");
|
||||
return res.json() as any;
|
||||
}
|
||||
}
|
||||
|
||||
export default SnapAPI;
|
||||
14
sdk/node/tsconfig.json
Normal file
14
sdk/node/tsconfig.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"declaration": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue