jssdk

An official Screenshot API client for JavaScript and TypeScript.
It takes minutes to start taking screenshots. Just sign up to get access and secret keys, import the client, and you are ready to go.
The SDK client is synchronized with the latest screenshot API options.
Installation
npm install screenshotone-api-sdk --save
Usage
Generate a screenshot URL without executing the request. Or download the screenshot. It is up to you:
import * as fs from 'fs';
import * as screenshotone from 'screenshotone-api-sdk';
const client = new screenshotone.Client("<access key>", "<secret key>");
const options = screenshotone.TakeOptions
.url("https://example.com")
.delay(3)
.blockAds(true);
const url = client.generateTakeURL(options);
console.log(url);
try {
const imageBlob = await client.take(options);
const buffer = Buffer.from(await imageBlob.arrayBuffer());
fs.writeFileSync("example.png", buffer);
} catch (error) {
if (error instanceof screenshotone.APIError) {
} else if (error instanceof screenshotone.ConnectionError) {
console.error(error.message);
} else {
console.error("An unexpected error occurred:", error.message);
}
}
Connection errors and retries
A request can fail on the network between your server and the API, for example with ECONNRESET when a proxy, NAT gateway or firewall closes the connection. The SDK then throws a ConnectionError that says:
- whether the connection broke before the API responded (
phase: "request") or while the result was downloading (phase: "response", with httpStatusCode)
- how many attempts were made (
attempts)
- how long after the last attempt started (
lastElapsedMs), and how long after the first one started, waits between retries included (totalElapsedMs)
- what usually causes it
To retry connection resets automatically, set maxRetries. It’s 0 by default, so the SDK doesn’t retry unless you set it:
const client = new screenshotone.Client("<access key>", "<secret key>", {
maxRetries: 3,
});
Retries apply only to ECONNRESET and wait with exponential backoff. A retry sends the request again. If the API had already finished the first one, the retry renders the screenshot again and counts toward your usage again, unless the result is served from the cache.
Many requests starting at once make connection resets more likely. If you take screenshots in large batches, limit how many requests run at the same time.
Build and publish (a manual for SDK developers)
To build and publish the SDK:
- Bump the version property in the
package.json file.
- Run
npm run prepare.
- Run
npm publish.
License
screenshotone/jssdk is released under the MIT license.
评论