# Overview

Some pages require JavaScript rendering to fully load the contents. Our Rendering service spawns headless browsers to execute those page scripts before returning the response.

To enable JS Rendering, include the `render=true` parameter with your requests. The API will fetch the page using a headless browser instance. **This feature is available on all plans.**

In addition to JS rendering, you can use the `wait_for_selector` parameter to instruct the API to wait for a specific element to appear on the page, before returning the response. This is useful for pages where content appears with a higher delay. This parameter must be used in combination with `render=true` and will not work if specified on its own.

### Passing JS Rendering as part of the URL

* **API REQUEST**

{% tabs %}
{% tab title="cURL" %}

```bash
curl --request GET \
  --url 'https://api.scraperapi.com?api_key=API_KEY&render=true&url=https://example.com/'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

target_url = 'https://example.com/'
# Replace the value for api_key with your actual API Key.
api_key = 'API_KEY'

request_url = (
    f'https://api.scraperapi.com?'
    f'api_key={api_key}'
    f'&render=true'
    f'&url={target_url}'
)

response = requests.get(request_url)
print(response.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import request from 'node-fetch';

// Replace the value for api_key with your actual API Key.
const url = 'http://api.scraperapi.com/?api_key=API_KEY&render=true&url=https://example.com/';

request(url)
  .then(response => {
    console.log(response);
  })
  .catch(error => {
    console.error(error);
  });
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

// Replace the value for api_key with your actual API Key.
$url = "https://api.scraperapi.com?api_key=API_KEY&render=true&url=https://example.com";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
} else {
    print_r($response);
}

curl_close($ch);
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'

params = {
  # Replace the value for api_key with your actual API Key.
  api_key: "API_KEY",
  render: "true",
  url: "https://example.com/"
}

uri = URI('https://api.scraperapi.com/')
uri.query = URI.encode_www_form(params)

website_content = Net::HTTP.get(uri)
puts website_content
```

{% endtab %}

{% tab title="Java" %}

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // Replace the value for api_key with your actual API Key.
        String apiKey = "API_KEY";
        String targetUrl = "https://example.com/";

        String scraperApiUrl =
                "https://api.scraperapi.com?api_key=" + apiKey
                + "&render=true"
                + "&url=" + targetUrl;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(scraperApiUrl))
                .GET()
                .build();

        HttpResponse<String> response =
                client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
    }
}
```

{% endtab %}
{% endtabs %}

* **ASYNC REQUEST**

{% tabs %}
{% tab title="cURL" %}

```bash
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
        "apiKey": "API_KEY",
        "url": "https://example.com/",
        "apiParams": {
          "render": "true"
        }
      }' \
  "https://async.scraperapi.com/jobs"
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

r = requests.post(
    url='https://async.scraperapi.com/jobs',
    json={
        # Replace the value for api_key with your actual API Key.
        'apiKey': 'API_KEY',
        'render': 'true',
        'url': 'https://example.com/'
    }
)

print(r.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import axios from 'axios';

(async () => {
  try {
    const { data } = await axios({
      method: 'POST',
      url: 'https://async.scraperapi.com/jobs',
      headers: { 'Content-Type': 'application/json' },
      data: {
        // Replace the value for api_key with your actual API Key.
        apiKey: 'API_KEY',
        url: 'https://example.com',
        apiParams: {
          render: 'true'
        }
      }
    });

    console.log(data);
  } catch (error) {
    console.error(error);
  }
})();
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$payload = json_encode([
    // Replace the value for api_key with your actual API Key.
    "apiKey" => "API_KEY",
    "url"    => "https://example.com",
    "apiParams" => [
        "render"  => "true"
    ]
]);

$ch = curl_init("https://async.scraperapi.com/jobs");

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

print_r($response);
?>
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'
require 'json'

uri = URI('https://async.scraperapi.com/jobs')

payload = {
  # Replace the value for api_key with your actual API Key.
  "apiKey" => "API_KEY",
  "url" => "https://example.com",
  "apiParams" => {
    "render" => "true"
  }
}

response = Net::HTTP.post(uri, payload.to_json, "Content-Type" => "application/json")
puts response.body
```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://async.scraperapi.com/jobs");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            String payload = "{"
                    // Replace the value for api_key with your actual API Key.
                    + "\"apiKey\": \"API_KEY\","
                    + "\"url\": \"https://example.com\","
                    + "\"apiParams\": {"
                    + "\"render\": \"true\""
                    + "}"
                    + "}";

            try (OutputStream os = conn.getOutputStream()) {
                os.write(payload.getBytes(StandardCharsets.UTF_8));
            }

            try (BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream()
            ))) {
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) response.append(line);
                System.out.println(response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

{% endtab %}
{% endtabs %}

* **PROXY MODE**

{% tabs %}
{% tab title="cURL" %}

```bash
curl --proxy 'http://scraperapi.render=true:API_KEY@proxy-server.scraperapi.com:8001' \
  -k \
  'https://example.com/'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

# Replace the value for api_key with your actual API Key.
proxies = {
    "http": "http://scraperapi.render=true:API_KEY@proxy-server.scraperapi.com:8001"
}

r = requests.get('http://example.com/', proxies=proxies, verify=False)
print(r.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import axios from 'axios';

axios.get('http://example.com/', {
  method: 'GET',
  proxy: {
    host: 'proxy-server.scraperapi.com',
    port: 8001,
    auth: {
      username: 'scraperapi.render=true',
      // Replace the value for password with your actual API Key.
      password: 'API_KEY'
    },
    protocol: 'http'
  }
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.log(error);
  });
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/");
curl_setopt($ch, CURLOPT_PROXY, 
    // Replace the value for api_key with your actual API Key.
    "http://scraperapi.render=true:API_KEY@proxy-server.scraperapi.com:8001"
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'httparty'

HTTParty::Basement.default_options.update(verify: false)

response = HTTParty.get('http://example.com/', {
  http_proxyaddr: "proxy-server.scraperapi.com",
  http_proxyport: 8001,
  http_proxyuser: "scraperapi.render=true",
  # Replace the value for http_proxypass with your actual API Key.
  http_proxypass: "API_KEY"
})

results = response.body
puts results
```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.security.cert.X509Certificate;

public class Main {
    public static void main(String[] args) {
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() { return null; }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                }
            }, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);

            String apiKey = "API_KEY"; // Replace with your actual API Key.
            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                        "scraperapi.render=true", apiKey.toCharArray()
                    );
                }
            });

            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            System.setProperty("https.proxyHost", "proxy-server.scraperapi.com");
            System.setProperty("https.proxyPort", "8001");

            HttpsURLConnection conn = (HttpsURLConnection) new URL("https://example.com/").openConnection();
            conn.setRequestMethod("GET");

            BufferedReader in;
            if (conn.getResponseCode() >= 400) {
                in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }

            StringBuilder resp = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                resp.append(line).append("\n");
            }
            in.close();

            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

{% endtab %}
{% endtabs %}

### Passing JS Rendering as part of the headers

* **API REQUEST**

{% tabs %}
{% tab title="cURL" %}

```bash
 curl --request GET \
  --url 'https://api.scraperapi.com?url=https://www.example.com' \
  --header 'x-sapi-api_key: API_KEY' \
  --header 'x-sapi-render: true'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://api.scraperapi.com"
params = {
    "url": "https://www.example.com"
}

headers = {
    # Replace the value for api_key with your actual API Key.
    "x-sapi-api_key": "API_KEY",
    "x-sapi-render": "true"
}

response = requests.get(url, params=params, headers=headers)
print(response.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import fetch from "node-fetch";

const url = new URL("https://api.scraperapi.com");
url.searchParams.append("url", "https://www.example.com");

const response = await fetch(url, {
  method: "GET",
  headers: {
    // Replace the value for api_key with your actual API Key.
    "x-sapi-api_key": "API_KEY",
    "x-sapi-render": "true",
  },
});

const body = await response.text();
console.log(body);
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$url = "https://api.scraperapi.com?url=https://www.example.com";
$ch = curl_init();

$headers = array(
    // Replace the value for api_key with your actual API Key.
    'x-sapi-api_key: API_KEY',
    'x-sapi-render: true'
);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

$response = curl_exec($ch);

curl_close($ch);

print_r($response);
?>
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'
require 'json'

params = {
  url: "https://www.example.com"
}

uri = URI('https://api.scraperapi.com')
uri.query = URI.encode_www_form(params)

req = Net::HTTP::Get.new(uri)

# Replace the value for api_key with your actual API Key.
req['x-sapi-api_key'] = 'API_KEY'
req['x-sapi-render'] = 'true'

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

website_content = http.request(req)
puts website_content.body
```

{% endtab %}

{% tab title="Java" %}

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // Replace the value for api_key with your actual API Key.
        String apiKey = "API_KEY";
        String targetUrl = "https://www.example.com/";

        String scraperApiUrl = "https://api.scraperapi.com?url=" + targetUrl;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(scraperApiUrl))
                .GET()
                .header("x-sapi-api_key", apiKey)
                .header("x-sapi-render", "true")
                .build();

        HttpResponse<String> response =
                client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
    }
}
```

{% endtab %}
{% endtabs %}

* **ASYNC REQUEST**

{% tabs %}
{% tab title="cURL" %}

```bash
curl -X POST https://async.scraperapi.com/jobs \
  -H "Content-Type: application/json" \
  -d '{
    "apiKey": "API_KEY",
    "url": "https://www.example.com", 
    "headers": {
      "x-sapi-render": "true", 
    }
  }'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://async.scraperapi.com/jobs"

payload = {
    # Replace the value for api_key with your actual API Key.
    "apiKey": "API_KEY",
    "url": "https://www.example.com",
    "headers": {
        "x-sapi-render": "true"
    }
}

response = requests.post(url, json=payload)
print(response.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import axios from "axios";

const url = "https://async.scraperapi.com/jobs";

const payload = {
  // Replace the value for api_key with your actual API Key.
  apiKey: "API_KEY",
  url: "https://www.example.com",
  headers: {
    "x-sapi-render": "true"
  }
};

axios.post(url, payload, {
  headers: {
    "Content-Type": "application/json"
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error.response?.data || error.message);
});
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$payload = json_encode([
    // Replace the value for api_key with your actual API Key.
    "apiKey" => "API_KEY",
    "url"    => "https://example.com",
    "headers" => [
        "x-sapi-render" => "true"
    ]
]);

$ch = curl_init("https://async.scraperapi.com/jobs");

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

print_r($response);
?>
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'
require 'json'

uri = URI('https://async.scraperapi.com/jobs')

payload = {
  # Replace the value for api_key with your actual API Key.
  "apiKey" => "API_KEY",
  "url" => "https://example.com",
  "headers" => {
    "x-sapi-render" => "true"
  }
}

response = Net::HTTP.post(
  uri,
  payload.to_json,
  "Content-Type" => "application/json"
)

puts response.body
```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://async.scraperapi.com/jobs");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            String payload = "{"
                    // Replace the value for api_key with your actual API Key.
                    + "\"apiKey\": \"API_KEY\","
                    + "\"url\": \"https://example.com\","
                    + "\"headers\": {"
                    + "\"x-sapi-render\": \"true\""
                    + "}"
                    + "}";

            try (OutputStream os = conn.getOutputStream()) {
                os.write(payload.getBytes(StandardCharsets.UTF_8));
            }

            try (BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getResponseCode() == 200 ? conn.getInputStream() : conn.getErrorStream()
            ))) {
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
                System.out.println(response.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

{% endtab %}
{% endtabs %}

* **PROXY MODE**

{% tabs %}
{% tab title="cURL" %}

```bash
curl --request GET \
  --url 'https://www.example.com' \
  --proxy 'http://scraperapi:API_KEY@proxy-server.scraperapi.com:8001' \
  --header 'x-sapi-render: true' \
  -k
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://www.example.com"
# Replace the value for api_key with your actual API Key.
proxy = "http://scraperapi:API_KEY@proxy-server.scraperapi.com:8001"

proxies = {
    "http": proxy,
    "https": proxy,
}

headers = {
    "x-sapi-render": "true"
}

response = requests.get(url, headers=headers, proxies=proxies, verify=False)
print(response.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import axios from 'axios';
import https from 'https';

const url = 'https://www.example.com';
const agent = new https.Agent({ rejectUnauthorized: false });

axios.get(url, {
  headers: {
    'x-sapi-render': 'true'
  },
  proxy: {
    host: 'proxy-server.scraperapi.com',
    port: 8001,
    auth: {
      username: 'scraperapi',
      // Replace the value for password with your actual API Key.
      password: 'API_KEY'
    },
    protocol: 'http'
  },
  httpsAgent: agent
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com");
curl_setopt($ch, CURLOPT_PROXY,
    // Replace the value for api_key with your actual API Key.
    "http://scraperapi:API_KEY@proxy-server.scraperapi.com:8001"
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "x-sapi-render: true"
]);

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
} else {
    var_dump($response);
}

curl_close($ch);
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'httparty'

HTTParty::Basement.default_options.update(verify: false)

response = HTTParty.get('https://www.example.com', {
  http_proxyaddr: "proxy-server.scraperapi.com",
  http_proxyport: 8001,
  http_proxyuser: "scraperapi",
  # Replace the value for http_proxypass with your actual API Key.
  http_proxypass: "API_KEY",
  headers: {
    "x-sapi-render" => "true"
  }
})

results = response.body
puts results
```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.security.cert.X509Certificate;

public class Main {
    public static void main(String[] args) {
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() { return null; }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                }
            }, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);

            String apiKey = "API_KEY"; // Replace the value for api_key with your actual API Key.
            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                        "scraperapi", apiKey.toCharArray()
                    );
                }
            });

            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            System.setProperty("https.proxyHost", "proxy-server.scraperapi.com");
            System.setProperty("https.proxyPort", "8001");

            HttpsURLConnection conn = (HttpsURLConnection) new URL("https://www.example.com/").openConnection();
            conn.setRequestMethod("GET");

            conn.setRequestProperty("x-sapi-render", "true");

            BufferedReader in;
            if (conn.getResponseCode() >= 400) {
                in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }

            StringBuilder resp = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                resp.append(line).append("\n");
            }
            in.close();

            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.scraperapi.com/javascript-rendering/overview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
