VercelLogotypeVercelLogotype
LoginSign Up
Back to Templates

Proxy requests to external origins

Rewrite API traffic to an external backend using vercel.ts.

DeployView Demo
Screenshot 2026-02-27 at 6.00.07 PM

API proxy rewrite

Proxy an external API through your domain using vercel.ts. Requests to /api/external/:path* are rewritten to your backend, cached at the CDN, and tagged for targeted purging.

Proxying puts the Vercel CDN in front of your backend, improving performance through Vercel's global edge network and reducing cost with built-in caching. It also adds a layer of security with the Vercel Firewall.

Demo

https://proxy-requests-to-external-origins.vercel.app/

Visit /blog to see blog posts fetched from an external API through the CDN proxy.

How to use

One-Click Deploy

Deploy the template on Vercel using this link.

To proxy your own API instead of the demo, set the EXTERNAL_API_URL environment variable in your project settings or update the default value in vercel.ts, then redeploy. By default, the template uses jsonplaceholder.typicode.com as the external API.

Clone and Deploy

pnpm create next-app --example https://github.com/vercel/examples/tree/main/cdn/api-proxy-rewrite
cd api-proxy-rewrite
pnpm install
pnpm dev

How it works

The rewrite and caching configuration lives in vercel.ts:

import { routes, type VercelConfig } from '@vercel/config/v1'
const EXTERNAL_API_URL =
process.env.EXTERNAL_API_URL || 'https://jsonplaceholder.typicode.com'
export const config: VercelConfig = {
framework: 'nextjs',
outputDirectory: '.next',
rewrites: [
routes.rewrite('/api/external/:path*', `${EXTERNAL_API_URL}/:path*`),
],
headers: [
routes.header('/api/external/:path*', [
{
key: 'CDN-Cache-Control',
value: 'public, max-age=60, stale-while-revalidate=3600',
},
{ key: 'Vercel-Cache-Tag', value: 'api' },
]),
],
}
  1. The Next.js app renders the blog page and fetches /api/external/posts
  2. The rewrite in vercel.ts matches /api/external/:path* and proxies the request to your configured backend
  3. The CDN-Cache-Control header caches responses at the edge for 1 minute, with a 1-hour stale-while-revalidate window
  4. The Vercel-Cache-Tag header tags responses with api for targeted cache purging

CDN caching

CDN-Cache-Control controls caching at the Vercel CDN only. Unlike Cache-Control, which affects both the browser and CDN, CDN-Cache-Control is stripped before reaching the browser.

  • max-age=60: The CDN serves cached responses for 1 minute without hitting the external API.
  • stale-while-revalidate=3600: After the 1-minute window, the CDN serves stale content immediately while fetching a fresh copy in the background.

Cache tags

Vercel-Cache-Tag: api labels cached responses with a tag. You can purge all responses tagged api without flushing the entire project cache:

  • Dashboard: Navigate to CDN > Caches, select Cache Tag, enter api, and click Purge
  • CLI:
vercel cache invalidate --tag api

No-code alternative

If you prefer to configure routing without code or redeployment, you can use project level routing rules through the Vercel Dashboard, CLI, API, or SDK. See the Add API External Rewrite Routing Rule template for a no-code approach.

GitHub Repovercel/examples
LicenseView License
Use Cases
CDN
Stack
Next.jsTailwind

Get Started

  • Templates
  • Supported frameworks
  • Marketplace
  • Domains

Build

  • Next.js on Vercel
  • Turborepo
  • v0

Scale

  • Content delivery network
  • Fluid compute
  • CI/CD
  • Observability
  • AI GatewayNew
  • Vercel AgentNew

Secure

  • Platform security
  • Web Application Firewall
  • Bot management
  • BotID
  • SandboxNew

Resources

  • Pricing
  • Customers
  • Enterprise
  • Articles
  • Startups
  • Solution partners

Learn

  • Docs
  • Blog
  • Changelog
  • Knowledge Base
  • Academy
  • Community

Frameworks

  • Next.js
  • Nuxt
  • Svelte
  • Nitro
  • Turbo

SDKs

  • AI SDK
  • Workflow SDKNew
  • Flags SDK
  • Chat SDK
  • Streamdown AINew

Use Cases

  • Composable commerce
  • Multi-tenant platforms
  • Web apps
  • Marketing sites
  • Platform engineers
  • Design engineers

Company

  • About
  • Careers
  • Help
  • Press
  • Legal
  • Privacy Policy

Community

  • Open source program
  • Events
  • Shipped on Vercel
  • GitHub
  • LinkedIn
  • X
  • YouTube

Loading status…

Select a display theme:
    • AI Cloud
      • v0

        Build applications with AI

      • AI SDK

        The AI Toolkit for TypeScript

      • AI Gateway

        One endpoint, all your models

      • Vercel Agent

        An agent that knows your stack

      • Sandbox

        AI workflows in live environments

    • Core Platform
      • CI/CD

        Helping teams ship 6× faster

      • Content Delivery

        Fast, scalable, and reliable

      • Fluid Compute

        Servers, in serverless form

      • Observability

        Trace every step

    • Security
      • Bot Management

        Scalable bot protection

      • BotID

        Invisible CAPTCHA

      • Platform Security

        DDoS Protection, Firewall

      • Web Application Firewall

        Granular, custom protection

    • Company
      • Customers

        Trusted by the best teams

      • Blog

        The latest posts and changes

      • Changelog

        See what shipped

      • Press

        Read the latest news

      • Events

        Join us at an event

    • Learn
      • Docs

        Vercel documentation

      • Academy

        Linear courses to level up

      • Knowledge Base

        Find help quickly

      • Community

        Join the conversation

    • Open Source
      • Next.js

        The native Next.js platform

      • Nuxt

        The progressive web framework

      • Svelte

        The web’s efficient UI framework

      • Turborepo

        Speed with Enterprise scale

    • Use Cases
      • AI Apps

        Deploy at the speed of AI

      • Composable Commerce

        Power storefronts that convert

      • Marketing Sites

        Launch campaigns fast

      • Multi-tenant Platforms

        Scale apps with one codebase

      • Web Apps

        Ship features, not infrastructure

    • Tools
      • Marketplace

        Extend and automate workflows

      • Templates

        Jumpstart app development

      • Partner Finder

        Get help from solution partners

    • Users
      • Platform Engineers

        Automate away repetition

      • Design Engineers

        Deploy for every idea

  • Enterprise
  • Pricing
Log InContact
Sign Up
Sign Up
Back to Templates
DeployView Demo
v0

Build applications with AI

AI SDK

The AI Toolkit for TypeScript

AI Gateway

One endpoint, all your models

Vercel Agent

An agent that knows your stack

Sandbox

AI workflows in live environments

CI/CD

Helping teams ship 6× faster

Content Delivery

Fast, scalable, and reliable

Fluid Compute

Servers, in serverless form

Observability

Trace every step

Bot Management

Scalable bot protection

BotID

Invisible CAPTCHA

Platform Security

DDoS Protection, Firewall

Web Application Firewall

Granular, custom protection

Customers

Trusted by the best teams

Blog

The latest posts and changes

Changelog

See what shipped

Press

Read the latest news

Events

Join us at an event

Docs

Vercel documentation

Academy

Linear courses to level up

Knowledge Base

Find help quickly

Community

Join the conversation

Next.js

The native Next.js platform

Nuxt

The progressive web framework

Svelte

The web’s efficient UI framework

Turborepo

Speed with Enterprise scale

AI Apps

Deploy at the speed of AI

Composable Commerce

Power storefronts that convert

Marketing Sites

Launch campaigns fast

Multi-tenant Platforms

Scale apps with one codebase

Web Apps

Ship features, not infrastructure

Marketplace

Extend and automate workflows

Templates

Jumpstart app development

Partner Finder

Get help from solution partners

Platform Engineers

Automate away repetition

Design Engineers

Deploy for every idea