Ready-to-copy Kotlin code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses OkHttp or java.net.http.
The simplest way to capture a screenshot. Just a URL and your API key.
val client = OkHttpClient()
val url = "https://api.screenshotdev.com/v1/screenshot".toHttpUrl().newBuilder()
.addQueryParameter("url", "https://example.com")
.addQueryParameter("access_key", "YOUR_ACCESS_KEY")
.build()
val response = client.newCall(Request.Builder().url(url).build()).execute()
File("screenshot.png").writeBytes(response.body!!.bytes())Complete working script with options for viewport, format, full page capture, and error handling.
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File
import java.io.IOException
val apiKey = "YOUR_ACCESS_KEY"
val baseUrl = "https://api.screenshotdev.com/v1/screenshot"
val client = OkHttpClient()
fun takeScreenshot(
url: String,
format: String = "webp",
width: Int = 1280,
fullPage: Boolean = false,
darkMode: Boolean = false,
): ByteArray {
val httpUrl = baseUrl.toHttpUrl().newBuilder()
.addQueryParameter("url", url)
.addQueryParameter("access_key", apiKey)
.addQueryParameter("format", format)
.addQueryParameter("width", width.toString())
.addQueryParameter("full_page", fullPage.toString())
.addQueryParameter("dark_mode", darkMode.toString())
.build()
val request = Request.Builder().url(httpUrl).build()
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
val body = response.body?.string() ?: ""
throw IOException("Screenshot failed (${response.code}): $body")
}
return response.body!!.bytes()
}
fun main() {
val img = takeScreenshot("https://example.com", format = "webp", width = 1440, fullPage = true)
File("screenshot.webp").writeBytes(img)
println("Saved screenshot.webp (${img.size} bytes)")
}PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.
val url = "https://api.screenshotdev.com/v1/pdf".toHttpUrl().newBuilder()
.addQueryParameter("url", "https://example.com")
.addQueryParameter("access_key", "YOUR_ACCESS_KEY")
.addQueryParameter("format", "a4")
.addQueryParameter("landscape", "false")
.addQueryParameter("margin", "20px")
.build()
val response = client.newCall(Request.Builder().url(url).build()).execute()
File("page.pdf").writeBytes(response.body!!.bytes())import kotlinx.coroutines.*
val urls = listOf(
"https://example.com",
"https://github.com",
"https://news.ycombinator.com",
)
runBlocking {
urls.map { url ->
async(Dispatchers.IO) {
val img = takeScreenshot(url, format = "webp")
val host = java.net.URI(url).host
File("$host.webp").writeBytes(img)
println(" $host.webp (${img.size} bytes)")
}
}.awaitAll()
}import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
val json = JSONObject(mapOf(
"url" to "https://example.com",
"access_key" to "YOUR_ACCESS_KEY",
"webhook_url" to "https://your-app.com/api/webhooks/screenshot",
"format" to "webp",
"full_page" to true,
))
val body = json.toString().toRequestBody("application/json".toMediaType())
val request = Request.Builder()
.url("https://api.screenshotdev.com/v1/screenshot")
.post(body)
.build()
val response = client.newCall(request).execute()
val job = JSONObject(response.body!!.string())
println("Job queued: ${job.getString("id")}")val url = baseUrl.toHttpUrl().newBuilder()
.addQueryParameter("url", "https://example.com")
.addQueryParameter("access_key", apiKey)
.addQueryParameter("inject_css", "body { font-family: 'Inter'; } .ad { display: none; }")
.addQueryParameter("inject_js", "document.querySelectorAll('[data-cookie-banner]').forEach(el => el.remove())")
.addQueryParameter("block_ads", "true")
.build()
val response = client.newCall(Request.Builder().url(url).build()).execute()
File("clean.webp").writeBytes(response.body!!.bytes())import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.smithy.kotlin.runtime.content.ByteStream
// 1. Capture
val img = takeScreenshot("https://example.com", format = "webp")
// 2. Upload
S3Client { region = "us-east-1" }.use { s3 ->
s3.putObject(PutObjectRequest {
bucket = "my-screenshots"
key = "captures/example-com.webp"
contentType = "image/webp"
body = ByteStream.fromBytes(img)
})
}Test the API in the interactive playground — pre-filled with Kotlin snippets.
Install the HTTP library used in the examples above.
implementation("com.squareup.okhttp3:okhttp:4.12.0")Uses OkHttp or java.net.http. Any HTTP client that supports GET requests will work.
100 free API calls per month. No credit card. First screenshot in under 30 seconds.