Skip to content

Function

The function parameter lets you run any JavaScript remotely with full Puppeteer access, npm packages, and zero infrastructure to manage.
Send a function, get the result back. No Lambda bundle, no browser fleet, no server. Microlink handles the headless browser, installs dependencies on-the-fly, and executes your code in a sandboxed runtime.

Install

The library lets you write normal JavaScript functions and run them remotely. It handles serialization, compression, and the API call for you:
npm install @microlink/function

Your first function

Pass a JavaScript function and a target URL. The library sends it to the Microlink API and returns the result:
const microlink = require('@microlink/function')

const fn = microlink(() => 40 + 2)
const result = await fn('https://example.com')

console.log(result.isFulfilled) // true
console.log(result.value)       // 42
The function runs remotely. The result includes the returned value at result.value and execution metrics at result.profiling.
When your function references page, Microlink starts a headless browser and gives you full Puppeteer access:

The following examples show how to use the Microlink API with CLI, cURL, JavaScript, Python, Ruby, PHP & Golang, targeting 'https://developer.mozilla.org/en-US/docs/Web/API/Document/title' URL with 'function' & 'meta' API parameters:

CLI Microlink API example

microlink https://developer.mozilla.org/en-US/docs/Web/API/Document/title&function='({ page }) => page.title()'

cURL Microlink API example

curl -G "https://api.microlink.io" \
  -d "url=https://developer.mozilla.org/en-US/docs/Web/API/Document/title" \
  -d "function=(%7B%20page%20%7D)%20%3D%3E%20page.title()" \
  -d "meta=false"

JavaScript Microlink API example

import mql from '@microlink/mql'

const { data } = await mql('https://developer.mozilla.org/en-US/docs/Web/API/Document/title', {
  function: "({ page }) => page.title()",
  meta: false
})

Python Microlink API example

import requests

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

querystring = {
    "url": "https://developer.mozilla.org/en-US/docs/Web/API/Document/title",
    "function": "({ page }) => page.title()",
    "meta": "false"
}

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://developer.mozilla.org/en-US/docs/Web/API/Document/title",
  function: "({ page }) => page.title()",
  meta: "false"
}

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://developer.mozilla.org/en-US/docs/Web/API/Document/title",
    "function" => "({ page }) => page.title()",
    "meta" => "false"
];

$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://developer.mozilla.org/en-US/docs/Web/API/Document/title")
    q.Set("function", "({ page }) => page.title()")
    q.Set("meta", "false")
    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))
}

Choose the lightest tool

Use function when the built-in parameters stop being expressive enough, not as the default for every workflow.
If you needBest optionWhy
Simple field extraction from the DOMdataDeclarative rules are shorter, easier to maintain, and easier to reuse
Inject CSS or JavaScript before another workflowstyles, modules, or scriptsLighter than full browser automation
Click, wait, compute, reshape, or orchestrate custom logicfunctionYou get Puppeteer access plus any npm package
For simpler extraction flows, start with data extraction and only escalate to function when the declarative approach becomes limiting.

What's next