Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Proxy an external API through your domain using vercel.ts with CDN caching and cache tags.
API proxy rewrite is a Next.js boilerplate template that proxies an external API through your own Vercel domain via a vercel.ts rewrite rule, adding CDN caching and targeted cache purging.
API proxy rewrite is a Vercel example template that intercepts requests to /api/external/:path* on your domain and rewrites them to a backend API configured through the EXTERNAL_API_URL environment variable. The rewritten responses are cached on Vercel's CDN and tagged with a cache tag, so you can purge them selectively. It is built on Next.js and designed to run on the Vercel platform.
vercel.ts file using the routes.rewrite helper, so no custom server code is required.CDN-Cache-Control: public, max-age=60, stale-while-revalidate=3600 sets a 1-minute edge cache with a 1-hour stale-while-revalidate window.Vercel-Cache-Tag: api, allowing you to invalidate only /api/external responses via the Vercel Dashboard or CLI.EXTERNAL_API_URL to point to any REST API; the template defaults to https://jsonplaceholder.typicode.com./api/external/posts and renders blog posts to demonstrate the proxy working end to end.pnpm create next-app --example.This template suits developers deploying on Vercel who want to place their backend behind Vercel's CDN and caching layer. It is also useful for teams that need to consolidate multiple API origins behind a single domain path, and for developers who want a minimal, configuration-driven approach to API proxying without writing serverless functions.
/api/external/posts.vercel.ts rewrite matches /api/external/:path* and proxies the request to the URL in EXTERNAL_API_URL.CDN-Cache-Control and Vercel-Cache-Tag headers, caching the response at the edge.Set the EXTERNAL_API_URL environment variable in your Vercel project settings to your backend URL, then redeploy. The template uses https://jsonplaceholder.typicode.com by default if the variable is not set.
Use the Vercel Dashboard under CDN > Caches, select Cache Tag, enter api, and click Purge. Or run vercel cache invalidate --tag api from the CLI.
Yes, Vercel also supports project-level routing rules through the Dashboard, CLI, API, or SDK. The README links to a separate template for that no-code approach.
