Why Vercel preview deployments usually call a shared staging API, how to point each frontend preview at a backend preview built from the same pull request, how to handle CORS for preview URLs, and a Next.js example that picks its API from the pull request number.

Vercel builds a preview deployment for every pull request, which covers frontend-only changes. When the pull request also changes the API, the preview still calls staging, and the reviewer sees an error that will not exist after merge.
The fix is a backend preview per pull request that the frontend can find. This guide shows how to wire it up.
Deploy a backend preview for the same pull request, at a URL the frontend can work out from the pull request number, and have the frontend choose that URL at build time when it is running as a preview. Then allow the preview origins in the backend's CORS settings.
Most projects store the API URL in a Vercel environment variable scoped to Preview, and Preview variables apply to every non-production branch unless you add a branch-specific override. One value for all branches means every preview calls the same backend, which is almost always staging.
Branch-specific overrides exist, but someone or something has to create one per branch with the right backend URL and remove it afterwards. In practice that becomes a CI step that calls the Vercel API after the backend preview is up, and it breaks in quiet ways when the frontend builds before the backend has finished deploying.
Derive the backend URL from the pull request number instead of passing it around. Vercel exposes the pull request number as a system environment variable, VERCEL_GIT_PULL_REQUEST_ID, and frameworks get a prefixed copy such as NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID that is inlined into client code at build time. If the backend preview for pull request 72 always lives at a predictable URL, the frontend can build that URL itself and nothing needs to be synchronized.
Pull request numbers are per repository, so the frontend and the backend have to live in the same repository for one number to name both previews, and the variable is empty for a deployment created from a push before the pull request was opened, so the code needs a fallback such as staging until the next push.
Every preview has a different origin, following the patterns in Vercel's generated URLs docs: <project>-<hash>-<scope>.vercel.app for a commit and <project>-git-<branch>-<scope>.vercel.app for a branch. Listing them one by one is impossible, so the backend needs a wildcard that matches your team's scope slug and nothing else, and https://*.vercel.app is far too broad, since anyone can deploy to that domain.
Requests that carry cookies or HTTP authentication are stricter, since the browser refuses a wildcard Access-Control-Allow-Origin for them, as MDN's CORS guide explains, so the server has to echo back the exact origin after matching it against its allow list. Sending a bearer token in the Authorization header instead of a cookie also avoids the third-party cookie rules that apply when a vercel.app page calls an API on another domain.
Vercel and the backend deploy independently, so the frontend preview for a new push is often live a minute or two before the matching backend, and during that window its requests fail or reach the previous commit's backend. A URL derived from the pull request number keeps working once the backend catches up, with no rebuild of the frontend, which is the main advantage over writing the backend URL into a variable after the backend deploys.
End-to-end tests that run against the preview should wait for both. A check that polls the backend's health endpoint for the expected commit before starting the browser tests avoids failures that only reflect deploy order.
Encore brings infrastructure from code to the backend side of the pull request, so the preview it builds has the same databases, topics and endpoints the branch declares, at a URL that only depends on the pull request number. Once the app is linked to GitHub, each pull request gets a preview environment hosted by Encore, named pr:72 for pull request 72, with its API at https://pr72-$APP_ID.encr.app, and it is destroyed when the pull request is merged or closed.

Run in your terminal to get started locally.
With the Next.js app in frontend/ and the Encore app in backend/ of the same repository, set the Encore app's Root Directory to backend under Settings in the Platform Dashboard, and point the Vercel project's root directory at frontend. Then generate a typed client for the frontend with client generation:
cd backend
encore gen client my-app --lang=typescript --output=../frontend/lib/client.ts --env=staging
The generated client exports Local, Environment(name) and PreviewEnv(pr), so choosing a backend is one function in the frontend:
// frontend/lib/api.ts
import Client, { Environment, Local, PreviewEnv } from "./client";
function backendURL(): string {
// Vercel inlines NEXT_PUBLIC_ variables into the bundle at build time.
const vercelEnv = process.env.NEXT_PUBLIC_VERCEL_ENV;
const pr = process.env.NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID;
if (vercelEnv === "production") {
return Environment("production"); // the name of your production environment
}
if (vercelEnv === "preview") {
// Empty when the branch was pushed before the pull request was opened.
return pr ? PreviewEnv(pr) : Environment("staging");
}
return Local; // http://localhost:4000 while running encore run
}
export const api = new Client(backendURL());
Encore allows requests without credentials from any origin by default, and allows every origin when running locally. It treats a request that carries cookies or an Authorization header as credentialed, so a frontend that sends a bearer token or a session cookie needs your team's preview pattern in allow_origins_with_credentials in encore.app, which accepts wildcards, as the CORS docs describe:
{
"id": "my-app",
"global_cors": {
"allow_origins_with_credentials": [
"https://*-acme.vercel.app",
"https://app.example.com"
]
}
}
Encore matches the * against any run of characters in the hostname, hyphens and dots included, so the pattern covers branch URLs with several hyphens, such as web-git-add-refunds-acme.vercel.app, along with commit URLs such as web-a1b2c3-acme.vercel.app and any other vercel.app hostname that ends in -acme.vercel.app. Every project in that team scope can then make credentialed requests, so narrow the pattern to the frontend project while keeping both URL forms, such as https://web-*-acme.vercel.app, if other projects share the scope, and keep authentication on the endpoints, since CORS is not an access control.
When a pull request changes an endpoint, regenerate the client against that preview with encore gen client my-app --lang=typescript --env=pr:72 to type-check the frontend against the new API before merge. Cron jobs do not run in preview environments, so a reviewer who needs a scheduled job's effect calls its endpoint on the preview directly.
The backend's databases and topics are declared in its code, so a coding agent that adds a table, changes an endpoint and updates the page that calls it can run all of it locally and open one pull request, which gets a matching pair of previews for the reviewer.
Best fit: Teams with a frontend on Vercel that want every pull request to show the frontend and the backend change together, with the backend running on AWS or GCP in production.
The API URL is usually a Preview environment variable with one value for every non-production branch, and that value points at staging. A pull request that changes both the frontend and the API then shows the new frontend calling the old backend, so the preview cannot show the change end to end.
Vercel sets VERCEL_GIT_PULL_REQUEST_ID on deployments triggered by a pull request, and exposes it to Next.js code as NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID. The value is an empty string for a deployment created on a branch before the pull request exists, so code that uses it needs a fallback.
Preview URLs follow the pattern project-hash-scope.vercel.app and project-git-branch-scope.vercel.app, so allow them with a wildcard such as https://*-yourteam.vercel.app, only for your own team slug. Requests that send cookies or other credentials need the origin on an explicit allow list, since browsers reject a wildcard response for credentialed requests.
Keep the frontend and the Encore app in the same repository, so one pull request number names both previews. Generate the client with encore gen client and construct it with PreviewEnv(process.env.NEXT_PUBLIC_VERCEL_GIT_PULL_REQUEST_ID) in preview deployments, which resolves to https://pr<number>-<app-id>.encr.app.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.