# function

## Table of Contents

- [Browser access](#browser-access)
- [Result](#result)
- [Options](#options)
  - [Custom parameters](#custom-parameters)
- [Compression](#compression)
- [Limits](#limits)

---

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:

```js
const{value}=awaitmicrolink.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](https://pptr.dev/api/puppeteer.page) object:

```js
const{value}=awaitmicrolink.function(

'https://example.com',

async({page})=>{

awaitpage.waitForSelector('h1')

returnpage.$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](https://microlink.io/docs/guides/function/browser-interaction) and [writing functions](https://microlink.io/docs/guides/function/writing-functions) for the patterns.

The function can also be passed as a string of JavaScript source.

## Result

It resolves to the [function response](https://microlink.io/docs/api/parameters/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:

```js
constresult=awaitmicrolink.function('https://example.com',()=>{

thrownewError('boom')

})

console.log(result.isFulfilled)// false

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

## Options

The third argument takes the [shared options](https://microlink.io/docs/sdk/getting-started/options), so [scripts](https://microlink.io/docs/api/parameters/scripts), [click](https://microlink.io/docs/api/parameters/click), [waitForSelector](https://microlink.io/docs/api/parameters/waitForSelector), or [headers](https://microlink.io/docs/api/parameters/headers) can prepare the page before your code runs:

```js
const{value}=awaitmicrolink.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:

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

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

Exceeding a limit returns `isFulfilled: false` with a plan-aware error such as `TimeoutError`. See [plan limits](https://microlink.io/docs/api/parameters/function#plan-limits) and [troubleshooting](https://microlink.io/docs/guides/function/troubleshooting).

See the [function guide](https://microlink.io/docs/guides/function) for writing patterns, package dependencies, and profiling.