getsnap 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -0
- package/dist/index.d.ts +159 -0
- package/dist/index.js +76 -0
- package/package.json +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# snapapi-sdk
|
|
2
|
+
|
|
3
|
+
Official Node.js/TypeScript SDK for [SnapAPI](https://getsnap.dev) — Screenshot & PDF API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install snapapi-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import SnapAPI from "snapapi-sdk";
|
|
15
|
+
|
|
16
|
+
const snap = new SnapAPI("sk_live_YOUR_KEY");
|
|
17
|
+
|
|
18
|
+
// Take a screenshot
|
|
19
|
+
const { url } = await snap.screenshot({
|
|
20
|
+
url: "https://github.com",
|
|
21
|
+
format: "png",
|
|
22
|
+
full_page: true,
|
|
23
|
+
remove_popups: true,
|
|
24
|
+
});
|
|
25
|
+
console.log(url); // CDN URL to your screenshot
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- Full TypeScript types for all options and responses
|
|
31
|
+
- Screenshot capture (URL or HTML)
|
|
32
|
+
- Binary response (raw image bytes)
|
|
33
|
+
- Batch capture (up to 100 URLs)
|
|
34
|
+
- Usage tracking
|
|
35
|
+
- Async status polling
|
|
36
|
+
- All API parameters supported: lazy_load, wait_for_selector, hide_selectors, remove_selectors, extract_text, extract_html, click_selector, scroll_to_selector, and more
|
|
37
|
+
|
|
38
|
+
## API
|
|
39
|
+
|
|
40
|
+
### `new SnapAPI(apiKey, options?)`
|
|
41
|
+
|
|
42
|
+
Create a client instance.
|
|
43
|
+
|
|
44
|
+
- `apiKey` — Your SnapAPI key (starts with `sk_live_` or `sk_test_`)
|
|
45
|
+
- `options.baseUrl` — Custom base URL (default: `https://api.getsnap.dev`)
|
|
46
|
+
|
|
47
|
+
### `snap.screenshot(options)`
|
|
48
|
+
|
|
49
|
+
Take a screenshot. Returns `{ url, cached, request_id, extracted_text?, extracted_html? }`.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const result = await snap.screenshot({
|
|
53
|
+
url: "https://example.com",
|
|
54
|
+
format: "png",
|
|
55
|
+
viewport_width: 1280,
|
|
56
|
+
viewport_height: 720,
|
|
57
|
+
full_page: true,
|
|
58
|
+
remove_popups: true,
|
|
59
|
+
block_ads: true,
|
|
60
|
+
lazy_load: true,
|
|
61
|
+
extract_text: true,
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### `snap.screenshotBinary(options)`
|
|
66
|
+
|
|
67
|
+
Get raw image/PDF bytes as an `ArrayBuffer`.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { writeFile } from "fs/promises";
|
|
71
|
+
|
|
72
|
+
const buffer = await snap.screenshotBinary({
|
|
73
|
+
url: "https://example.com",
|
|
74
|
+
format: "webp",
|
|
75
|
+
quality: 90,
|
|
76
|
+
});
|
|
77
|
+
await writeFile("screenshot.webp", Buffer.from(buffer));
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### `snap.batch(options)`
|
|
81
|
+
|
|
82
|
+
Capture multiple URLs in one request.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const batch = await snap.batch({
|
|
86
|
+
urls: ["https://github.com", "https://stripe.com", "https://vercel.com"],
|
|
87
|
+
format: "png",
|
|
88
|
+
remove_popups: true,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
console.log(`${batch.succeeded}/${batch.count} captured`);
|
|
92
|
+
batch.results.forEach(r => console.log(r.source_url, "->", r.url));
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### `snap.usage()`
|
|
96
|
+
|
|
97
|
+
Check current usage and quota.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
const { used, limit, plan } = await snap.usage();
|
|
101
|
+
console.log(`${used}/${limit} (${plan})`);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### `snap.status(requestId)`
|
|
105
|
+
|
|
106
|
+
Check status of an async (webhook) request.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const status = await snap.status("req_abc123");
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Error Handling
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { SnapAPI, SnapAPIError } from "snapapi-sdk";
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
await snap.screenshot({ url: "https://example.com", format: "png" });
|
|
119
|
+
} catch (err) {
|
|
120
|
+
if (err instanceof SnapAPIError) {
|
|
121
|
+
console.error(err.status, err.error, err.message);
|
|
122
|
+
// 402, "quota_exceeded", "Monthly limit reached..."
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
export interface ScreenshotOptions {
|
|
2
|
+
/** URL to capture (required unless html is provided) */
|
|
3
|
+
url?: string;
|
|
4
|
+
/** HTML content to render (required unless url is provided) */
|
|
5
|
+
html?: string;
|
|
6
|
+
/** Output format */
|
|
7
|
+
format?: "png" | "jpg" | "jpeg" | "webp" | "pdf";
|
|
8
|
+
/** Viewport width in pixels (320-3840) */
|
|
9
|
+
viewport_width?: number;
|
|
10
|
+
/** Viewport height in pixels (200-2160) */
|
|
11
|
+
viewport_height?: number;
|
|
12
|
+
/** Capture full scrollable page */
|
|
13
|
+
full_page?: boolean;
|
|
14
|
+
/** Device preset */
|
|
15
|
+
device?: "desktop" | "mobile" | "tablet";
|
|
16
|
+
/** Extra delay in ms after page load (0-10000) */
|
|
17
|
+
delay?: number;
|
|
18
|
+
/** Custom CSS to inject */
|
|
19
|
+
css?: string;
|
|
20
|
+
/** Custom JavaScript to execute */
|
|
21
|
+
js?: string;
|
|
22
|
+
/** Render page in dark mode */
|
|
23
|
+
dark_mode?: boolean;
|
|
24
|
+
/** Image quality for JPEG/WebP (1-100) */
|
|
25
|
+
quality?: number;
|
|
26
|
+
/** Remove cookie banners and popups */
|
|
27
|
+
remove_popups?: boolean;
|
|
28
|
+
/** Block ads */
|
|
29
|
+
block_ads?: boolean;
|
|
30
|
+
/** CSS selector to capture specific element */
|
|
31
|
+
selector?: string;
|
|
32
|
+
/** Device scale factor / retina (0.5-3) */
|
|
33
|
+
device_scale_factor?: number;
|
|
34
|
+
/** Custom HTTP headers */
|
|
35
|
+
headers?: Record<string, string>;
|
|
36
|
+
/** Cookies to set */
|
|
37
|
+
cookies?: Array<{
|
|
38
|
+
name: string;
|
|
39
|
+
value: string;
|
|
40
|
+
domain?: string;
|
|
41
|
+
path?: string;
|
|
42
|
+
httpOnly?: boolean;
|
|
43
|
+
secure?: boolean;
|
|
44
|
+
}>;
|
|
45
|
+
/** Browser locale (e.g. "de-DE") */
|
|
46
|
+
locale?: string;
|
|
47
|
+
/** Browser timezone (e.g. "Europe/Berlin") */
|
|
48
|
+
timezone?: string;
|
|
49
|
+
/** Custom user agent string */
|
|
50
|
+
user_agent?: string;
|
|
51
|
+
/** Wait for this CSS selector to appear before capture */
|
|
52
|
+
wait_for_selector?: string;
|
|
53
|
+
/** Wait for network to become idle */
|
|
54
|
+
wait_for_network_idle?: boolean;
|
|
55
|
+
/** Auto-scroll page to trigger lazy-loaded content */
|
|
56
|
+
lazy_load?: boolean;
|
|
57
|
+
/** CSS selectors to hide (display: none) */
|
|
58
|
+
hide_selectors?: string[];
|
|
59
|
+
/** CSS selectors to remove from DOM */
|
|
60
|
+
remove_selectors?: string[];
|
|
61
|
+
/** Transparent background for PNG */
|
|
62
|
+
omit_background?: boolean;
|
|
63
|
+
/** Extract page text and return in response */
|
|
64
|
+
extract_text?: boolean;
|
|
65
|
+
/** Extract full HTML and return in response */
|
|
66
|
+
extract_html?: boolean;
|
|
67
|
+
/** Click this CSS selector before capture */
|
|
68
|
+
click_selector?: string;
|
|
69
|
+
/** Scroll to this CSS selector before capture */
|
|
70
|
+
scroll_to_selector?: string;
|
|
71
|
+
/** Return binary image data instead of CDN URL */
|
|
72
|
+
response_type?: "binary" | "url";
|
|
73
|
+
/** URL to receive async webhook with results */
|
|
74
|
+
webhook_url?: string;
|
|
75
|
+
/** Upload to your own S3 bucket */
|
|
76
|
+
s3_upload?: {
|
|
77
|
+
access_key_id: string;
|
|
78
|
+
secret_access_key: string;
|
|
79
|
+
bucket: string;
|
|
80
|
+
region: string;
|
|
81
|
+
endpoint?: string;
|
|
82
|
+
path?: string;
|
|
83
|
+
acl?: string;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export interface ScreenshotResponse {
|
|
87
|
+
url: string;
|
|
88
|
+
cached: boolean;
|
|
89
|
+
request_id: string;
|
|
90
|
+
response_time_ms?: number;
|
|
91
|
+
s3_key?: string;
|
|
92
|
+
s3_bucket?: string;
|
|
93
|
+
extracted_text?: string;
|
|
94
|
+
extracted_html?: string;
|
|
95
|
+
}
|
|
96
|
+
export interface AsyncResponse {
|
|
97
|
+
request_id: string;
|
|
98
|
+
status: "pending";
|
|
99
|
+
message: string;
|
|
100
|
+
status_url: string;
|
|
101
|
+
}
|
|
102
|
+
export interface BatchOptions extends Omit<ScreenshotOptions, "url" | "html" | "selector" | "response_type"> {
|
|
103
|
+
urls: string[];
|
|
104
|
+
}
|
|
105
|
+
export interface BatchResponse {
|
|
106
|
+
batch_id: string;
|
|
107
|
+
count: number;
|
|
108
|
+
succeeded: number;
|
|
109
|
+
failed: number;
|
|
110
|
+
response_time_ms: number;
|
|
111
|
+
results: Array<ScreenshotResponse & {
|
|
112
|
+
source_url: string;
|
|
113
|
+
}>;
|
|
114
|
+
}
|
|
115
|
+
export interface ErrorResponse {
|
|
116
|
+
error: string;
|
|
117
|
+
message: string;
|
|
118
|
+
details?: Record<string, string[]>;
|
|
119
|
+
}
|
|
120
|
+
export declare class SnapAPIError extends Error {
|
|
121
|
+
status: number;
|
|
122
|
+
error: string;
|
|
123
|
+
details?: Record<string, string[]>;
|
|
124
|
+
constructor(status: number, body: ErrorResponse);
|
|
125
|
+
}
|
|
126
|
+
export declare class SnapAPI {
|
|
127
|
+
private apiKey;
|
|
128
|
+
private baseUrl;
|
|
129
|
+
constructor(apiKey: string, options?: {
|
|
130
|
+
baseUrl?: string;
|
|
131
|
+
});
|
|
132
|
+
/**
|
|
133
|
+
* Take a screenshot of a URL or HTML content.
|
|
134
|
+
*/
|
|
135
|
+
screenshot(options: ScreenshotOptions): Promise<ScreenshotResponse>;
|
|
136
|
+
/**
|
|
137
|
+
* Take a screenshot and return raw binary image data.
|
|
138
|
+
*/
|
|
139
|
+
screenshotBinary(options: Omit<ScreenshotOptions, "response_type">): Promise<ArrayBuffer>;
|
|
140
|
+
/**
|
|
141
|
+
* Capture screenshots of multiple URLs in one request.
|
|
142
|
+
*/
|
|
143
|
+
batch(options: BatchOptions): Promise<BatchResponse>;
|
|
144
|
+
/**
|
|
145
|
+
* Check current usage and quota.
|
|
146
|
+
*/
|
|
147
|
+
usage(): Promise<{
|
|
148
|
+
used: number;
|
|
149
|
+
limit: number;
|
|
150
|
+
plan: string;
|
|
151
|
+
period: string;
|
|
152
|
+
}>;
|
|
153
|
+
/**
|
|
154
|
+
* Check the status of an async webhook request.
|
|
155
|
+
*/
|
|
156
|
+
status(requestId: string): Promise<Record<string, unknown>>;
|
|
157
|
+
private request;
|
|
158
|
+
}
|
|
159
|
+
export default SnapAPI;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export class SnapAPIError extends Error {
|
|
2
|
+
constructor(status, body) {
|
|
3
|
+
super(body.message);
|
|
4
|
+
this.name = "SnapAPIError";
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.error = body.error;
|
|
7
|
+
this.details = body.details;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export class SnapAPI {
|
|
11
|
+
constructor(apiKey, options) {
|
|
12
|
+
this.apiKey = apiKey;
|
|
13
|
+
this.baseUrl = options?.baseUrl || "https://api.getsnap.dev";
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Take a screenshot of a URL or HTML content.
|
|
17
|
+
*/
|
|
18
|
+
async screenshot(options) {
|
|
19
|
+
return this.request("POST", "/v1/screenshot", options);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Take a screenshot and return raw binary image data.
|
|
23
|
+
*/
|
|
24
|
+
async screenshotBinary(options) {
|
|
25
|
+
const response = await fetch(`${this.baseUrl}/v1/screenshot`, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
"x-api-key": this.apiKey,
|
|
30
|
+
},
|
|
31
|
+
body: JSON.stringify({ ...options, response_type: "binary" }),
|
|
32
|
+
});
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
const body = await response.json();
|
|
35
|
+
throw new SnapAPIError(response.status, body);
|
|
36
|
+
}
|
|
37
|
+
return response.arrayBuffer();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Capture screenshots of multiple URLs in one request.
|
|
41
|
+
*/
|
|
42
|
+
async batch(options) {
|
|
43
|
+
return this.request("POST", "/v1/batch", options);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Check current usage and quota.
|
|
47
|
+
*/
|
|
48
|
+
async usage() {
|
|
49
|
+
return this.request("GET", "/v1/usage");
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check the status of an async webhook request.
|
|
53
|
+
*/
|
|
54
|
+
async status(requestId) {
|
|
55
|
+
return this.request("GET", `/v1/screenshot/${requestId}`);
|
|
56
|
+
}
|
|
57
|
+
async request(method, path, body) {
|
|
58
|
+
const options = {
|
|
59
|
+
method,
|
|
60
|
+
headers: {
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
"x-api-key": this.apiKey,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
if (body && method !== "GET") {
|
|
66
|
+
options.body = JSON.stringify(body);
|
|
67
|
+
}
|
|
68
|
+
const response = await fetch(`${this.baseUrl}${path}`, options);
|
|
69
|
+
const data = await response.json();
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
throw new SnapAPIError(response.status, data);
|
|
72
|
+
}
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export default SnapAPI;
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "getsnap",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official Node.js SDK for SnapAPI - Screenshot & PDF API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["screenshot", "api", "pdf", "webpage", "capture", "snapapi"],
|
|
14
|
+
"author": "SnapAPI",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"homepage": "https://getsnap.dev",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/rokas-kve/snapapi-sdk-node"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^5.0.0"
|
|
23
|
+
}
|
|
24
|
+
}
|