🍎 Swift SDK — Quick Start

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+.

On This Page

Installation

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.

Authentication

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"] ?? "")
💡 Tip: For iOS apps, store your API key in a server-side proxy to avoid exposing it in the client bundle.

Take Screenshots

Basic Screenshot

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)

Full Page Screenshot

let imageData = try await client.screenshot(
    url: "https://example.com",
    format: .png,
    fullPage: true,
    width: 1280
)

Screenshot with Options

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
)

Get Hosted URL

let result = try await client.screenshot(
    url: "https://example.com",
    format: .png,
    responseType: .url
)

print(result.url)
// → https://cdn.snapapi.pics/screenshots/abc123.png

Generate PDFs

let 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"))

PDF from HTML String

let pdfData = try await client.pdf(
    html: "<h1>Invoice #1234</h1><p>Total: $99.00</p>",
    format: .a4,
    printBackground: true
)

Extract Content

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...

Extract Structured Data

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)")

Record Videos

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"))

Error Handling

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)")
}
⚠️ Rate Limits: Free tier: 200 requests/month. Starter: $19/mo. Pro: $79/mo. View plans.

SwiftUI Integration

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)
        }
    }
}

Next Steps