AgentSkillsCN

microlink-api

通过 Microlink API 和 MQL 进行元数据提取、截图生成、PDF 生成、网页抓取以及浏览器自动化操作。当用户提及 Microlink、MQL、@microlink/mql、使用 CSS 选择器进行网页抓取、截取网站截图、从 URL 生成 PDF、从链接中提取元数据,或嵌入链接预览时,可使用此功能。

SKILL.md
--- frontmatter
name: microlink-api
description: Interact with Microlink API and MQL for metadata extraction, screenshots, PDFs, web scraping, and browser automation. Use when the user mentions Microlink, MQL, @microlink/mql, web scraping with CSS selectors, taking website screenshots, generating PDFs from URLs, extracting metadata from links, or embedding link previews.

Microlink API

Microlink API automates browser actions via HTTP GET. Given a URL, it returns structured data (metadata, screenshots, PDFs, scraped content).

Endpoints

  • Free: https://api.microlink.io — unauthenticated, 50 reqs/day
  • Pro: https://pro.microlink.io — authenticated via x-api-key header, starts at 14,000 reqs/month

Query parameters support both camelCase and snake_case.

Authentication

Pass API key as x-api-key request header. Verify with x-pricing-plan: pro in response headers.

Never expose API keys in client-side code. Use proxy or edge-proxy for frontend usage.

Response Format (JSend)

json
{
  "status": "success",
  "data": { "title": "...", "description": "...", "image": { "url": "...", "width": 1280, "height": 720, "type": "jpg", "size": 120116, "size_pretty": "120 kB" }, "url": "..." },
  "message": "optional error message",
  "more": "optional docs link"
}

Status values: 'success' (2xx), 'fail' (4xx), 'error' (5xx).

Default Data Fields

Extracted automatically from any URL: title, description, author, publisher, date (ISO 8601), lang (ISO 639-1), url, image, video, logo.

Media fields include: width, height, type, size, size_pretty. Playable media adds duration, duration_pretty.

Additional HTTP info: statusCode, headers, redirects.

Core Parameters

ParameterTypeDefaultProDescription
urlstringrequiredTarget URL (must include protocol)
screenshotbooleanfalseGenerate screenshot
pdfbooleanfalseGenerate PDF
videobooleanfalseDetect video sources
audiobooleanfalseDetect audio sources
metaboolean/objecttrueMetadata extraction (disable with false to speed up)
dataobjectCustom scraping rules (CSS selectors)
embedstringReturn specific field as response body (e.g., 'screenshot.url')
filterstringComma-separated fields to pick from response
prerenderboolean/string'auto'Headless browser: true, false, or 'auto'
iframeboolean/objectfalseoEmbed detection
insightsboolean/objectfalseLighthouse + Wappalyzer tech detection
palettebooleanfalseColor palette extraction
functionstringExecute JS with Puppeteer page access

Screenshot Options

ParameterTypeDefaultDescription
screenshot.elementstringCSS selector to capture specific element
screenshot.fullPagebooleanfalseFull scrollable page
screenshot.typestring'png''jpeg' or 'png'
screenshot.omitBackgroundbooleanfalseTransparent background
screenshot.overlayobjectBrowser chrome overlay (browser: 'light'/'dark', background: CSS gradient/color/image URL)
screenshot.codeSchemestring'atom-dark'Syntax highlighting theme

PDF Options

ParameterTypeDefaultDescription
pdf.formatstringPage format (e.g., 'A4', 'Letter')
pdf.landscapebooleanfalseLandscape orientation
pdf.scalenumberScale factor
pdf.marginstring/objectPage margins
pdf.pageRangesstringPages to print (e.g., '1-3')

Browser Control

ParameterTypeDefaultDescription
devicestring'macbook pro 13'Device emulation (e.g., 'iPad', 'iPhone X')
viewportobject{ width, height, deviceScaleFactor, isMobile }
colorSchemestring'no-preference''light' or 'dark'
javascriptbooleantrueEnable/disable JS execution
animationsbooleanfalseEnable CSS animations
mediaTypestring'screen''screen' or 'print'
clickstring/string[]CSS selector(s) to click
scrollstringCSS selector to scroll to
scriptsstring/string[]Inject <script> (inline code or URL)
modulesstring/string[]Inject <script type="module">
stylesstring/string[]Inject <style> (inline CSS or URL)
waitUntilstring/string[]'auto''load', 'domcontentloaded', 'networkidle0', 'networkidle2'
waitForSelectorstringWait for CSS selector
waitForTimeoutstring/numberWait duration (e.g., '3s', 3000)
timeoutstring/number28sMax request lifecycle

Cache Control

ParameterTypeDefaultProDescription
ttlstring/number'24h'yesCache lifetime (1m to 31d). Formats: '1d', '24h', 86400000
staleTtlstring/number/booleanfalseyesServe stale while revalidating. staleTtl=0 always revalidates
forcebooleanfalseBypass cache, get fresh copy

Cache headers: x-cache-status (HIT/MISS/BYPASS), x-cache-ttl.

Pro-Only Parameters

headers, proxy, ttl, staleTtl, filename require a Pro plan.

  • headers: Custom HTTP headers. Use x-api-header-* prefix for sensitive headers
  • proxy: Custom proxy URL or automatic proxy resolution for anti-bot bypass
  • filename: Custom name for generated screenshot/PDF assets

Embed Pattern

Use embed to serve assets directly (no JSON wrapper):

html
<img src="https://api.microlink.io/?url=https://example.com&screenshot=true&meta=false&embed=screenshot.url">

Common embed fields: screenshot.url, pdf.url, image.url, logo.url, video.url.

function Parameter

Execute custom JS with Puppeteer access. Receives { page, html, response } plus any query params:

js
const { data } = await mql('https://example.com', {
  function: '({ page }) => page.evaluate("document.title")',
  meta: false
})
console.log(data.function)

Supports compression (lz#, gz#, br# prefixes) and NPM packages: cheerio, lodash, got, jsdom, @mozilla/readability, ioredis, @aws-sdk/client-s3, youtube-dl-exec, and others.

Error Codes

CodeCause
EAUTHInvalid API key
ERATEDaily rate limit reached
EINVALURLInvalid URL format
EBRWSRTIMEOUTBrowser navigation timeout
EFATALGeneric resolution failure
EPROAuth request sent to free endpoint
ETIMEOUTRequest timeout exceeded

For complete details, see api-reference.md.


MQL (Microlink Query Language)

MQL is the official Node.js/browser HTTP client for Microlink API (@microlink/mql).

Installation

bash
npm install @microlink/mql --save

Environments

  • Node.js: const mql = require('@microlink/mql')
  • Edge/WinterCG: import mql from '@microlink/mql/lightweight'
  • Browser: import mql from 'https://esm.sh/@microlink/mql'

Basic Usage

js
const mql = require('@microlink/mql')
const { status, data, response } = await mql('https://example.com')

API Signature

code
mql(url, [options], [httpOptions])
  • url: Target URL (required)
  • options: Any Microlink API parameter + apiKey, cache, retry
  • httpOptions: Passed to underlying HTTP client (got/ky)

Additional methods: mql.stream(), mql.buffer().

Error Handling

js
const { MicrolinkError } = mql
try {
  const { data } = await mql('https://example.com', { screenshot: true })
} catch (error) {
  // error.status, error.code, error.message, error.statusCode
}

Client-Side Caching (Node.js only)

js
const cache = new Map()
const { data } = await mql('https://example.com', { cache })
// data.response.fromCache → true on second call

Custom Data Extraction (Scraping)

Define rules with three primitives: selector (CSS), attr (what to extract), type (validation).

Single Field

js
const { data } = await mql('https://kikobeats.com', {
  data: {
    avatar: { selector: '#avatar', type: 'image', attr: 'src' }
  }
})

Multiple Fields

js
const { data } = await mql('https://news.ycombinator.com', {
  data: {
    headline: { selector: '.titleline > a', attr: 'text' },
    link: { selector: '.titleline > a', attr: 'href', type: 'url' }
  }
})

Collections (selectorAll)

js
const { data } = await mql('https://news.ycombinator.com', {
  data: {
    stories: { selectorAll: '.titleline > a', attr: 'text' }
  }
})

Fallback Rules

js
data: {
  title: [
    { selector: 'meta[property="og:title"]', attr: 'content' },
    { selector: 'title', attr: 'text' },
    { selector: 'h1', attr: 'text' }
  ]
}

Nested Rules

js
data: {
  stats: {
    selector: '.profile',
    attr: {
      followers: { selector: '.followers', type: 'number' },
      stars: { selector: '.stars', type: 'number' }
    }
  }
}

Whole-Page Serialization

js
data: {
  content: { attr: 'markdown' }  // omit selector for full page
}

attr Values

Standard HTML attributes plus: 'html', 'outerHTML', 'text', 'markdown', 'val'.

type Values

'auto', 'string', 'number', 'boolean', 'date', 'image', 'url', 'audio', 'video', 'email', 'author', 'publisher', 'title', 'description', 'lang', 'logo', 'object', 'regexp', 'ip'.

evaluate

Execute JS in browser context instead of CSS selectors:

js
data: {
  version: { evaluate: 'window.next.version', type: 'string' }
}

CLI

bash
npm install @microlink/cli --global
microlink <url> [flags]

Flags: --api-key, --colors, --copy, --pretty.

Additional Resources