Add the SnapAPI Kotlin SDK via Gradle and capture screenshots, PDFs, and web content using Kotlin coroutines. Supports Android and JVM.
Add the JitPack repository and dependency to your build.gradle.kts:
// settings.gradle.kts
dependencyResolutionManagement {
repositories {
maven("https://jitpack.io")
}
}
// build.gradle.kts
dependencies {
implementation("com.github.nickthedick1994:snapapi-kotlin:1.1.0")
}For Maven:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.github.nickthedick1994</groupId>
<artifactId>snapapi-kotlin</artifactId>
<version>1.1.0</version>
</dependency>import pics.snapapi.SnapAPI
// Initialize with your API key
val client = SnapAPI("YOUR_API_KEY")
// Or from environment variable
val client = SnapAPI(System.getenv("SNAPAPI_KEY"))local.properties or use BuildConfig fields to avoid hardcoding secrets.import pics.snapapi.SnapAPI
import java.io.File
val client = SnapAPI("YOUR_API_KEY")
// Using coroutines
val imageBytes = client.screenshot(
url = "https://example.com",
format = "png",
width = 1280,
height = 800
)
File("screenshot.png").writeBytes(imageBytes)
println("Screenshot saved!")val imageBytes = client.screenshot(
url = "https://example.com",
format = "png",
fullPage = true,
width = 1280
)val imageBytes = 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
)val result = client.screenshot(
url = "https://example.com",
format = "png",
responseType = "url"
)
println(result.url)
// → https://cdn.snapapi.pics/screenshots/abc123.pngval pdfBytes = client.pdf(
url = "https://example.com",
format = "A4",
printBackground = true,
margin = mapOf(
"top" to "20mm",
"bottom" to "20mm",
"left" to "15mm",
"right" to "15mm"
)
)
File("page.pdf").writeBytes(pdfBytes)val pdfBytes = client.pdf(
html = "<h1>Invoice #1234</h1><p>Total: $99.00</p>",
format = "A4",
printBackground = true
)val content = client.extract(
url = "https://example.com/blog-post",
format = "markdown"
)
println(content.markdown)
// → # Blog Post Title\n\nArticle content here...data class Product(
val title: String,
val price: Double,
val description: String
)
val product = client.extract<Product>(
url = "https://example.com/product",
format = "structured"
)
println("${product.title} - $${product.price}")val videoBytes = client.video(
url = "https://example.com",
duration = 5, // seconds
width = 1280,
height = 720,
format = "mp4"
)
File("recording.mp4").writeBytes(videoBytes)import pics.snapapi.SnapAPI
import pics.snapapi.exceptions.*
val client = SnapAPI("YOUR_API_KEY")
try {
val image = client.screenshot(url = "https://example.com")
File("screenshot.png").writeBytes(image)
} catch (e: AuthenticationException) {
println("Invalid API key")
} catch (e: RateLimitException) {
println("Rate limited. Retry after ${e.retryAfter}s")
} catch (e: BadRequestException) {
println("Bad request: ${e.message}")
} catch (e: SnapAPIException) {
println("API error (${e.status}): ${e.message}")
}import androidx.compose.runtime.*
import androidx.compose.foundation.Image
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import coil.compose.rememberAsyncImagePainter
import pics.snapapi.SnapAPI
@Composable
fun ScreenshotScreen() {
val client = remember { SnapAPI(BuildConfig.SNAPAPI_KEY) }
var imageBytes by remember { mutableStateOf<ByteArray?>(null) }
var isLoading by remember { mutableStateOf(false) }
Column {
imageBytes?.let { bytes ->
Image(
painter = rememberAsyncImagePainter(bytes),
contentDescription = "Screenshot"
)
}
Button(
onClick = {
isLoading = true
scope.launch {
imageBytes = client.screenshot(
url = "https://example.com",
format = "png"
)
isLoading = false
}
},
enabled = !isLoading
) {
Text("Capture Screenshot")
}
}
}