# 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 mql from '@microlink/mql'

const { data } = await mql('https://microlink.io', {
  function: '({ 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))
}
```

    import mql from '@microlink/mql'

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

Click to run the code and see the API response

The function will receive any query parameter provided, plus:

- `html`: The target [url](https://microlink.io/docs/api/parameters/url) HTML markup.
- `page`: The [](https://pptr.dev/api/puppeteer.page)


  puppeteer#page



  to interact with the headless browser.
- `response`: The [](https://pptr.dev/api/puppeteer.httpresponse)


  puppeteer#response



  as result of the implicit [](https://pptr.dev/api/puppeteer.page.goto)


  page.goto



  .

## [Compression](https://microlink.io/docs/api/parameters/function#compression)

Since the function body can be large, you can compress it:

    const { compressToURI } = require('lz-ts')
    const mql = require('@microlink/mql')

    const code = ({ page }) => page.evaluate('jQuery.fn.jquery')

    const { status, data } = await mql('https://microlink.io', {
      function: `lz#${compressToURI(code.toString())}`,
      meta: false,
      scripts: 'https://code.jquery.com/jquery-3.5.0.min.js'
    })

    mql.render(data.function)

You should to prefix the compressed data with the compressor alias.

The following compression algorithms are supported:

- brotli (br)
- gzip (gz)
- lz-string (lz)

Read [how to compress](https://microlink.io/blog/compress) to know more.

## [NPM packages](https://microlink.io/docs/api/parameters/function#npm-packages)

Require NPM packages on runtime is supported.

    const mql = require('@microlink/mql')

    const code = ({ statusCode, response }) => {
      const { result } = require('lodash')
      return result(response, statusCode ? 'status' : 'statusText')
    }

    const ping = (url, props) =>
      mql(url, { function: code.toString(), meta: false, ...props }).then(
        ({ data }) => data.function
      )

    // try passing `statusCode: false`
    await ping('https://example.com', { statusCode: true })

The list of allowed NPM packages are:

- [](https://npm.im/@aws-sdk/client-s3)


  @aws-sdk/client-s3


- [](https://npm.im/@mozilla/readability)


  @mozilla/readability


- [](https://npm.im/cheerio)


  cheerio


- [](https://npm.im/extract-email-address)


  extract-email-address


- [](https://npm.im/got)


  got


- [](https://npm.im/ioredis)


  ioredis


- [](https://npm.im/jsdom)


  jsdom


- [](https://npm.im/lodash)


  lodash


- [](https://npm.im/metascraper)


  metascraper


- [](https://npm.im/p-reflect)


  p-reflect


- [](https://npm.im/p-retry)


  p-retry


- [](https://npm.im/p-timeout)


  p-timeout


- [](https://nodejs.org/api/path.html)


  path


- [](https://nodejs.org/api/url.html)


  url


- [](https://npm.im/youtube-dl-exec)


  youtube-dl-exec



If you want to request a npm package, please feel free to [](mailto:hello@microlink.io)

reach us

.