# function

## Table of Contents

- [CLI Microlink API example](#cli-microlink-api-example)
  - [cURL Microlink API example](#curl-microlink-api-example)
  - [JavaScript Microlink API example](#javascript-microlink-api-example)
  - [Python Microlink API example](#python-microlink-api-example)
  - [Ruby Microlink API example](#ruby-microlink-api-example)
  - [PHP Microlink API example](#php-microlink-api-example)
  - [Golang Microlink API example](#golang-microlink-api-example)
  - [CLI Microlink API example](#cli-microlink-api-example-1)
  - [cURL Microlink API example](#curl-microlink-api-example-1)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-1)
  - [Python Microlink API example](#python-microlink-api-example-1)
  - [Ruby Microlink API example](#ruby-microlink-api-example-1)
  - [PHP Microlink API example](#php-microlink-api-example-1)
  - [Golang Microlink API example](#golang-microlink-api-example-1)
- [Request](#request)
  - [url](#url)
  - [CLI Microlink API example](#cli-microlink-api-example-2)
  - [cURL Microlink API example](#curl-microlink-api-example-2)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-2)
  - [Python Microlink API example](#python-microlink-api-example-2)
  - [Ruby Microlink API example](#ruby-microlink-api-example-2)
  - [PHP Microlink API example](#php-microlink-api-example-2)
  - [Golang Microlink API example](#golang-microlink-api-example-2)
  - [page](#page)
  - [CLI Microlink API example](#cli-microlink-api-example-3)
  - [cURL Microlink API example](#curl-microlink-api-example-3)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-3)
  - [Python Microlink API example](#python-microlink-api-example-3)
  - [Ruby Microlink API example](#ruby-microlink-api-example-3)
  - [PHP Microlink API example](#php-microlink-api-example-3)
  - [Golang Microlink API example](#golang-microlink-api-example-3)
    - [page.extract()](#pageextract)
  - [CLI Microlink API example](#cli-microlink-api-example-4)
  - [cURL Microlink API example](#curl-microlink-api-example-4)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-4)
  - [Python Microlink API example](#python-microlink-api-example-4)
  - [Ruby Microlink API example](#ruby-microlink-api-example-4)
  - [PHP Microlink API example](#php-microlink-api-example-4)
  - [Golang Microlink API example](#golang-microlink-api-example-4)
    - [page.metadata()](#pagemetadata)
  - [CLI Microlink API example](#cli-microlink-api-example-5)
  - [cURL Microlink API example](#curl-microlink-api-example-5)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-5)
  - [Python Microlink API example](#python-microlink-api-example-5)
  - [Ruby Microlink API example](#ruby-microlink-api-example-5)
  - [PHP Microlink API example](#php-microlink-api-example-5)
  - [Golang Microlink API example](#golang-microlink-api-example-5)
  - [response](#response)
  - [CLI Microlink API example](#cli-microlink-api-example-6)
  - [cURL Microlink API example](#curl-microlink-api-example-6)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-6)
  - [Python Microlink API example](#python-microlink-api-example-6)
  - [Ruby Microlink API example](#ruby-microlink-api-example-6)
  - [PHP Microlink API example](#php-microlink-api-example-6)
  - [Golang Microlink API example](#golang-microlink-api-example-6)
  - [headers](#headers)
  - [CLI Microlink API example](#cli-microlink-api-example-7)
  - [cURL Microlink API example](#curl-microlink-api-example-7)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-7)
  - [Python Microlink API example](#python-microlink-api-example-7)
  - [Ruby Microlink API example](#ruby-microlink-api-example-7)
  - [PHP Microlink API example](#php-microlink-api-example-7)
  - [Golang Microlink API example](#golang-microlink-api-example-7)
  - [Custom parameters](#custom-parameters)
  - [CLI Microlink API example](#cli-microlink-api-example-8)
  - [cURL Microlink API example](#curl-microlink-api-example-8)
  - [JavaScript Microlink API example](#javascript-microlink-api-example-8)
  - [Python Microlink API example](#python-microlink-api-example-8)
  - [Ruby Microlink API example](#ruby-microlink-api-example-8)
  - [PHP Microlink API example](#php-microlink-api-example-8)
  - [Golang Microlink API example](#golang-microlink-api-example-8)
- [Response](#response)
- [Plan limits](#plan-limits)
- [Compression](#compression)

---

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

```bash
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

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://microlink.io',

({page})=>page.evaluate("jQuery.fn.jquery"),

{

scripts:[

"https://code.jquery.com/jquery-3.5.0.min.js"

]

}

)
```

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

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

### cURL Microlink API example

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://example.com',

()=>{

constcheerio=require('cheerio')

const$=cheerio.load('<h1>Hello world</h1>')

return$('h1').text()

}

)
```

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

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

### cURL Microlink API example

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://example.com',

({url})=>newURL("/robots.txt",url).href

)
```

It is the URL you requested, not the one a redirect settles on. A function asked for on its own skips the [ping](https://microlink.io/docs/api/parameters/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:

```js
// 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](https://pptr.dev/api/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

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

### cURL Microlink API example

```bash
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

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

const microlink = createClient()

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

### Python Microlink API example

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://example.com',

({page})=>page.title()

)
```

The [page API](https://pptr.dev/api/puppeteer.page) 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](https://microlink.io/docs/api/parameters/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

```bash
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

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.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'}

}

}

})

)
```

#### page.metadata()

It returns the same normalized metadata as [meta](https://microlink.io/docs/api/parameters/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

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

### cURL Microlink API example

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://example.com',

async({page})=>(awaitpage.metadata()).title

)
```

### response

The [puppeteer#response](https://pptr.dev/api/puppeteer.httpresponse) as result of the implicit [page.goto](https://pptr.dev/api/puppeteer.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

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

### cURL Microlink API example

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://edge-ping.vercel.app',

({page,response})=>response.status()

)
```

### 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

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

### cURL Microlink API example

```bash
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

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

const microlink = createClient()

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

### Python Microlink API example

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://example.com',

({headers})=>headers["user-agent"]

)
```

### 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

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

### cURL Microlink API example

```bash
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

```javascript
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

```python
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

```ruby
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
<?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))
}
```

```javascript
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

const{value}=awaitmicrolink.function(

'https://example.com',

({greetings})=>greetings,

{

greetings:"hello world"

}

)
```

## Response

The result is wrapped under `data.function`:

```json
{

"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":{}

}

}

}
```

| Field         | Description                                                                                               |
| ------------- | --------------------------------------------------------------------------------------------------------- |
| `isFulfilled` | `true` when the function completed without errors                                                         |
| `value`       | The return value on success, or `{ name, message }` on failure                                            |
| `profiling`   | Execution metrics: phase durations (ms), CPU time (ms), a memory breakdown (bytes), and code size (bytes) |
| `logging`     | Captured console output from the function runtime                                                         |

The profiling phases are:

| Phase     | Description                                                  |
| --------- | ------------------------------------------------------------ |
| `install` | Time spent installing npm dependencies detected in your code |
| `build`   | Time spent bundling the function code                        |
| `spawn`   | Time spent starting the isolated runtime process             |
| `run`     | Time spent executing the function itself                     |
| `total`   | Wall-clock time from start to finish                         |

## Plan limits

The function parameter is available on both free and pro plans with different resource limits:

|                   | Free plan             | Paid plan    |
| ----------------- | --------------------- | ------------ |
| Timeout           | 15 seconds            | 60 seconds   |
| Memory            | 64 MB                 | 128 MB       |
| Code size         | 5120 bytes            | Unlimited    |
| Concurrency       | 2 concurrent requests | Unlimited    |
| Outgoing requests | Same-origin only      | Unrestricted |

When a limit is exceeded, the function returns `isFulfilled: false` with a descriptive error instead of failing the entire request:

```json
{

"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:

| Error                  | Trigger                                                                 |
| ---------------------- | ----------------------------------------------------------------------- |
| `TimeoutError`         | Function wall-clock time exceeded the plan limit                        |
| `CpuTimeError`         | Function CPU time exceeded the plan limit                               |
| `MemoryError`          | Function memory usage exceeded the plan limit                           |
| `CodeSizeError`        | Function code exceeds the 5120 bytes free plan limit                    |
| `ConcurrencyError`     | The same function is already running against the same URL without a key |
| `OutgoingRequestError` | Function 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

Function bodies travel in the URL, so large ones are compressed before they are sent. The SDK does it for you: `microlink.function()` compresses the code with brotli when the runtime supports it, falls back to gzip otherwise, and prefixes the payload with `br#` or `gz#`. The call stays the same.

If you call the API directly, compress the function body and prefix the payload with the alias of the algorithm:

- `br#` for brotli
- `gz#` for gzip
- `lz#` for lz-string

The free plan's 5120 bytes code size limit applies to the code once decompressed, so compression keeps the URL short but does not raise the limit. Read [how to compress](https://microlink.io/blog/compress) to know more.