Back to Blog

How to Build a Link Preview Feature Using a Screenshot API

Link previews — the visual cards that show a thumbnail, title, and description when you paste a URL — are one of the most engaging features you can add to a communication or content platform. Here's how to build one using a screenshot API.

What Makes a Great Link Preview

A good link preview includes:

Architecture Overview

The flow works like this:

  1. User pastes a URL in your app
  2. Your backend fetches the page metadata (title, description, favicon)
  3. Your backend requests a screenshot thumbnail from PxShot
  4. You cache both the metadata and screenshot
  5. The frontend renders a rich preview card

Backend: Fetch Metadata + Screenshot

async function generatePreview(url: string) {
  // Fetch page metadata
  const page = await fetch(url);
  const html = await page.text();
  const title = html.match(/<title>([^<]+)<\/title>/)?.[1] || url;
  const desc = html.match(/meta.*?description.*?content="([^"]+)"/)?.[1] || '';

  // Capture thumbnail via PxShot
  const screenshot = await fetch(
    `https://pxshot.dev/api/capture?url=${encodeURIComponent(url)}&width=800&height=450&format=webp&quality=75&key=${API_KEY}`
  );
  const imageBlob = await screenshot.arrayBuffer();

  // Store in your object storage (R2, S3, etc.)
  const imageUrl = await uploadToStorage(imageBlob, `previews/${hash(url)}.webp`);

  return { title, description: desc, imageUrl, domain: new URL(url).hostname };
}

Frontend: Render the Preview Card

function LinkPreview({ preview }) {
  return (
    <div className="link-preview">
      <img src={preview.imageUrl} alt={preview.title} />
      <div className="preview-info">
        <h4>{preview.title}</h4>
        <p>{preview.description}</p>
        <span className="domain">{preview.domain}</span>
      </div>
    </div>
  );
}

Caching Strategy

Link previews should be cached aggressively since pages rarely change:

Production Tips

With PxShot's free tier, you can build and test link previews without any upfront cost. Get your API key.

Ready to try PxShot?

Capture any webpage as an image with a single API call. Free tier included.

Get Started Free