# Link previews in Cloudflare Workers

> Extract Open Graph and URL metadata in a Cloudflare Worker with linkpeek: fetch-native, no Node APIs, with cache headers and safe defaults.

linkpeek uses platform `fetch`, Web Streams, `TextDecoder`, and `AbortController`. It needs no Node HTTP client or DOM implementation.

## Create a Worker endpoint

```bash
npm install linkpeek
```

```ts
import { preview } from "linkpeek";

export default {
	async fetch(request: Request): Promise<Response> {
		const url = new URL(request.url).searchParams.get("url");
		if (!url) return Response.json({ error: "Missing url" }, { status: 400 });

		try {
			const result = await preview(url);
			return Response.json(result, {
				headers: {
					"Cache-Control": result.statusCode >= 200 && result.statusCode < 300
						? "public, max-age=3600"
						: "no-store",
				},
			});
		} catch {
			return Response.json({ error: "Preview unavailable" }, { status: 422 });
		}
	},
};
```

## Caching

The `Cache-Control` header lets browsers and downstream caches reuse a result. This Worker doesn't use the Cloudflare Cache API, so every request that reaches the handler still fetches the target. If that matters, add a shared cache keyed on a stable URL, store only 2xx previews, and rate-limit public callers.

## Runtime limitations

linkpeek validates literal addresses and each redirect target. The Worker runtime resolves hostnames and establishes connections. Keep private-IP access disabled and use platform egress controls when hostile public input is in scope.

Related: [Security model](https://linkpeek.js.org/security/) · [API reference](https://linkpeek.js.org/api/) · [Next.js](https://linkpeek.js.org/integrations/nextjs/) · [Complete example on GitHub](https://github.com/thegruber/linkpeek/tree/main/examples/cloudflare-worker)
