Capturing Dark Mode Screenshots via API: The Real Setup
Learn how to capture dark mode screenshots via API using prefers-color-scheme emulation, cookies, and practical examples for OG images and testing.
Dark mode isn't a nice-to-have anymore. Most modern web apps ship both a light and dark theme, and if your visual monitoring, OG image generation, or PDF exports only ever capture the light version, you're missing half the picture — literally. Here's how to actually get dark mode screenshots through an API, without hacking together a headless browser script from scratch.
Why Dark Mode Screenshots Are Harder Than They Look
The naive approach is "just add a dark class to the body and screenshot it." That works for maybe 20% of sites. The rest use one of these patterns, each of which needs a different trick:
- OS-level preference detection — sites that use
prefers-color-scheme: darkand never expose a manual toggle - JavaScript-driven theme state — a toggle button that flips a class or data attribute after the page loads
- Cookie or localStorage-based persistence — the theme is only applied if a specific cookie or localStorage key exists before render
- Query param or URL-based themes — rare, but some docs sites and dashboards support
?theme=dark
A generic "take a screenshot" API call won't know which of these applies to your target page. You need a way to tell the rendering engine to emulate the dark preference before the page paints.
The Correct Way: Emulate `prefers-color-scheme`
The most reliable method — and the one that works across the largest number of sites — is emulating the browser's prefers-color-scheme media feature at the browser level, not by injecting CSS. This is exactly what browsers like Chrome do when you flip your OS dark mode setting, and most frameworks (Tailwind's dark: variants, CSS @media queries, React theme providers) key off this signal.
With PxShot, this is a single parameter on the capture request:
GET https://api.pxshot.dev/capture
?url=https://example.com
&format=png
&dark_mode=true
Behind the scenes, this sets the emulated media feature before navigation, so any site that respects prefers-color-scheme: dark in CSS renders correctly — no injected classes, no fragile selectors, no waiting for a toggle click.
When the Site Doesn't Respect prefers-color-scheme
Some sites only apply dark mode after a user explicitly clicks a toggle, and that click sets a cookie or localStorage flag rather than relying on the OS preference. For these, you have three options:
- Pass a cookie — if the site reads a cookie like
theme=dark, most screenshot APIs (including PxShot) let you pass custom cookies in the request so the page renders dark on first load - Target a dark-specific URL — check if the site supports a query param like
?theme=darkor a separate route - Wait for a selector, then click it — as a last resort, use a "wait for element" + "click" step before capture if the API supports interaction steps; this is slower and more brittle, so only use it when the other two aren't possible
Practical Use Cases for Dark Mode Screenshot Capture
1. OG Images That Match Your Site's Theme
If your product has a dark-themed marketing site or docs, your Open Graph preview images should match — a bright white OG card on top of a dark-themed Twitter/X timeline looks broken. Generate both variants at build time and serve the one that matches context, or default to dark if that's your brand's primary look.
2. Visual Regression Testing Across Themes
Dark mode CSS bugs are notoriously easy to miss — a text color that's fine in light mode but unreadable on a dark background, a border that disappears, an icon that vanishes because it was styled with a hardcoded dark stroke. Automate this:
- Capture the same URL in both light and dark mode on every deploy
- Diff against the previous baseline for each theme separately
- Flag any diff above a pixel-difference threshold for manual review
This catches theme-specific regressions that a single-theme screenshot pipeline will never see.
3. Dark Mode PDF Exports
This one's counterintuitive but comes up a lot: internal dashboards, admin panels, and reporting tools that are dark-themed by default still need to export to PDF for sharing or archiving. Rendering the PDF in the dashboard's native dark theme (rather than forcing a light stylesheet override) keeps exported reports visually consistent with what users actually see day-to-day.
4. Marketing Screenshots for Product Pages
If you're building a landing page and want to show your product in both themes (a common pattern for dev tools and SaaS dashboards), capturing consistent, high-resolution screenshots of both modes via API means you can regenerate them automatically whenever the UI changes — no manual screenshotting in DevTools every release.
Common Mistakes When Capturing Dark Mode Screenshots
- Forgetting to wait for theme transition animations. Some sites animate the light-to-dark switch over 200–400ms. If your API captures immediately on media emulation, you might catch a mid-transition frame with a mismatched background and text color. Add a short delay (300–500ms) before capture if you notice this.
- Assuming dark mode is purely a CSS class swap. Some sites conditionally load different image assets, fonts, or even different DOM structures in dark mode. Test both branches, don't assume symmetry.
- Not testing on the actual production URL. Staging environments sometimes have different theme defaults baked into build-time env vars. Always verify against production before wiring up automated capture.
- Ignoring viewport-specific dark mode differences. Some responsive designs apply different dark mode treatments on mobile vs desktop viewports. If you're capturing for both, set viewport dimensions explicitly alongside the dark mode flag.
- Relying on screenshot delay instead of proper media emulation. Just waiting longer doesn't fix a page that never received the dark mode signal in the first place — you need the emulation to happen before first paint, not after.
A Minimal Working Example
Here's a full request that captures a page in dark mode, at a specific viewport, as a WebP for smaller file size — useful if you're batch-generating preview images:
GET https://api.pxshot.dev/capture
?url=https://yourapp.com/dashboard
&format=webp
&dark_mode=true
&viewport_width=1280
&viewport_height=800
&delay=400
The delay parameter here accounts for any CSS transition on theme switch, and the explicit viewport ensures you're not accidentally capturing a mobile breakpoint's dark mode variant when you wanted desktop.
Getting Started
If you're currently screenshotting pages with a headless browser script and manually toggling dark mode classes, it's worth trying an API that handles the media emulation correctly out of the box. PxShot supports the dark_mode parameter natively, along with custom cookies, viewport control, and PDF export — all through plain HTTP requests, no browser infrastructure to maintain.
Head to pxshot.dev and try the free tier — no credit card required to test dark mode capture against your own URLs.