detach
Type: <boolean>
Default: false
Default: false
It queues the request over the target 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
microlink screenshot https://microlink.io --detachcURL Microlink API example
curl -G "https://api.microlink.io" \
-d "url=https://microlink.io" \
-d "screenshot=true" \
-d "detach=true"JavaScript Microlink API example
import createClient from 'microlink.io'
const microlink = createClient()
const data = await microlink.screenshot('https://microlink.io', {
detach: true
})Python Microlink API example
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
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.bodyPHP Microlink API example
<?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
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))
}import createClient from 'microlink.io'
const microlink = createClient()
const data = await microlink.screenshot('https://microlink.io', {
detach: true
})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
-
Send the request with
detach. If the result is already cached, you get the normal200response right away. -
Otherwise the request is queued, and the API answers
202with aRetry-Afterheader: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" } -
Wait at least the seconds in
Retry-After(5), then send the same request again,detachincluded. It answers202while the request is still waiting or running. -
Once it has finished, the same request answers with the final response: the
200you would get withoutdetach, 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 can be sent once and left.
While you poll, read the answer this way:
202,503with the EDETACHQUEUE code, or429with the ECONCURRENCY code: send it again.EDETACHQUEUEmeans the request could not be queued yet, andECONCURRENCYthat too many requests arrived in the same second. A429ECONCURRENCYabout a function (itsdatanamesfunction) is the outcome of the request, not a reason to repeat.429with ERATE,401or403: 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
A detached request is identified by three things:
- its cache key: the URL and the parameters that change the response, such as
screenshotordata, plus cacheKey when you set one; - its 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, staleTtl, filter or embed, do not make a new request.
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 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. The limits inside it do not grow:
- 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 prints in one pass, so a document too large for that answers 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
Once the request finishes, its answer is kept for 24 hours for whoever sent it, and a successful response is also cached like any other. During those 24 hours the same request returns the same answer, whatever ttl it sends, and
detach cannot be combined with force.To run it again sooner, make it a new request: a different timeout, or a new 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), is kept after the first run. An invalid parameter is never queued: the first request answers with the error. A function on the free plan that reaches its concurrency limit (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 (
503): the request could not be queued. Send it again afterRetry-After. - EDETACHFAILED (
500): the request did not finish after several attempts. - EINVALQUERY (
400):detachwas combined withforce, the URL and headers are too large to queue, or the endpoint does not support it.
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 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 for detached requests.