package pxshot
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
Official OCaml SDK for the Pxshot screenshot API
Install
dune-project
Dependency
Authors
Maintainers
Sources
pxshot-0.1.2.tbz
sha256=3af9325059bfa07d0445b01528a538bf600d76187871e3ae93d409b7f0dd62ff
sha512=e7954e56968a9bf73171377b3dec641fd99a9fb8b0fc91e4f3971b3cf19f39937fe21e4163eb1d9646ff913ac751c947fdd319a3b00edc76317dadb51ad02369
Description
A type-safe OCaml client for capturing screenshots via the Pxshot API. Supports configurable viewport sizes, full-page captures, various image formats, and cloud storage options.
README
Pxshot OCaml SDK
Official OCaml SDK for the Pxshot screenshot API.
Features
- 🔒 Type-safe API with Result types for error handling
- ⚡ Async operations with Lwt
- 📸 Full screenshot API support (formats, viewport, full-page, selectors)
- ☁️ Cloud storage option (get URLs instead of raw bytes)
- 📊 Usage statistics
Requirements
- OCaml 4.14+
- opam
Installation
opam install pxshotOr add to your dune-project:
(depends
(pxshot (>= 0.1.0)))Quick Start
open Lwt.Syntax
let () =
let client = Pxshot.create "px_your_api_key" in
Lwt_main.run begin
(* Capture a screenshot *)
let* result = Pxshot.screenshot client
~url:"https://example.com"
~format:Pxshot.Png
~width:1280
~height:720
() in
match result with
| Ok (Pxshot.Bytes data) ->
let oc = open_out_bin "screenshot.png" in
output_bytes oc data;
close_out oc;
print_endline "Screenshot saved!";
Lwt.return_unit
| Ok (Pxshot.Stored info) ->
Printf.printf "URL: %s\n" info.url;
Lwt.return_unit
| Error e ->
Printf.eprintf "Error: %s\n" (Pxshot.error_to_string e);
Lwt.return_unit
endAPI Reference
Creating a Client
(* Default API endpoint *)
let client = Pxshot.create "px_your_api_key"
(* Custom endpoint *)
let client = Pxshot.create ~base_url:"https://custom.api.com" "px_your_api_key"Taking Screenshots
val screenshot :
t ->
url:string ->
?format:format -> (* Png | Jpeg | Webp *)
?quality:int -> (* 1-100, for JPEG/WebP *)
?width:int -> (* Viewport width in pixels *)
?height:int -> (* Viewport height in pixels *)
?full_page:bool -> (* Capture entire scrollable page *)
?wait_until:wait_until -> (* Load | Domcontentloaded | Networkidle *)
?wait_for_selector:string -> (* CSS selector to wait for *)
?wait_for_timeout:int -> (* Additional wait in milliseconds *)
?device_scale_factor:float -> (* For retina displays *)
?store:bool -> (* Return URL instead of bytes *)
?block_ads:bool -> (* Block ads and trackers *)
unit ->
(screenshot_response, error) result Lwt.tFull-Page Screenshot
let* result = Pxshot.screenshot client
~url:"https://example.com"
~full_page:true
~format:Pxshot.Webp
~quality:90
()Wait for Dynamic Content
let* result = Pxshot.screenshot client
~url:"https://spa-app.com"
~wait_until:Pxshot.Networkidle
~wait_for_selector:".content-loaded"
~wait_for_timeout:2000
()Store and Get URL
let* result = Pxshot.screenshot client
~url:"https://example.com"
~store:true
() in
match result with
| Ok (Pxshot.Stored info) ->
Printf.printf "URL: %s\n" info.url;
Printf.printf "Expires: %s\n" info.expires_at;
Printf.printf "Size: %dx%d (%d bytes)\n"
info.width info.height info.size_bytes;
Lwt.return_unit
| _ -> ...Getting Usage Statistics
let* result = Pxshot.get_usage client in
match result with
| Ok usage ->
Printf.printf "Screenshots: %d/%d\n"
usage.screenshots_taken usage.screenshots_limit;
Printf.printf "Storage: %d/%d bytes\n"
usage.bytes_used usage.bytes_limit;
Lwt.return_unit
| Error e ->
Printf.eprintf "Error: %s\n" (Pxshot.error_to_string e);
Lwt.return_unitError Handling
All API calls return (result, error) result Lwt.t. Error types:
type error =
| Unauthorized of string (* Invalid API key *)
| BadRequest of string (* Invalid parameters *)
| RateLimited of string (* Too many requests *)
| ServerError of string (* API server error *)
| NetworkError of string (* Connection issues *)
| ParseError of string (* Response parsing failed *)Use error_to_string for human-readable messages:
match result with
| Error e -> Printf.eprintf "Failed: %s\n" (Pxshot.error_to_string e)
| Ok _ -> ...Types
Format
type format = Png | Jpeg | WebpWait Until
type wait_until = Load | Domcontentloaded | NetworkidleScreenshot Response
type screenshot_response =
| Bytes of bytes (* Raw image data *)
| Stored of stored_screenshot (* URL and metadata *)
type stored_screenshot = {
url : string;
expires_at : string;
width : int;
height : int;
size_bytes : int;
}Usage
type usage = {
screenshots_taken : int;
screenshots_limit : int;
bytes_used : int;
bytes_limit : int;
period_start : string;
period_end : string;
}Examples
See the examples directory:
Run examples:
PXSHOT_API_KEY=px_your_key dune exec examples/basic.exeDevelopment
# Install dependencies
opam install . --deps-only --with-test
# Build
dune build
# Run tests
dune test
# Format code
dune fmtLicense
MIT - see LICENSE
Links
Dependencies (6)
Dev Dependencies (3)
-
odoc
with-doc -
alcotest-lwt
with-test & >= "1.6.0" -
alcotest
with-test & >= "1.6.0"
Used by
None
Conflicts
None
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page