Add the SnapAPI Swift SDK via Swift Package Manager and capture screenshots, PDFs, and web content using modern async/await. Supports iOS 15+ and macOS 12+.
Add SnapAPI via Swift Package Manager. In your Package.swift:
dependencies: [
.package(url: "https://github.com/nickthedick1994/snapapi-swift", from: "1.0.0")
]Or in Xcode: File → Add Package Dependencies and paste the repository URL.
Get your API key from the SnapAPI dashboard and initialize the client:
import SnapAPI // Initialize with your API key let client = SnapAPI(apiKey: "YOUR_API_KEY") // Or load from environment / config let client = SnapAPI(apiKey: ProcessInfo.processInfo.environment["SNAPAPI_KEY"] ?? "")
import SnapAPI
let client = SnapAPI(apiKey: "YOUR_API_KEY")
let imageData = try await client.screenshot(
url: "https://example.com",
format: .png,
width: 1280,
height: 800
)
// Save to file (macOS)
try imageData.write(to: URL(fileURLWithPath: "screenshot.png"))
// Or use in UIImage (iOS)
let image = UIImage(data: imageData)let imageData = try await client.screenshot(
url: "https://example.com",
format: .png,
fullPage: true,
width: 1280
)let imageData = try await client.screenshot(
url: "https://example.com",
format: .webp, // .png, .jpeg, .webp, .avif
width: 1440,
height: 900,
fullPage: false,
blockAds: true, // Block advertisements
blockCookies: true, // Block cookie banners
delay: 2000, // Wait 2s before capture
devicePreset: "iPhone15", // Mobile device emulation
css: "body { background: white; }",
responseType: .url // Get hosted URL instead of binary
)let result = try await client.screenshot(
url: "https://example.com",
format: .png,
responseType: .url
)
print(result.url)
// → https://cdn.snapapi.pics/screenshots/abc123.pnglet pdfData = try await client.pdf(
url: "https://example.com",
format: .a4,
printBackground: true,
margin: PDFMargin(
top: "20mm",
bottom: "20mm",
left: "15mm",
right: "15mm"
)
)
try pdfData.write(to: URL(fileURLWithPath: "page.pdf"))let pdfData = try await client.pdf(
html: "<h1>Invoice #1234</h1><p>Total: $99.00</p>",
format: .a4,
printBackground: true
)Extract clean markdown or structured data from any webpage — ideal for LLM pipelines.
let content = try await client.extract(
url: "https://example.com/blog-post",
format: .markdown
)
print(content.markdown)
// → # Blog Post Title\n\nArticle content here...struct Product: Codable {
let title: String
let price: Double
let description: String
}
let product: Product = try await client.extract(
url: "https://example.com/product",
format: .structured
)
print("\(product.title) - $\(product.price)")let videoData = try await client.video(
url: "https://example.com",
duration: 5, // seconds
width: 1280,
height: 720,
format: .mp4
)
try videoData.write(to: URL(fileURLWithPath: "recording.mp4"))do {
let image = try await client.screenshot(url: "https://example.com")
try image.write(to: URL(fileURLWithPath: "screenshot.png"))
} catch SnapAPIError.authenticationFailed {
print("Invalid API key")
} catch SnapAPIError.rateLimitExceeded(let retryAfter) {
print("Rate limited. Retry after \(retryAfter)s")
} catch SnapAPIError.badRequest(let message) {
print("Bad request: \(message)")
} catch {
print("Unexpected error: \(error)")
}import SwiftUI
import SnapAPI
struct ScreenshotView: View {
@State private var image: UIImage?
@State private var isLoading = false
let client = SnapAPI(apiKey: "YOUR_API_KEY")
var body: some View {
VStack {
if let image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
}
Button("Capture Screenshot") {
Task {
isLoading = true
defer { isLoading = false }
let data = try await client.screenshot(
url: "https://example.com",
format: .png
)
image = UIImage(data: data)
}
}
.disabled(isLoading)
}
}
}