Screenshot and PDF Generation in Astro with SnapAPI
April 2026 — 6 min read
Astro is the content-focused web framework that ships zero JavaScript to the browser by default, delivering exceptional performance for blogs, documentation sites, and marketing pages. Its API endpoints run server-side and can call external services without exposing secrets to the client. In this tutorial, you will add screenshot and PDF generation to an Astro site using SnapAPI, implementing the endpoint in an Astro API route, protecting it with a simple API key check, and returning binary responses efficiently.
Astro API Endpoints for Screenshot Generation
Astro API endpoints are defined as .ts or .js files with a GET or POST export in the src/pages/api/ directory. They run server-side when Astro is deployed in SSR mode using an adapter like the Node.js, Vercel, or Netlify adapter. Create a file at src/pages/api/screenshot.ts that exports a GET function. The function receives an APIContext object with access to the request URL, headers, and Astro's runtime environment. Extract the target URL from the query string, validate it, call SnapAPI, and return the binary response.
Enabling SSR in Astro
Astro is a static site generator by default. To use API endpoints that make server-side calls, you need to enable SSR by adding an adapter to your astro.config.mjs. For Node.js deployment, install the @astrojs/node adapter and set output: 'server' in your Astro config. For Vercel, use @astrojs/vercel. For Cloudflare Pages, use @astrojs/cloudflare. The adapter handles the server runtime so your API endpoint code is identical across deployment targets. Your SnapAPI key is stored in a .env file and accessed through Astro's import.meta.env object, which Astro guarantees is only available server-side when prefixed appropriately.
Environment Variables and Key Security
Astro distinguishes between public and private environment variables using naming conventions. Variables prefixed with PUBLIC_ are available both server-side and in client-side components. Variables without this prefix are only available in server-side code including API endpoints. Store your SnapAPI key as SNAPAPI_KEY in your .env file without the PUBLIC_ prefix. Access it in your API endpoint with import.meta.env.SNAPAPI_KEY. Astro's build process ensures this variable never appears in client JavaScript bundles, keeping your key secure even if your site's JavaScript is inspected.
Calling SnapAPI and Returning Binary Data
Inside your Astro API endpoint, use the native fetch API to call the SnapAPI screenshot endpoint. Pass the validated target URL as a query parameter and your SnapAPI key in the Authorization header. When the SnapAPI response arrives, check the status code and construct an Astro Response object from the response body buffer. Set the Content-Type header to match the SnapAPI response -- image/png for screenshots or application/pdf for PDFs. Astro's response handling correctly serializes the binary content without any additional encoding or base64 conversion.
Generating OG Images for Astro Blog Posts
One of the most compelling uses of SnapAPI in Astro sites is automated Open Graph image generation for blog posts. Create a dedicated Astro API endpoint at src/pages/api/og/[slug].ts that accepts a post slug, retrieves the post metadata from your content collection, renders an HTML template with the post title and metadata, passes the template URL to SnapAPI, and returns the PNG. Reference this endpoint URL in your blog post layout's Open Graph meta tags. When social platforms crawl your post URLs, they request the OG image endpoint, which generates a unique branded image for each post. Astro's content collections make it straightforward to retrieve post metadata from your Markdown frontmatter for use in the OG image template.
Caching Screenshot Responses
Screenshot generation adds one to five seconds of latency to each unique request. For public-facing endpoints, caching is essential. Set Cache-Control headers on your Astro screenshot API response to tell CDN edge nodes and browsers to cache the result. For Vercel deployment, Astro's Vercel adapter propagates Cache-Control headers to Vercel's Edge Network automatically. For Cloudflare Pages deployment, set Cache-Control headers with appropriate max-age values and Cloudflare's CDN caches the screenshot at the edge closest to each requester. Subsequent requests for the same screenshot URL are served from cache in milliseconds without hitting your Astro server or consuming SnapAPI credits.