Skip to content

function

Type: <string>
It runs JavaScript code with runtime access to a headless browser.

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

CLI Microlink API example

microlink https://microlink.io&function='({ page }) => page.evaluate("jQuery.fn.jquery")'&scripts=https://code.jquery.com/jquery-3.5.0.min.js

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://microlink.io" \
  -d "function=(%7B%20page%20%7D)%20%3D%3E%20page.evaluate(%22jQuery.fn.jquery%22)" \
  -d "scripts=https://code.jquery.com/jquery-3.5.0.min.js"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://microlink.io',
  ({ page }) => page.evaluate("jQuery.fn.jquery"),
  {
    scripts: [
      "https://code.jquery.com/jquery-3.5.0.min.js"
    ]
  }
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://microlink.io",
    "function": '''({ page }) => page.evaluate("jQuery.fn.jquery")''',
    "scripts": "https://code.jquery.com/jquery-3.5.0.min.js"
}

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",
  function: '({ page }) => page.evaluate("jQuery.fn.jquery")',
  scripts: "https://code.jquery.com/jquery-3.5.0.min.js"
}

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

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

$params = [
    "url" => "https://microlink.io",
    "function" => '({ page }) => page.evaluate("jQuery.fn.jquery")',
    "scripts" => "https://code.jquery.com/jquery-3.5.0.min.js"
];

$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)
    }
    fn := `({ page }) => page.evaluate("jQuery.fn.jquery")`

    q := u.Query()
    q.Set("url", "https://microlink.io")
    q.Set("function", fn)
    q.Set("scripts", "https://code.jquery.com/jquery-3.5.0.min.js")
    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))
}
The function runtime supports require() for any npm package:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://example.com' URL with 'function' API parameter:

CLI Microlink API example

microlink https://example.com&function='() => {
  const cheerio = require('"'"'cheerio'"'"')
  const $ = cheerio.load('"'"'<h1>Hello world</h1>'"'"')
  return $('"'"'h1'"'"').text()
}'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://example.com" \
  -d "function=()%20%3D%3E%20%7B%0A%20%20const%20cheerio%20%3D%20require('cheerio')%0A%20%20const%20%24%20%3D%20cheerio.load('%3Ch1%3EHello%20world%3C%2Fh1%3E')%0A%20%20return%20%24('h1').text()%0A%7D"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://example.com',
  () => {
    const cheerio = require('cheerio')
    const $ = cheerio.load('<h1>Hello world</h1>')
    return $('h1').text()
  }
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://example.com",
    "function": '''() => {
  const cheerio = require('cheerio')
  const $ = cheerio.load('<h1>Hello world</h1>')
  return $('h1').text()
}'''
}

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://example.com",
  function: "() => {
  const cheerio = require('cheerio')
  const $ = cheerio.load('<h1>Hello world</h1>')
  return $('h1').text()
}"
}

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

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

$params = [
    "url" => "https://example.com",
    "function" => "() => {
  const cheerio = require('cheerio')
  const $ = cheerio.load('<h1>Hello world</h1>')
  return $('h1').text()
}"
];

$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)
    }
    fn := `() => {
  const cheerio = require('cheerio')
  const $ = cheerio.load('<h1>Hello world</h1>')
  return $('h1').text()
}`

    q := u.Query()
    q.Set("url", "https://example.com")
    q.Set("function", fn)
    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))
}
Dependencies are detected automatically from your code and installed on-the-fly during the install phase.
The runtime restricts certain system capabilities for security. Operations such as spawning child processes or writing to the filesystem outside the sandbox are not permitted.

Request

The function will receive the parameters explained below as part of the user's request code.

url

The URL you asked for. Available whether or not the function uses page, so you do not need a browser just to know which origin you are running against:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://example.com' URL with 'function' API parameter:

CLI Microlink API example

microlink https://example.com&function='({ url }) => new URL("/robots.txt", url).href'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://example.com" \
  -d "function=(%7B%20url%20%7D)%20%3D%3E%20new%20URL(%22%2Frobots.txt%22%2C%20url).href"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://example.com',
  ({ url }) => new URL("/robots.txt", url).href
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://example.com",
    "function": '''({ url }) => new URL("/robots.txt", url).href'''
}

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://example.com",
  function: '({ url }) => new URL("/robots.txt", url).href'
}

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

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

$params = [
    "url" => "https://example.com",
    "function" => '({ url }) => new URL("/robots.txt", url).href'
];

$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)
    }
    fn := `({ url }) => new URL("/robots.txt", url).href`

    q := u.Query()
    q.Set("url", "https://example.com")
    q.Set("function", fn)
    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))
}
It is the URL you requested, not the one a redirect settles on. A function asked for on its own skips the ping that would resolve that hop, so data.url in the response is the same URL, and the two agree.
Use page.url() when you need the settled one, which resolves the page:
// requested http://github.com
({ url }) => url                  // => "http://github.com/"
async ({ page }) => page.url()    // => "https://github.com/"
Asking for ping explicitly settles data.url without changing the argument.

page

A Puppeteer Page for the URL you asked for.:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://example.com' URL with 'function' API parameter:

CLI Microlink API example

microlink https://example.com&function='({ page }) => page.title()'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://example.com" \
  -d "function=(%7B%20page%20%7D)%20%3D%3E%20page.title()"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://example.com',
  ({ page }) => page.title()
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://example.com",
    "function": "({ page }) => page.title()"
}

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://example.com",
  function: "({ page }) => page.title()"
}

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

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

$params = [
    "url" => "https://example.com",
    "function" => "({ page }) => page.title()"
];

$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://example.com")
    q.Set("function", "({ page }) => page.title()")
    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))
}
The page API is supported, so any method on that object is available.
Additionally, we provide methods that extend the page:

page.extract()

It takes the same rules as data and returns one value per rule:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://news.ycombinator.com' URL with 'function' API parameter:

CLI Microlink API example

microlink https://news.ycombinator.com&function='({ page }) => page.extract({
  stories: {
    selectorAll: '"'"'.athing'"'"',
    attr: {
      title: { selector: '"'"'.titleline > a'"'"', attr: '"'"'text'"'"' },
      href: { selector: '"'"'.titleline > a'"'"', attr: '"'"'href'"'"', type: '"'"'url'"'"' }
    }
  }
})'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://news.ycombinator.com" \
  -d "function=(%7B%20page%20%7D)%20%3D%3E%20page.extract(%7B%0A%20%20stories%3A%20%7B%0A%20%20%20%20selectorAll%3A%20'.athing'%2C%0A%20%20%20%20attr%3A%20%7B%0A%20%20%20%20%20%20title%3A%20%7B%20selector%3A%20'.titleline%20%3E%20a'%2C%20attr%3A%20'text'%20%7D%2C%0A%20%20%20%20%20%20href%3A%20%7B%20selector%3A%20'.titleline%20%3E%20a'%2C%20attr%3A%20'href'%2C%20type%3A%20'url'%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D)"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://news.ycombinator.com',
  ({ page }) => page.extract({
    stories: {
      selectorAll: '.athing',
      attr: {
        title: { selector: '.titleline > a', attr: 'text' },
        href: { selector: '.titleline > a', attr: 'href', type: 'url' }
      }
    }
  })
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://news.ycombinator.com",
    "function": '''({ page }) => page.extract({
  stories: {
    selectorAll: '.athing',
    attr: {
      title: { selector: '.titleline > a', attr: 'text' },
      href: { selector: '.titleline > a', attr: 'href', type: 'url' }
    }
  }
})'''
}

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://news.ycombinator.com",
  function: "({ page }) => page.extract({
  stories: {
    selectorAll: '.athing',
    attr: {
      title: { selector: '.titleline > a', attr: 'text' },
      href: { selector: '.titleline > a', attr: 'href', type: 'url' }
    }
  }
})"
}

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

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

$params = [
    "url" => "https://news.ycombinator.com",
    "function" => "({ page }) => page.extract({
  stories: {
    selectorAll: '.athing',
    attr: {
      title: { selector: '.titleline > a', attr: 'text' },
      href: { selector: '.titleline > a', attr: 'href', type: 'url' }
    }
  }
})"
];

$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)
    }
    fn := `({ page }) => page.extract({
  stories: {
    selectorAll: '.athing',
    attr: {
      title: { selector: '.titleline > a', attr: 'text' },
      href: { selector: '.titleline > a', attr: 'href', type: 'url' }
    }
  }
})`

    q := u.Query()
    q.Set("url", "https://news.ycombinator.com")
    q.Set("function", fn)
    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))
}

page.metadata()

It returns the same normalized metadata as meta for the target URL:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://example.com' URL with 'function' API parameter:

CLI Microlink API example

microlink https://example.com&function='async ({ page }) => (await page.metadata()).title'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://example.com" \
  -d "function=async%20(%7B%20page%20%7D)%20%3D%3E%20(await%20page.metadata()).title"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://example.com',
  async ({ page }) => (await page.metadata()).title
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://example.com",
    "function": "async ({ page }) => (await page.metadata()).title"
}

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://example.com",
  function: "async ({ page }) => (await page.metadata()).title"
}

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

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

$params = [
    "url" => "https://example.com",
    "function" => "async ({ page }) => (await page.metadata()).title"
];

$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://example.com")
    q.Set("function", "async ({ page }) => (await page.metadata()).title")
    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))
}

response

The puppeteer#response as result of the implicit page.goto. Only available when the function loads the page in a browser:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://edge-ping.vercel.app' URL with 'function' API parameter:

CLI Microlink API example

microlink https://edge-ping.vercel.app&function='({ page, response }) => response.status()'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://edge-ping.vercel.app" \
  -d "function=(%7B%20page%2C%20response%20%7D)%20%3D%3E%20response.status()"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://edge-ping.vercel.app',
  ({ page, response }) => response.status()
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://edge-ping.vercel.app",
    "function": "({ page, response }) => response.status()"
}

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://edge-ping.vercel.app",
  function: "({ page, response }) => response.status()"
}

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

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

$params = [
    "url" => "https://edge-ping.vercel.app",
    "function" => "({ page, response }) => response.status()"
];

$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://edge-ping.vercel.app")
    q.Set("function", "({ page, response }) => response.status()")
    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))
}

headers

The request headers used to fetch the target URL:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://example.com' URL with 'function' API parameter:

CLI Microlink API example

microlink https://example.com&function='({ headers }) => headers["user-agent"]'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://example.com" \
  -d "function=(%7B%20headers%20%7D)%20%3D%3E%20headers%5B%22user-agent%22%5D"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://example.com',
  ({ headers }) => headers["user-agent"]
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://example.com",
    "function": '''({ headers }) => headers["user-agent"]'''
}

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://example.com",
  function: '({ headers }) => headers["user-agent"]'
}

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

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

$params = [
    "url" => "https://example.com",
    "function" => '({ headers }) => headers["user-agent"]'
];

$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)
    }
    fn := `({ headers }) => headers["user-agent"]`

    q := u.Query()
    q.Set("url", "https://example.com")
    q.Set("function", fn)
    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))
}

Custom parameters

Any extra query parameter is forwarded to the function:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://example.com' URL with 'function' & 'greetings' API parameters:

CLI Microlink API example

microlink https://example.com&function='({ greetings }) => greetings'&greetings='hello world'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://example.com" \
  -d "function=(%7B%20greetings%20%7D)%20%3D%3E%20greetings" \
  -d "greetings=hello%20world"

JavaScript Microlink API example

import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://example.com',
  ({ greetings }) => greetings,
  {
    greetings: "hello world"
  }
)

Python Microlink API example

import requests

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

querystring = {
    "url": "https://example.com",
    "function": "({ greetings }) => greetings",
    "greetings": "hello world"
}

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://example.com",
  function: "({ greetings }) => greetings",
  greetings: "hello world"
}

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

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

$params = [
    "url" => "https://example.com",
    "function" => "({ greetings }) => greetings",
    "greetings" => "hello world"
];

$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://example.com")
    q.Set("function", "({ greetings }) => greetings")
    q.Set("greetings", "hello world")
    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))
}

Response

The result is wrapped under data.function:
{
  "status": "success",
  "data": {
    "function": {
      "isFulfilled": true,
      "value": "3.5.0",
      "profiling": {
        "phases": { "install": 0, "build": 120, "spawn": 45, "run": 890, "total": 1055 },
        "cpu": 234,
        "memory": { "total": 69996544, "used": 2359296, "heap": 4410880, "external": 1742574 },
        "size": 156
      },
      "logging": {}
    }
  }
}
FieldDescription
isFulfilledtrue when the function completed without errors
valueThe return value on success, or { name, message } on failure
profilingExecution metrics: phase durations (ms), CPU time (ms), a memory breakdown (bytes), and code size (bytes)
loggingCaptured console output from the function runtime
The profiling phases are:
PhaseDescription
installTime spent installing npm dependencies detected in your code
buildTime spent bundling the function code
spawnTime spent starting the isolated runtime process
runTime spent executing the function itself
totalWall-clock time from start to finish

Plan limits

The function parameter is available on both free and pro plans with different resource limits:
FreePro
Timeout15 secondsUp to 60 seconds
Memory64 MB128 MB
Code size1024 bytesUnlimited
Concurrency1 in-flight per IPUnlimited
Outgoing requestsSame-origin onlyUnrestricted
When a limit is exceeded, the function returns isFulfilled: false with a descriptive error instead of failing the entire request:
{
  "data": {
    "function": {
      "isFulfilled": false,
      "value": {
        "name": "TimeoutError",
        "message": "Function exceeded the 15s free plan timeout. Upgrade to pro for up to 60s."
      },
      "profiling": {},
      "logging": {}
    }
  }
}
The resource error types are:
ErrorTrigger
TimeoutErrorFunction wall-clock time exceeded the plan limit
CpuTimeErrorFunction CPU time exceeded the plan limit
MemoryErrorFunction memory usage exceeded the plan limit
CodeSizeErrorFunction code exceeds the 1024 bytes free plan limit
ConcurrencyErrorToo many concurrent function executions for the free plan
OutgoingRequestErrorFunction made a cross-origin network request on the free plan
Each error message is plan-aware and tells you the exact limit that was hit.

Compression

Since the function body can be large, it is compressed before being sent. The SDK handles this for you: microlink.function() compresses the code with brotli in Node.js and lz-string in browsers, so the call stays the same:
import createClient from 'microlink.io'

const microlink = createClient()

const { value } = await microlink.function(
  'https://microlink.io',
  ({ page }) => page.evaluate('jQuery.fn.jquery'),
  { scripts: 'https://code.jquery.com/jquery-3.5.0.min.js' }
)
The SDK picks the compressor for the runtime and prefixes the payload with its alias.
If you call the API yourself, compress the function body and send it prefixed with the compressor alias, e.g. lz#<compressed code>. The following compression algorithms are supported:
  • brotli (br)
  • gzip (gz)
  • lz-string (lz)
Read how to compress to know more.