Skip to content

Function: Writing functions

Your function runs remotely in a Node.js sandbox. The simplest function is just plain JavaScript — no browser, no page, no Puppeteer:
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
When your function does not reference page, no browser is started. This makes execution faster and cheaper.

Return any value

Functions can return strings, numbers, booleans, arrays, or plain objects:
const microlink = require('@microlink/function')

const fn = microlink(() => ({
  greeting: 'Hello',
  items: [1, 2, 3],
  nested: { works: true }
}))

const result = await fn('https://example.com')
console.log(result.value)
// { greeting: 'Hello', items: [1, 2, 3], nested: { works: true } }
The return value is always available at result.value. If the function throws, result.isFulfilled is false and result.value contains the error details instead.

Custom parameters

Any extra parameter you include in the request is forwarded to the function:
const microlink = require('@microlink/function')

const greet = ({ name, greeting }) => `${greeting}, ${name}!`
const fn = microlink(greet)

const result = await fn('https://example.com', {
  name: 'Kiko',
  greeting: 'Hello'
})

console.log(result.value) // 'Hello, Kiko!'
This is the simplest way to make one function reusable across different requests without changing the function code.

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' & 'meta' 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" \
  -d "meta=false"

JavaScript Microlink API example

import mql from '@microlink/mql'

const { data } = await mql('https://example.com', {
  function: "({ greetings }) => greetings",
  greetings: "hello world",
  meta: false
})

Python Microlink API example

import requests

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

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

Using npm packages

You can require() any npm package inside your function. Dependencies are detected automatically and installed on-the-fly:
const microlink = require('@microlink/function')

const fn = microlink(() => {
  const { kebabCase } = require('lodash')
  return kebabCase('Hello World')
})

const result = await fn('https://example.com')
console.log(result.value) // 'hello-world'
When your function contains a require() call, the runtime:
  1. Parses your code to detect all dependency names.
  2. Installs them into an isolated sandbox during the install phase.
  3. Bundles everything during the build phase.
  4. Caches the result so subsequent runs with the same dependencies skip installation.
You can see how long each step takes in result.profiling.phases. A high install value on the first run is normal — it drops to zero once cached.

Pin a version

Append the version to the package name:
const cheerio = require('[email protected]')
When no version is specified, the latest version is installed.

Security restrictions

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. If a package tries to use a restricted capability, the function will return an error:
{
  "isFulfilled": false,
  "value": {
    "name": "Error",
    "code": "ERR_ACCESS_DENIED",
    "permission": "ChildProcess",
    "message": "Access to this API has been restricted."
  }
}

When to add page

When your function references page, Microlink starts a headless browser and navigates to the URL before calling your function. This gives you full Puppeteer access but takes more time:
const microlink = require('@microlink/function')

const getTitle = ({ page }) => page.title()
const fn = microlink(getTitle)

const result = await fn('https://example.com')
console.log(result.value) // 'Example Domain'
If you only need to compute a value or call an external API, skip page entirely — your function will run faster.
See Browser interaction for Puppeteer helpers, execution contexts, and browser automation.

See also