# Callbacks & API Params

### Callbacks

Using the `statusURL` is a convenient way to test the API or get started quickly. For environments that require more robust or automated response handling, **callbacks** are are available for streamlined and efficient response handling. Currently, only `webhook` callbacks are supported but we are planning to expand this in the future (e.g. direct database callbacks, AWS S3, etc).

***Hint:*** [*Webhook.site*](https://webhook.site/) *is a free online service to test webhooks without requiring you to build a complex infrastructure.*

When using a **callback,** you don’t need to call the `statusURL` (although you still can) to fetch the status and results of the job. Once the job is complete, our system will call the webhook URL you provide and send the response data.

An example of using a webhook callback:

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

```bash
curl --request POST \
  --url "https://async.scraperapi.com/jobs" \
  --header "Content-Type: application/json" \
  --data '{
    "apiKey": "API_KEY",
    "url": "https://example.com",
    "callback": {
      "type": "webhook",
      "url": "YYYYY"
    }
  }'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

r = requests.post(
    url='https://async.scraperapi.com/jobs',
    json={
        #Replace with your actual API Key.
        "apiKey": "API_KEY",
        "url": "https://example.com",
        "callback": {
            "type": "webhook",
            #Replace with your actual Webhook URL.
            "url": "YYYYY"
        }
    }
)

print(r.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import axios from 'axios';

(async () => {
  const { data } = await axios({
    data: {
      //Replace with your actual API Key.
      apiKey: 'API_KEY',
      callback: {
        type: 'webhook',
        //Replace with your actual Webhook URL.
        url: 'YYYYY'
      },
      url: 'https://example.com'
    },
    headers: { 'Content-Type': 'application/json' },
    method: 'POST',
    url: 'https://async.scraperapi.com/jobs'
  });

  console.log(data);
})();
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$payload = json_encode([
    #Replace with your actual API Key.
    "apiKey" => "API_KEY",
    "url"    => "https://example.com",
    "callback" => [
        "type" => "webhook",
        #Replace with your actual Webhook URL.
        "url"  => "YYYYY"
    ]
]);

$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')

website_content = Net::HTTP.post(uri, {
#Replace with your actual API Key.
"apiKey" => "API_KEY",
"callback" => {
#Replace with your actual Webhook URL.
"type" => "webhook",
"url" => "YYYYY"
},
"url" => "https://example.com"
}.to_json, "Content-Type" => "application/json")
print(website_content.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 with your actual API Key.
                    + "\"apiKey\": \"API_KEY\","
                    + "\"url\": \"https://example.com\","
                    + "\"callback\": {"
                    +     "\"type\": \"webhook\","
                    //Replace with your actual Webhook URL.
                    +     "\"url\": \"YYYYY\""
                    + "}"
                    + "}";

            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 %}

{% hint style="warning" %}
By default, we'll call the webhook URL you provide for successful requests only. If you'd like to receive data on failed attempts too, you will have to include the **`expectsUnsuccessReport: true`** parameter in your request structure, **as part of the payload**.
{% endhint %}

Example callback response for a failed request:

```json5
{
  id: 'db726d84-2609-4709-93cb-88c4a9fa4288',
  attempts: 50,
  status: 'failed',
  statusUrl: 'https://async.scraperapi.com/jobs/db726d84-2609-4709-93cb-88c4a9fa4288',
  failReason: 'failed_due_to_timeout',
  url: 'http://httpbin.org/status/500'
}
```

***Note:** The system will try to invoke the webhook URL 3 times, then it cancels the job. So please make sure that the webhook URL is available through the public internet and will be capable of handling the traffic that you need.*

### API Params

You can use the standard API parameters the same way you’d use them with our synchronous API. These parameters should go into an `apiParams` object inside the POST data:

```json5
{
"apiKey": "API_KEY",
"apiParams": {
"autoparse": false, // boolean
"country_code": "us", // string, see: https://api.scraperapi.com/geo
"keep_headers": true, // boolean
"device_type": "desktop", // desktop | mobile
"follow_redirect": false, // boolean, true by default
"premium": true, // boolean
"ultra_premium": true, // boolean
"render": false, // boolean
"wait_for_selector": "#replace-with-selector", // works in combination with render=true
"screenshot": true, // boolean
"retry_404": false, // boolean
"output_format": "text" // text | markdown. Default is HTML.
},
"url": "https://example.com",
"expectUnsuccessReport": true, // boolean, returns data for failed jobs
"timeoutSec": "value", // number of seconds for the timeout. timeoutSec=600 --> 600 seconds.
"meta": "value" // Object. User-defined metadata returned unchanged in callbacks and responses. Useful for request correlation and tracking.
}

// Note: expectUnsuccessReport, timeoutSec and meta are exclusive to the Async API
```


---

# 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/asynchronous-api/callbacks-and-api-params.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.
