Skip to content

function

Execute your own JavaScript remotely and get the value back. The function runs in a sandboxed Node.js runtime on Microlink's side; code that never touches page runs faster and cheaper:
const { value } = await microlink.function('https://example.com', () => 40 + 2)

console.log(value) // 42
run is an alias of function, so microlink.run(url, code) is the same call.

Browser access

page.content(), page.extract(), and page.metadata() fetch the page when you call them, with no browser. Other methods, such as page.title(), page.click(), or page.evaluate(), load the page in a browser once and hand you the full Puppeteer Page object:
const { value } = await microlink.function('https://example.com', async ({ page }) => {
  await page.waitForSelector('h1')
  return page.$eval('h1', el => el.textContent)
})
The function always receives url, the target of the request, without starting a browser. It also receives response (the Puppeteer response of that navigation) when the function loads the page in a browser, and headers. Any npm package can be required from inside the function; dependencies are detected, installed on the fly, and cached. See browser interaction and writing functions for the patterns.
The function can also be passed as a string of JavaScript source.

Result

It resolves to the function response object:
  • isFulfilled — true when the function completed without throwing.
  • value — the return value on success, or { name, message } describing the error on failure.
  • profiling — execution metrics: phase durations, CPU time, memory breakdown, and code size.
  • logging — the console output captured from the function runtime.
A throwing function does not reject the promise; check isFulfilled instead:
const result = await microlink.function('https://example.com', () => {
  throw new Error('boom')
})

console.log(result.isFulfilled) // false
console.log(result.value.name, result.value.message) // 'Error' 'boom'

Options

The third argument takes the shared options, so scripts, click, waitForSelector, or headers can prepare the page before your code runs:
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' }
)
Normalized metadata is skipped by default (meta: false) so the request only pays for the function; pass meta: true to get it back in the API response.

Custom parameters

Any option that isn't an API parameter is forwarded to the function as a named argument, which makes one function reusable across requests without changing its code:
const { value } = await microlink.function(
  'https://example.com',
  ({ page, selector }) => page.$eval(selector, el => el.textContent),
  { selector: 'h1' }
)

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 1024 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 to know more.

Limits

Free planPaid plan
Timeout15 seconds60 seconds
Memory64 MB128 MB
Code size1024 bytesUnlimited
Concurrency2 concurrent requestsUnlimited
Outgoing requestsSame-origin onlyUnrestricted
Exceeding a limit returns isFulfilled: false with a plan-aware error such as TimeoutError. See plan limits and troubleshooting.
See the function guide for writing patterns, package dependencies, and profiling.