OpenLink

Preview API

Extract Open Graph metadata from any URL.

The Preview API extracts rich metadata from any URL including Open Graph tags, Twitter cards, and standard HTML meta tags.

Endpoint

GET /api/preview

Parameters

ParameterTypeRequiredDescription
urlstringYesThe URL to extract metadata from
ttlstringNoCache duration (e.g., 1h, 1d). Default: 1h
freshbooleanNoBypass cache and fetch fresh data. Default: false

TTL Format

The ttl parameter accepts duration strings:

FormatExampleDescription
Seconds30s30 seconds
Minutes5m5 minutes
Hours1h1 hour
Days7d7 days

Valid range: 1m to 31d

Response

Success Response

{
  "data": {
    "title": "GitHub",
    "description": "Where the world builds software",
    "image": "https://github.githubassets.com/images/og-image.png",
    "favicon": "https://github.com/favicon.ico",
    "siteName": "GitHub",
    "url": "https://github.com",
    "requestedAt": "2024-01-15T10:30:00.000Z"
  },
  "meta": {
    "cache": {
      "status": "HIT",
      "age": 3600000,
      "ageHuman": "1h",
      "ttl": 3600000,
      "ttlHuman": "1h",
      "expiresIn": 0,
      "expiresInHuman": "0s"
    },
    "timing": {
      "requestedAt": "2024-01-15T10:30:00.000Z"
    }
  }
}

Cache Status Values

StatusDescription
HITServed from cache, data is fresh
MISSCache empty, fetched from origin
STALEServed stale data while revalidating in background
BYPASSCache bypassed with fresh=true

Response Headers

HeaderDescription
X-Cache-StatusCurrent cache status
X-Cache-AgeAge of cached data in seconds
X-Cache-TTLTTL of cached data in seconds
Cache-ControlBrowser caching directive

Examples

Basic Request

curl "https://openlink.sh/api/preview?url=https://github.com"

With Custom TTL

Cache the result for 24 hours:

curl "https://openlink.sh/api/preview?url=https://github.com&ttl=24h"

Force Fresh Fetch

Bypass cache and get fresh data:

curl "https://openlink.sh/api/preview?url=https://github.com&fresh=true"

JavaScript

async function getPreview(url: string) {
  const res = await fetch(
    `https://openlink.sh/api/preview?url=${encodeURIComponent(url)}`
  )
  const { data } = await res.json()
  return data
}

const preview = await getPreview("https://github.com")
console.log(preview.title) // "GitHub"

React Hook

import { useState, useEffect } from "react"

function usePreview(url: string) {
  const [data, setData] = useState(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    setLoading(true)
    fetch(`/api/preview?url=${encodeURIComponent(url)}`)
      .then(res => res.json())
      .then(result => {
        setData(result.data)
        setLoading(false)
      })
  }, [url])

  return { data, loading }
}

Error Responses

Missing URL

{
  "error": "Missing required parameter: url",
  "code": "MISSING_URL",
  "docs": "https://openlink.sh/docs#preview"
}

Invalid URL

{
  "error": "Invalid URL format",
  "code": "INVALID_URL",
  "docs": "https://openlink.sh/docs#preview"
}

Invalid TTL

{
  "error": "TTL must be between 1m and 31d",
  "code": "INVALID_TTL",
  "docs": "https://openlink.sh/docs#caching"
}

Fetch Error

{
  "error": "Failed to fetch preview",
  "code": "FETCH_ERROR",
  "message": "Connection timeout",
  "docs": "https://openlink.sh/docs#errors"
}

On this page