# detach

Type: \<boolean>\
Default: false

It queues the request over the target [url](https://microlink.io/docs/api/parameters/url) instead of holding the connection open until it finishes. The API answers at once, runs the request in the background, and keeps the result for you to collect.

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://microlink.io' URL with 'screenshot' & 'detach' API parameters:

CLI Microlink API example

```bash
microlink screenshot https://microlink.io --detach
```

cURL Microlink API example

```bash
curl -G "https://api.microlink.io" \
  -d "url=https://microlink.io" \
  -d "screenshot=true" \
  -d "detach=true"
```

JavaScript Microlink API example

```javascript
import createClient from 'microlink.io'

const microlink = createClient()

const data = await microlink.screenshot('https://microlink.io', {
  detach: true
})
```

Python Microlink API example

```python
import requests

url = "https://api.microlink.io/"

querystring = {
    "url": "https://microlink.io",
    "screenshot": "true",
    "detach": "true"
}

response = requests.get(url, params=querystring)

print(response.json())
```

Ruby Microlink API example

```ruby
require 'uri'
require 'net/http'

base_url = "https://api.microlink.io/"

params = {
  url: "https://microlink.io",
  screenshot: "true",
  detach: "true"
}

uri = URI(base_url)
uri.query = URI.encode_www_form(params)

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Get.new(uri)
response = http.request(request)

puts response.body
```

PHP Microlink API example

```php
<?php

$baseUrl = "https://api.microlink.io/";

$params = [
    "url" => "https://microlink.io",
    "screenshot" => "true",
    "detach" => "true"
];

$query = http_build_query($params);
$url = $baseUrl . '?' . $query;

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET"
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #: " . $err;
} else {
    echo $response;
}
```

Golang Microlink API example

```go
package main

import (
    "fmt"
    "net/http"
    "net/url"
    "io"
)

func main() {
    baseURL := "https://api.microlink.io"

    u, err := url.Parse(baseURL)
    if err != nil {
        panic(err)
    }
    q := u.Query()
    q.Set("url", "https://microlink.io")
    q.Set("screenshot", "true")
    q.Set("detach", "true")
    u.RawQuery = q.Encode()

    req, err := http.NewRequest("GET", u.String(), nil)
    if err != nil {
        panic(err)
    }

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(body))
}
```

```javascript
import createClient from 'microlink.io'

const microlink = createClient()

const data = await microlink.screenshot('https://microlink.io', {
  detach: true
})
```

Click to run the code and see the API response

Use it when a request needs more time than a connection allows, or when you send many requests at once and do not want any of them to time out. It is available on `api.microlink.io` and `pro.microlink.io`.

## [How it works](https://microlink.io/docs/api/parameters/detach#how-it-works)

1. Send the request with `detach`. If the result is already cached, you get the normal `200` response right away.

2. Otherwise the request is queued, and the API answers `202` with a `Retry-After` header:

   ```
   content-type   : application/json; charset=utf-8
   x-pricing-plan : pro
   cache-control  : private, no-cache, no-store, max-age=0
   retry-after    : 5
   ```

   ```
   { "status": "pending" }
   ```

3. Wait at least the seconds in `Retry-After` (5), then send the **same request** again, `detach` included. It answers `202` while the request is still waiting or running.

4. Once it has finished, the same request answers with the final response: the `200` you would get without `detach`, or the error the request ended with.

The request runs and its answer is kept whether or not you come back for it, so a job that only warms the [cache](https://microlink.io/docs/api/basics/cache) can be sent once and left.

While you poll, read the answer this way:

- `202`, `503` with the [EDETACHQUEUE](https://microlink.io/docs/api/basics/error-codes#edetachqueue) code, or `429` with the [ECONCURRENCY](https://microlink.io/docs/api/basics/error-codes#econcurrency) code: send it again. `EDETACHQUEUE` means the request could not be queued yet, and `ECONCURRENCY` that too many requests arrived in the same second. A `429` `ECONCURRENCY` about a [function](https://microlink.io/docs/api/parameters/function) (its `data` names `function`) is the outcome of the request, not a reason to repeat.
- `429` with [ERATE](https://microlink.io/docs/api/basics/error-codes#erate), `401` or `403`: an answer about your key or quota, not about the request. Its outcome is still kept.
- Anything else is the outcome of the request, even when it carries its own `Retry-After`.

Keep `detach` on every repeat. Without it, a request that is not cached yet runs while you wait, like any other request.

## [What makes a request the same](https://microlink.io/docs/api/parameters/detach#what-makes-a-request-the-same)

A detached request is identified by three things:

- its [cache](https://microlink.io/docs/api/basics/cache) key: the URL and the parameters that change the response, such as `screenshot` or `data`, plus [cacheKey](https://microlink.io/docs/api/parameters/cacheKey) when you set one;
- its [timeout](https://microlink.io/docs/api/parameters/timeout);
- who sent it: the API key on `pro.microlink.io`, or the IP address on the free endpoint.

Sending the same request again while it is pending does not queue it twice. Parameters that only shape the answer, such as [ttl](https://microlink.io/docs/api/parameters/ttl), [staleTtl](https://microlink.io/docs/api/parameters/staleTtl), [filter](https://microlink.io/docs/api/parameters/filter) or [embed](https://microlink.io/docs/api/parameters/embed), do not make a new request.

## [Time budget](https://microlink.io/docs/api/parameters/detach#time-budget)

A detached request has **5 minutes** to finish on every plan, counted from the moment it starts running, instead of the 30 seconds (free) or 90 seconds (pro) of a request you wait for. A [timeout](https://microlink.io/docs/api/parameters/timeout) sets a shorter one; a longer one is capped at 5 minutes.

The budget covers the whole request: a slow page, a long render, an [animated screenshot](https://microlink.io/docs/api/parameters/screenshot/animated). The limits inside it do not grow:

- [function](https://microlink.io/docs/api/parameters/function) code still runs at most 60 seconds on pro and 10 seconds on free, so a function that navigates several pages does not get more time for it;
- each page load gets under 3 minutes, and each wait for a selector, network idle or a function 75 seconds;
- a [pdf](https://microlink.io/docs/api/parameters/pdf) prints in one pass, so a document too large for that answers [EPDFTOOLARGE](https://microlink.io/docs/api/basics/error-codes#epdftoolarge).

A request waits in the queue until a server has room for it. A request that keeps failing runs up to 3 times, each with its full budget. The pause between runs is 15 seconds, then 30, or the error's own `Retry-After` when that is longer, up to 15 minutes. Its error can take up to about 45 minutes to be final, so give your polling loop a deadline of about an hour. Giving up does not cancel the request: sending it again later picks up its answer.

## [Results and errors](https://microlink.io/docs/api/parameters/detach#results-and-errors)

Once the request finishes, its answer is kept for **24 hours** for whoever sent it, and a successful response is also [cached](https://microlink.io/docs/api/basics/cache) like any other. During those 24 hours the same request returns the same answer, whatever [ttl](https://microlink.io/docs/api/parameters/ttl) it sends, and `detach` cannot be combined with [force](https://microlink.io/docs/api/parameters/force).

To run it again sooner, make it a new request: a different [timeout](https://microlink.io/docs/api/parameters/timeout), or a new [cacheKey](https://microlink.io/docs/api/parameters/cacheKey) on pro. You can also send it with `force` and without `detach`: that runs it synchronously, with the normal time limit, and when it succeeds the detached request answers with that result.

A request that ends in a `408`, `424`, `429` or `5xx` status (it ran out of time, the page did not answer, or a server failed) is tried up to 3 times in all before its error is kept. Any other error, such as a PDF too large to print ([EPDFTOOLARGE](https://microlink.io/docs/api/basics/error-codes#epdftoolarge)), is kept after the first run. An invalid parameter is never queued: the first request answers with the error. A [function](https://microlink.io/docs/api/parameters/function) on the free plan that reaches its concurrency limit ([ECONCURRENCY](https://microlink.io/docs/api/basics/error-codes#econcurrency)) is not counted as a failure: it waits in the queue for its turn, for up to 24 hours, and then that error is kept.

Errors that only happen with `detach`:

- [EDETACHQUEUE](https://microlink.io/docs/api/basics/error-codes#edetachqueue) (`503`): the request could not be queued. Send it again after `Retry-After`.
- [EDETACHFAILED](https://microlink.io/docs/api/basics/error-codes#edetachfailed) (`500`): the request did not finish after several attempts.
- [EINVALQUERY](https://microlink.io/docs/api/basics/error-codes#einvalquery) (`400`): `detach` was combined with `force`, the URL and headers are too large to queue, or the endpoint does not support it.

## [Billing](https://microlink.io/docs/api/parameters/detach#billing)

Every request counts, the repeats included; the background run does not. A request that takes 2 minutes costs about 25 requests if you repeat it every 5 seconds, and about 8 if you wait a little longer each time, up to 30 seconds. The [background requests](https://microlink.io/docs/guides/common/background-requests) guide has a loop that does that.

On the free endpoint, where a day is 5 requests, polling every 5 seconds covers about 20 seconds of work. The request also belongs to your IP address there, so a client whose address changes between repeats (a serverless function, a phone) queues a new request each time. Use an [API key](https://microlink.io/docs/api/basics/authentication) for detached requests.
