Taking Screenshots
info
Currently, Browserless V2 is available in production via two domains: production-sfo.browserless.io
and production-lon.browserless.io
The screenshot
mutation allows for simple navigation to a site and capturing a screenshot. Browserless will respond with either a binary or base64 encode of a png
or jpg
(depending on parameters).
On this collection we'll look at:
Basic Usage
- BQL Query
- cURL
- Javascript
- Python
- Java
- C#
mutation Screenshot {
goto(url: "https://example.com") {
status
}
screenshot(omitBackground:true) {
base64
}
}
curl --request POST \
--url 'https://production-sfo.browserless.io/chromium/bql?token=YOUR_API_TOKEN_HERE' \
--header 'Content-Type: application/json' \
--data '{"query":"mutation Screenshot {\n goto(url: \"https://example.com\") {\n status\n }\n\n screenshot(omitBackground:true) {\n base64\n }\n}","variables":"","operationName":"Screenshot"}'
const endpoint = "https://production-sfo.browserless.io/chromium/bql";
const token = "YOUR_API_TOKEN_HERE";
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({"query":"mutation Screenshot {\n goto(url: \"https://example.com\") {\n status\n }\n\n screenshot(omitBackground:true) {\n base64\n }\n}","variables":"","operationName":"Screenshot"})
};
const url = `${endpoint}?token=${token}`;
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
import requests
endpoint = "https://production-sfo.browserless.io/chromium/bql"
query_string = {
"token": "YOUR_API_TOKEN_HERE",
}
headers = {
"Content-Type": "application/json",
}
payload = {
"query": "mutation Screenshot {\n goto(url: \"https://example.com\") {\n status\n }\n\n screenshot(omitBackground:true) {\n base64\n }\n}",
"variables": None,
"operationName": "Screenshot",
}
response = requests.post(endpoint, params=query_string, headers=headers, json=payload)
print(response.json())
String url = "https://production-sfo.browserless.io/chromium/bql";
String token = "YOUR_API_TOKEN_HERE";
String endpoint = String.format("%s?token=%s%s%s", url, token);
HttpResponse<String> response = Unirest.post(endpoint)
.header("Content-Type", "application/json")
.body({"query":"mutation Screenshot {\n goto(url: \"https://example.com\") {\n status\n }\n\n screenshot(omitBackground:true) {\n base64\n }\n}","variables":"","operationName":"Screenshot"})
.asString();
string url = "https://production-sfo.browserless.io/chromium/bql";
string token = "YOUR_API_TOKEN_HERE";
string endpoint = $"{url}?token={token}";
var payload = new
{
query = @"mutation Screenshot {
goto(url: ""https://example.com"") {
status
}
screenshot(omitBackground: true) {
base64
}
}",
variables = "",
operationName = "Screenshot"
};
using (HttpClient httpClient = new HttpClient())
{
var jsonPayload = System.Text.Json.JsonSerializer.Serialize(payload);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(endpoint, content);
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
BQL Schemas
For more details on BQL mutations, refer to the BrowserQL Schema reference pages.
Rest API
Taking Screenshots can also be done with Browserless Rest API. For endpoint details, parameters, and code samples, see the Browserless REST API.