Overview
The/api/ad-media endpoint proxies media assets (images and videos) from Facebook and Instagram CDNs. This solves CORS restrictions and provides consistent caching for ad creative assets.
This endpoint is unauthenticated and designed for high-volume usage with aggressive caching.
Endpoint
Request Parameters
Full URL of the media asset to proxy. Must be from an allowed host.Allowed hosts:
*.fbcdn.net*.facebook.com*.cdninstagram.com
Expected media type. Used for validation and content negotiation.Options:
image— Images only (PNG, JPEG, WebP, AVIF, GIF, etc.)video— Videos and streaming formats (MP4, M3U8, DASH, etc.)auto— Accept both images and videos
Response
Success (200 OK)
Returns the media asset bytes with appropriate headers:Success (206 Partial Content)
Supports HTTP range requests for video streaming:Pass-Through Headers
The following headers are passed through from upstream:Content-TypeContent-RangeAccept-RangesETagLast-ModifiedVaryCache-Control
Error Codes
| Status | Condition | Response Body |
|---|---|---|
400 | Missing src parameter | {"error": "Missing src query parameter."} |
400 | Invalid URL format | {"error": "Invalid src URL."} |
400 | Invalid kind value | {"error": "Invalid media kind. Use image, video, or auto."} |
400 | Unsupported protocol (non-HTTP/HTTPS) | {"error": "Unsupported URL protocol."} |
403 | Host not in allowlist | {"error": "Host is not allowed."} |
405 | Method not GET/HEAD | {"error": "Method not allowed."} |
415 | Content-Type mismatch | {"error": "Upstream response is not a video."} |
502 | Upstream fetch failed | {"error": "Failed to fetch media."} |
504 | Request timeout (15s) | {"error": "Timed out fetching media."} |
Content-Type Validation
The endpoint validates that upstreamContent-Type matches the requested kind:
Image Validation (kind=image)
Accepts:
image/*(JPEG, PNG, WebP, AVIF, GIF, BMP, etc.)application/octet-streamif URL ends with image extension
Video Validation (kind=video)
Accepts:
video/*(MP4, WebM, etc.)- Streaming formats:
application/vnd.apple.mpegurl,application/x-mpegurl,application/dash+xml application/octet-streamif URL ends with video extension (.mp4,.m4v,.mov,.webm,.m3u8,.mpd)
Auto Validation (kind=auto)
Accepts any image or video content type.
Examples
Proxy an Image
Proxy a Video
Handle Errors
Error Handling
Implementation Details
Request Timeout
Upstream requests timeout after 15 seconds (source:api/ad-media.js:4).User-Agent
Requests to upstream use a custom User-Agent:Referer Header
All upstream requests include:Accept-Encoding
The proxy requestsAccept-Encoding: identity to prevent compression mismatches when proxying bytes to browsers (source:api/ad-media.js:137).
Range Request Support
ClientRange headers are passed through to upstream for video streaming (source:api/ad-media.js:142-144).
Best Practices
Use kind parameter
Always specify
kind=video for videos to enable proper streaming format validationLeverage caching
The endpoint is designed for aggressive caching — use a CDN in front for best performance
Handle timeouts
Implement fallbacks for 504 errors, especially for large video files
Validate hosts
Only Facebook/Instagram CDN URLs are supported — validate before proxying
Related Endpoints
- Overview — API authentication and error handling
- Page Ripper — Capture landing pages with embedded media