/scraperapi:scraperapi-java-sdkfrom ScraperAPI
Best-practices reference for the ScraperAPI Java SDK covering Maven/Gradle setup, fluent builder API, request parameters, error handling, retry behavior, async jobs, and credit costs.
Ships in ScraperAPI. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Requires: Java 8+ for the SDK, Maven or Gradle, a ScraperAPI API key. The samples that call the API directly use java.net.http (Java 11+).
<dependency>
<groupId>com.scraperapi</groupId>
<artifactId>sdk</artifactId>
<version>1.2</version>
</dependency>
implementation 'com.scraperapi:sdk:1.2'
import com.scraperapi.ScraperApiClient;
ScraperApiClient client = new ScraperApiClient("YOUR_API_KEY");
Replace YOUR_API_KEY with your key from https://dashboard.scraperapi.com. Never commit a real key to source control.
country_code, device_type, session_number. There is no countryCode() or sessionNumber()..result() returns the response body as a String, whatever the status code. It doesn't throw on 401, 403, 429 or 500, and you can't read the status code. It only throws (kong.unirest.UnirestException) on timeouts and connection errors.render, premium, autoparse, country_code, device_type, session_number. For anything else (ultra_premium, wait_for_selector, output_format, max_cost, screenshot…), call the API directly. See Parameters the SDK Doesn't Have..timeout(75_000) (milliseconds) on every request..retry(n) doesn't retry n times. Any value above 0 only turns on retries of dropped connections; non-200 responses are never retried. .retry(0) turns it off..result() call resets Unirest's global configuration. If your app also uses Unirest, its settings get wiped.The SDK uses a fluent builder: client.get(url) returns a request, you chain parameters onto it, and .result() sends it and blocks until the body comes back.
// Simple GET, returns the body as a String
String html = client.get("https://example.com/").timeout(75_000).result();
// Multiple parameters
String html = client.get("https://example.com/")
.render(true)
.country_code("us")
.timeout(75_000)
.result();
| Situation | Approach |
|---|---|
| Single URL, synchronous | client.get(url).<params>.timeout(75_000).result() |
| Page loads content via JavaScript | Chain .render(true) |
| Site blocks datacenter proxies | Chain .premium(true) |
| Toughest anti-bot protection | apiGet with ultra_premium=true (not in the SDK) |
| Wait for an element, Markdown output, cost cap | apiGet with wait_for_selector, output_format, max_cost (not in the SDK) |
| You need the status code | apiGet |
| Multi-step / paginated flow on same domain | Chain .session_number("42") |
| 20+ URLs or batch jobs | Async endpoint via HttpClient |
| Supported platform (Amazon, Google, etc.) | Structured data endpoint |
// Render JavaScript before returning HTML
// Use when: page is a React/Vue/Angular SPA, or initial scrape returns empty content
// Cost: 10 credits per request (instead of 1)
String html = client.get("https://spa-site.com/").render(true).timeout(75_000).result();
Don't call .render(true) by default — try without it first. It adds cost and latency.
To wait for a DOM element, use apiGet with render=true and wait_for_selector.
// Route through a country-specific proxy — no extra credit cost
String html = client.get("https://example.com/").country_code("gb").timeout(75_000).result();
// Premium residential/mobile IPs — for sites that block datacenter proxies
// Cost: 10 credits (25 with render)
String html = client.get("https://hard-site.com/").premium(true).timeout(75_000).result();
For the toughest anti-bot protections, use ultra_premium=true through apiGet (30 credits, 75 with render).
premium and ultra_premium are mutually exclusive — never set both. ultra_premium ignores custom headers.
Escalation order: standard (1 cr) → render (10 cr) → premium (10 cr) → ultra_premium (30 cr).
// Reuse the same proxy IP across requests — useful for pagination and multi-step flows
// Sessions expire 15 minutes after last use. The SDK takes the session ID as a String.
String page1 = client.get("https://example.com/page1").session_number("42").timeout(75_000).result();
String page2 = client.get("https://example.com/page2").session_number("42").timeout(75_000).result();
// Forward a custom header to the target site (the SDK sets keep_headers=true for you)
String html = client.get("https://example.com/").header("Accept-Language", "de-DE").timeout(75_000).result();
// Emulate a mobile browser user-agent
String html = client.get("https://example.com/").device_type("mobile").timeout(75_000).result();
// Return structured JSON for supported sites (Amazon, Google, etc.)
String json = client.get("https://amazon.com/dp/B09V3KXJPB").autoparse(true).timeout(75_000).result();
// POST a JSON body to the target site through ScraperAPI
String result = client.post("https://example.com/api")
.header("Content-Type", "application/json")
.body("{\"key\":\"value\"}")
.timeout(75_000)
.result();
client.put(url) works the same way.
Call the API directly with java.net.http. This helper URL-encodes every value and returns the whole response, so you can check the status code. The structured, error-handling and escalation samples below reuse it.
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
private static final String API_KEY = "YOUR_API_KEY";
private static final HttpClient HTTP = HttpClient.newHttpClient();
// GET https://api.scraperapi.com/<path>?api_key=...&<params>
public static HttpResponse<String> apiGet(String path, Map<String, String> params) throws Exception {
Map<String, String> all = new LinkedHashMap<>(params);
all.put("api_key", API_KEY);
String query = all.entrySet().stream()
.map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));
HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.scraperapi.com/" + path + "?" + query))
.timeout(Duration.ofSeconds(75))
.GET()
.build();
return HTTP.send(req, HttpResponse.BodyHandlers.ofString());
}
// Ultra-premium
HttpResponse<String> resp = apiGet("", Map.of("url", "https://hardest-site.com/", "ultra_premium", "true"));
// Render and wait for an element, then return Markdown, never spending more than 10 credits
HttpResponse<String> resp = apiGet("", Map.of(
"url", "https://spa-site.com/",
"render", "true",
"wait_for_selector", ".product-list",
"output_format", "markdown",
"max_cost", "10"));
Always start cheapest. Escalate only when the site blocks the previous tier. The SDK hides the status code, so check the body; the last tier goes through apiGet because the SDK has no ultra_premium.
import com.scraperapi.ScraperApiClient;
import com.scraperapi.ScraperApiRequest;
import kong.unirest.UnirestException;
import java.util.function.Supplier;
public static String scrapeWithEscalation(ScraperApiClient client, String url) throws Exception {
List<Supplier<ScraperApiRequest>> tiers = Arrays.asList(
() -> client.get(url), // 1 credit
() -> client.get(url).render(true), // 10 credits
() -> client.get(url).premium(true), // 10 credits
() -> client.get(url).premium(true).render(true)); // 25 credits
for (Supplier<ScraperApiRequest> tier : tiers) {
try {
String html = tier.get().timeout(75_000).result();
if (html != null && html.toLowerCase().contains("<html")) return html;
} catch (UnirestException e) {
// Timeout or connection error: try the next tier
}
}
HttpResponse<String> resp = apiGet("", Map.of("url", url, "ultra_premium", "true")); // 30 credits
return resp.statusCode() == 200 ? resp.body() : null;
}
.result() blocks the calling thread. For 20+ URLs, submit async jobs via the REST endpoint and
collect results concurrently. These use API_KEY and HTTP from apiGet above, plus Jackson
(com.fasterxml.jackson.core:jackson-databind).
import com.fasterxml.jackson.databind.ObjectMapper;
private static final ObjectMapper JSON = new ObjectMapper();
public static Map<String, Object> submitJob(String url) throws Exception {
// without expectUnsuccessReport, a job that fails never reaches "failed"
String body = JSON.writeValueAsString(Map.of("apiKey", API_KEY, "url", url, "expectUnsuccessReport", true));
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("https://async.scraperapi.com/jobs"))
.POST(HttpRequest.BodyPublishers.ofString(body))
.header("Content-Type", "application/json")
.build();
HttpResponse<String> resp = HTTP.send(req, HttpResponse.BodyHandlers.ofString());
if (resp.statusCode() / 100 != 2)
throw new RuntimeException("Job submit failed: HTTP " + resp.statusCode() + " " + resp.body());
return JSON.readValue(resp.body(), Map.class); // {id, statusUrl}
}
public static String pollJob(Map<String, Object> job, int maxWaitSec) throws Exception {
long deadline = System.currentTimeMillis() + maxWaitSec * 1000L;
while (System.currentTimeMillis() < deadline) {
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create((String) job.get("statusUrl")))
.GET().build();
Map<String, Object> data = JSON.readValue(
HTTP.send(req, HttpResponse.BodyHandlers.ofString()).body(), Map.class);
if ("finished".equals(data.get("status")))
return ((Map<?, ?>) data.get("response")).get("body").toString();
if ("failed".equals(data.get("status")))
throw new RuntimeException("Job " + job.get("id") + " failed");
Thread.sleep(5_000);
}
throw new RuntimeException("Job " + job.get("id") + " timed out");
}
For supported platforms, use structured endpoints instead of raw HTML scraping.
public static String structuredGet(String vertical, Map<String, String> params) throws Exception {
HttpResponse<String> resp = apiGet("structured/" + vertical, params);
if (resp.statusCode() != 200)
throw new RuntimeException("Error " + resp.statusCode() + ": " + resp.body());
return resp.body();
}
// Google SERP
String serp = structuredGet("google/search", Map.of("query", "java web scraping"));
// Amazon product
String product = structuredGet("amazon/product", Map.of("asin", "B09V3KXJPB"));
// Walmart search
String items = structuredGet("walmart/search", Map.of("query", "standing desk", "tld", "com"));
.result() never tells you the status code, so an invalid key or exhausted credits come back as
an ordinary String. When you need to react to errors, use apiGet and switch on the status:
public static String safeScrape(String url) throws Exception {
HttpResponse<String> resp = apiGet("", Map.of("url", url));
switch (resp.statusCode()) {
case 200: return resp.body();
case 401: throw new IllegalStateException("Invalid API key — check your API key");
case 403: throw new IllegalStateException("Out of API credits for this billing cycle — upgrade or enable overages at https://dashboard.scraperapi.com/billing (don't retry)");
case 429: throw new RuntimeException("Rate limit — reduce concurrency or use async");
case 500: case 503:
throw new RuntimeException("Failed after ScraperAPI retried for 70s (not charged) — retry; on protected sites add premium/ultra_premium");
default: throw new RuntimeException("Scrape failed with HTTP " + resp.statusCode() + ": " + url);
}
}
HTTP.send throws java.net.http.HttpTimeoutException if no response arrives within 75 seconds.
Status codes: 200 success, 401 bad key, 403 credits exhausted (don't retry), 404 target not found (charged), 429 concurrency limit, 500 failed after 70s of retries (not charged — retry, then escalate to premium/ultra_premium on protected sites).
| Request type | Credits |
|---|---|
Standard .result() | 1 |
.render(true) | 10 |
.premium(true) | 10 |
.premium(true).render(true) | 25 |
ultra_premium=true (apiGet) | 30 |
ultra_premium=true&render=true (apiGet) | 75 |
Those are the costs on a normal site. Some sites cost more per request, before any parameters:
| Site | Credits |
|---|---|
| Amazon, Walmart, eBay | 5 |
| Google, Bing (all subdomains) | 25 |
| 30 | |
| Sites behind Cloudflare, DataDome, PerimeterX or Akamai | 10 |
You're charged for 200 and 404 responses, and for requests you cancel before ScraperAPI's 70s
of retries finish. Each response's sa-credit-cost header shows what it cost; preview a cost
without spending credits with GET https://api.scraperapi.com/account/urlcost?api_key=...&url=...
(it accepts the same parameters). Source: https://docs.scraperapi.com/getting-started/quick-start/credits-and-requests-costs
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub scraperapi/scraperapi-skills --plugin scraperapi20 skills in this plugin
/scraperapi-agent-onboardingEntry point for agents and developers new to ScraperAPI. Read first when integrating ScraperAPI into an agent or app, setting up the MCP server for Claude Code or other LLM tools, choosing the right ScraperAPI product, or getting an API key. Trigger on: "how do I use ScraperAPI", "set up ScraperAPI", "connect ScraperAPI to Claude", "which ScraperAPI product should I use", "get web data for my agent", "add web scraping to my project", "ScraperAPI getting started". After reading, route to the narrower skill that owns the chosen path. If the user already named a language or SDK, n8n, Zapier, or the sapi CLI, use that skill directly instead.
Note: Transmits user-supplied queries, URLs, and content to ScraperAPI.
/scraperapi-asyncReference for ScraperAPI's Async Jobs REST API: submit scraping jobs in the background and get results by polling or webhook, including batch jobs. Consult when the user is scraping many URLs, needs non-blocking requests, or wants webhook delivery. Use when user asks: "how do I scrape 1000 URLs with ScraperAPI", "ScraperAPI async jobs", "batch scraping with ScraperAPI", "submit a scraping job and poll for results", "ScraperAPI webhook callback", "ScraperAPI batchjobs endpoint". Covers single and batch jobs, webhook callbacks, apiParams, async-only parameters, binary responses, retention, and error handling. It is language-agnostic: for SDK client code, use the language's SDK skill; to crawl a whole site by following links, use scraperapi-crawler.
Note: Transmits user-supplied queries, URLs, and content to ScraperAPI.
Other plugins' skills for the same job