Building a Link Previewer in 5 Lines of Python

Python

Tutorial

API

Ever wonder how Slack, WhatsApp, or Telegram generate those rich link previews? They extract Open Graph metadata from the target URL. Here's how to do it yourself.

The Code

import requests

url = "https://news.ycombinator.com"
headers = {"X-Api-Key": "demo"}
resp = requests.get(
  "https://snapapi.loca.lt/api/metadata",
  params={"url": url},
  headers=headers
)
print(resp.json()["data"])
# {'title': 'Hacker News', 'favicon': 'y18.svg', 'url': 'https://news.ycombinator.com/'}

What You Get

The API returns title, description, Open Graph image, favicon, canonical URL — everything you need for a rich link preview.

Going Further: Add Screenshots

# Save a screenshot
img = requests.get(
  "https://snapapi.loca.lt/api/screenshot",
  params={"url": url, "width": 1200, "height": 630},
  headers=headers
)
with open("preview.png", "wb") as f:
  f.write(img.content)

That's it. You now have a full link preview system in ~8 lines of Python. SnapAPI handles the browser automation.

Get your free API key →