Rust SDK for capture.page - Browser automation and screenshot API.
Get your API Key and Secret from https://capture.page/console
List of all capture options: https://docs.capture.page
Add this to your Cargo.toml
:
[dependencies]
capture-rust = "0.1.0"
use capture_rust::{Capture, RequestOptions};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::new("your_api_key".to_string(), "your_api_secret".to_string());
let mut options = HashMap::new();
options.insert("full".to_string(), serde_json::Value::Bool(true));
options.insert("delay".to_string(), serde_json::Value::Number(serde_json::Number::from(3)));
// Build URL
let url = capture.build_image_url("https://capture.page/", Some(&options))?;
println!("Screenshot URL: {}", url);
// Fetch image data
let image_data = capture.fetch_image("https://capture.page/", Some(&options)).await?;
std::fs::write("screenshot.png", image_data)?;
Ok(())
}
use capture_rust::{Capture, CaptureOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = CaptureOptions { use_edge: true };
let capture = Capture::with_options("your_api_key".to_string(), "your_api_secret".to_string(), options);
let image_data = capture.fetch_image("https://capture.page/", None).await?;
std::fs::write("edge_screenshot.png", image_data)?;
Ok(())
}
use capture_rust::Capture;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::new("your_api_key".to_string(), "your_api_secret".to_string());
let mut options = HashMap::new();
options.insert("full".to_string(), serde_json::Value::Bool(true));
options.insert("delay".to_string(), serde_json::Value::Number(serde_json::Number::from(3)));
// Build URL
let url = capture.build_image_url("https://capture.page/", Some(&options))?;
// Fetch image
let image_data = capture.fetch_image("https://capture.page/", Some(&options)).await?;
std::fs::write("screenshot.png", image_data)?;
Ok(())
}
use capture_rust::Capture;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::new("your_api_key".to_string(), "your_api_secret".to_string());
let mut options = HashMap::new();
options.insert("full".to_string(), serde_json::Value::Bool(true));
options.insert("delay".to_string(), serde_json::Value::Number(serde_json::Number::from(3)));
// Build URL
let url = capture.build_pdf_url("https://capture.page/", Some(&options))?;
// Fetch PDF
let pdf_data = capture.fetch_pdf("https://capture.page/", Some(&options)).await?;
std::fs::write("page.pdf", pdf_data)?;
Ok(())
}
use capture_rust::Capture;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::new("your_api_key".to_string(), "your_api_secret".to_string());
// Build URL
let url = capture.build_content_url("https://capture.page/")?;
// Fetch content
let content = capture.fetch_content("https://capture.page/", None).await?;
println!("Success: {}", content.success);
println!("HTML: {}", content.html);
println!("Text: {}", content.text_content);
Ok(())
}
use capture_rust::Capture;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::new("your_api_key".to_string(), "your_api_secret".to_string());
// Build URL
let url = capture.build_metadata_url("https://capture.page/")?;
// Fetch metadata
let metadata = capture.fetch_metadata("https://capture.page/", None).await?;
println!("Success: {}", metadata.success);
println!("Metadata: {:?}", metadata.metadata);
Ok(())
}
Run the examples with your API credentials:
# Set your API credentials
export CAPTURE_API_KEY="your_api_key"
export CAPTURE_API_SECRET="your_api_secret"
# Run basic usage example
cargo run --example basic_usage
# Run edge endpoint example
cargo run --example edge_usage
The SDK uses the CaptureError
enum for error handling:
use capture_rust::{Capture, CaptureError};
#[tokio::main]
async fn main() {
let capture = Capture::new("key".to_string(), "secret".to_string());
match capture.fetch_image("https://example.com", None).await {
Ok(image_data) => {
println!("Got image data: {} bytes", image_data.len());
},
Err(CaptureError::HttpError(e)) => {
println!("HTTP error: {}", e);
},
Err(CaptureError::MissingCredentials) => {
println!("API key and secret are required");
},
Err(e) => {
println!("Other error: {}", e);
}
}
}
The main client for interacting with the capture.page API.
new(key: String, secret: String) -> Self
- Create a new client with API credentialswith_options(key: String, secret: String, options: CaptureOptions) -> Self
- Create a client with custom options
build_image_url(url: &str, options: Option<&RequestOptions>) -> Result<String>
- Build image capture URLbuild_pdf_url(url: &str, options: Option<&RequestOptions>) -> Result<String>
- Build PDF capture URLbuild_content_url(url: &str, options: Option<&RequestOptions>) -> Result<String>
- Build content extraction URLbuild_metadata_url(url: &str, options: Option<&RequestOptions>) -> Result<String>
- Build metadata extraction URL
fetch_image(url: &str, options: Option<&RequestOptions>) -> Result<Vec<u8>>
- Fetch image as bytesfetch_pdf(url: &str, options: Option<&RequestOptions>) -> Result<Vec<u8>>
- Fetch PDF as bytesfetch_content(url: &str, options: Option<&RequestOptions>) -> Result<ContentResponse>
- Fetch page contentfetch_metadata(url: &str, options: Option<&RequestOptions>) -> Result<MetadataResponse>
- Fetch page metadata
RequestOptions
- HashMap of capture optionsCaptureOptions
- SDK configuration optionsContentResponse
- Response from content extractionMetadataResponse
- Response from metadata extractionCaptureError
- Error types for the SDK
MIT