Configuring CORS for a Strapi API

Strapi's CORS settings decide which browser origins can call your API. This page explains how to set explicit origins, methods and headers, handle preview and admin origins, and test that front ends still work.

On this page

Why Strapi CORS configuration matters before go-live

CORS is a browser rule that decides which website origins may read responses from your Strapi API. Strapi enforces it through its CORS middleware, configured in the project's middleware settings, so the setting you ship determines which front ends a visitor's browser will let talk to the API. Treat CORS as a pre-launch review item because a permissive default lets any site's scripts read responses that the browser would otherwise block. CORS is not an access control. Scripts run outside a browser, such as curl or server-to-server calls, ignore it, so authentication, public role permissions and token scopes still have to do the real protection work.

Before changing anything, the team needs an inventory of every origin that calls the API. That list usually includes the production front end, staging, preview deployments that may use generated hostnames, the Strapi admin panel host if it is served separately, and any internal tools that fetch content from a browser. For each origin, record the HTTP methods and request headers the front end actually sends, because the allowlist should match that traffic and nothing more. Confirm the default values in the version you run, since Strapi 5 minor releases have shipped roughly weekly. A CORS setting that was reviewed on one release should be checked again after each upgrade.

A team with one front end on one known domain and experience reading browser network traffic can usually make this change in-house. A partner earns its place when several front ends share the API, when preview environments create hostnames that change often, or when the review needs written evidence that each origin was tested. Ask any partner for a written scope that names the origins, methods and headers to be tested, the browsers and front-end builds covered, and a retest after the change. Plan that testing before go-live, because a blocked origin often fails quietly in the browser console and the front end appears to be broken.

Which CORS settings fit which front-end pattern

This table pairs common front-end patterns with the CORS settings each one needs and what breaks when a setting is too narrow. Read each row as a question: which pattern matches your front end, and which origin, method, header and credential settings follow from it. The main takeaway is that wildcard origins and credentials should never be combined, and every allowed method and header should trace back to a real call your front end makes.

Front-end patternRecommended CORS settingsWhen it fitsWhat breaks if too narrow
Single production siteOne exact production origin, only the methods used (often GET), credentials off for public readsPublic website or app that calls the API from one domain and reads published contentBrowser blocks responses from any other origin, including local development hosts
Staging and productionExplicit list with each environment origin, no wildcard, credentials only where login is usedTeams that preview or test against a separate staging front endStaging calls fail with missing Access-Control-Allow-Origin errors while production works
Preview and editor sitesPreview domain added as its own exact origin, with the same methods as the live front endEditors open previews from a front end on a different hostPreview loads blank or shows errors, which often looks like a content bug
Wildcard originOrigin set to all hosts (*), which does not allow credentials to be sentOnly for fully public, anonymous read endpoints with no cookies or Authorization headerCombined with credentials, the browser refuses the response, so logged-in calls fail
Methods limited to useOnly the verbs the front end sends, such as GET for reads and POST or PUT for editor actionsRead-only sites list GET and OPTIONS; apps that submit forms add POSTA missing verb fails the preflight request before the call reaches Strapi
Authorization header allowedAuthorization listed in allowed headers when the front end sends bearer tokensFront ends that call protected routes with an API token or user JWTPreflight fails for every authenticated request, while anonymous requests still work
Credentials offCredentials not allowed, no cookies sent cross-originToken-based APIs where the front end sends a header and holds no cross-site sessionNothing breaks for token calls, but cookie-based sessions will not be sent
Credentials onExplicit origins only, never a wildcard, and the front end sets its request to include credentialsFront ends that rely on cookies or sessions across a separate API hostWithout an exact origin, the browser blocks responses and users see sign-in loops

Handling preview, admin and staging origins

The Strapi admin panel is a browser application that calls the same Strapi server, so its origin is the one most often overlooked when teams rewrite CORS settings. If the admin panel is served from the same host as the API, browser requests are same-origin and may need no CORS entry at all. If you host the admin panel separately, add only that exact origin, and confirm the deployment model with your build before changing anything. Keep the admin origin out of any list used by public front ends.

  • Confirm the hosting model: Check whether the admin panel shares the API's scheme, host and port, or is served from a separate hostname.
  • Allow one exact origin: Add the admin origin with its scheme and port, not a wildcard or a parent domain.
  • Keep it out of public lists: Do not reuse the admin origin for a marketing site or customer portal.
  • Restrict admin access separately: CORS does not protect the admin login, so pair this with admin access controls.

After a change, log in to the admin panel in each deployed environment and confirm that content lists and media load without console errors.

How to replace the default CORS settings step by step

This sequence moves a Strapi API from its permissive default CORS behavior to an explicit, environment-specific allowlist, and confirms each change with a check the reader can repeat.

  1. Inventory every browser origin and method

    Before changing any configuration, list every browser origin that calls the API: the production site, staging sites, preview deployments, the admin panel host if it runs on a separate domain, and any tools that send requests from a browser. For each origin, record the HTTP methods and request headers it uses, such as Authorization or Content-Type. Check front-end code, CDN logs or reverse proxy access logs to confirm the list is complete. The step is done when the team agrees on a written table of origins, methods and headers that they can point to during review.

  2. Write the allowlist for each environment

    Turn the inventory into an explicit allowlist for each environment. Production should list only production front-end origins, while development and staging can list their own hosts. Keep the methods list to what the API actually needs, and list only the headers that the browser must send. Avoid wildcard origins in any environment that handles real user data. Ask your front-end team to confirm each origin, including scheme and port, because a mismatch in either will fail the browser check. The step is done when each environment has a reviewed list that the security owner has approved.

  3. Edit the CORS middleware configuration

    In the Strapi project, open the middleware configuration file (typically config/middlewares.ts or config/middlewares.js) and replace the default cors entry with an object that sets origin, methods, headers and credentials to the values from the allowlist. Confirm the option names against the Strapi version you run, since the middleware is documented in the Strapi docs for your release. Keep the change in version control so that it goes through code review like any other backend change. The step is done when the file compiles, the project starts locally, and the diff matches the approved table.

  4. Load origins from environment variables

    Move the origin list out of the source code and into environment variables such as a comma-separated value that the configuration file splits into an array. This lets each environment carry its own list without a code change, and it keeps production hostnames out of the repository. Set the variable in each deployment platform or container definition, and provide a safe fallback that allows no cross-origin browser access if the variable is missing. Ask your platform team how the variable will be injected in your secrets or configuration system. The step is done when a startup log or config dump shows the expected origins for each environment.

  5. Restart and confirm preflight responses

    Restart the Strapi process in the target environment so the new configuration loads. Then send a preflight request from an approved origin using curl with an Origin header and an Access-Control-Request-Method header, and check that the response includes an Access-Control-Allow-Origin value matching that origin. Run the same request from a browser DevTools console on the front end. The step is done when every approved origin receives the expected preflight headers and allowed methods appear in the response.

  6. Confirm rejected origins fail as expected

    Send the same requests with an origin that is not on the list, such as an unapproved test domain, and confirm that the response does not include a matching Access-Control-Allow-Origin header. In the browser, load a page from that unapproved origin and confirm the console reports a blocked cross-origin request. Also test one disallowed method, such as DELETE if the front end never calls it. The step is done when every rejected origin and method is blocked, and the results are saved with the change record for later audit.

  7. Test front-end flows and preview paths

    Walk through the real front-end flows that depend on the API: page loads, search, forms, authenticated requests, and any preview or draft mode that loads content from an admin or preview host. Each flow should succeed in the browser with no CORS errors in the console. Pay particular attention to preview origins, which are often added late and can be missed. Record the flows tested and the origins used. The step is done when the front-end team signs off on each flow in each environment, and any missing origin has been added through the same review process as the original allowlist.

How a browser request passes the CORS check

The request starts in the browser on the front-end origin, which sends a preflight OPTIONS request to the Strapi API before any real call. Strapi's cors middleware compares the Origin header with the allowlist and either returns the allowed methods and headers or rejects the request. Only after an allowed preflight does the actual request reach the Content API, where the token or public permissions decide what is returned. Check each origin on the list against the exact scheme, host and port your front end uses.

Preflight and actual request path across the browser and Strapi API trust boundary.

Security risks of loose or stale CORS settings

CORS settings control which browser origins can call your Strapi API and read its responses. The risks below share one root cause: the allowlist drifts from what the deployment actually needs, either because it was never tightened from defaults or because environments, domains and proxies changed without anyone revisiting it.

Wildcard origins with credentials

Setting the allowed origin to a wildcard while the API accepts credentials lets any website make authenticated requests and read the responses in a visitor's browser. Browsers block the most obvious combination, but a configuration that reflects arbitrary request origins back to the client can reach the same result. Check that every origin in the list is an explicit scheme, host and port, and that credentials are enabled only for origins you control.

Strapi SSO docs

Development origins left in production

Entries such as localhost ports, staging front ends or test tunnels often survive from the initial build. Any page served from those origins, including a developer's own machine or a forgotten preview deployment, can then call production content and admin-facing endpoints from a logged-in browser. Before go-live, compare the production origin list against the list of front ends that are actually deployed and remove everything else.

Overly broad allowed headers

Allowing every request header, or leaving header lists at their defaults, widens what cross-origin requests can send, including custom authorization headers that a page should never set. Narrow the allowed headers to those your front end sends, typically content type and authorization, and test each client's requests in the browser network panel to confirm nothing else is needed.

Forgotten origins after a domain change

A rebrand, a new hosting region or a move from one preview platform to another leaves old domains in the allowlist. Those domains may be registered by someone else, expired, or still under your control but no longer monitored. Each retired origin is an entry that can still receive responses from your API. Record an owner for every origin and remove entries as part of the domain change checklist.

CORS mistaken for access control

CORS only tells browsers whether to expose a response to a page. It does not stop curl, server-to-server code, mobile apps or scripts from calling the API, and it does not replace permissions. A team that relaxes Public role permissions because the origin list is tight, or that treats a narrow allowlist as its only API protection, has misread the control. Keep role permissions, API tokens and admin restrictions as the actual access controls and test them directly.

Conflicting CORS headers from proxies

A reverse proxy, CDN or API gateway that adds its own Access-Control headers can send duplicate or contradictory values alongside Strapi's. Browsers then reject responses that are otherwise correct, or a proxy setting overrides a stricter Strapi rule. Decide in one place which layer owns CORS, disable the others, and inspect the response headers from the public hostname, not from the Strapi container.

How to test that front ends still work after tightening CORS

Start by treating the CORS change as a release that needs its own test plan. Run the production build of each front end and its preview build against a staging Strapi instance that already has the restricted origin list, then walk every page that calls Strapi. Record which endpoints each page requests, whether the call runs in the browser or on the server, and whether it sends credentials. A server-side render or a build-time data fetch never hits CORS at all, so a page can look broken in the browser yet be fine in the build, or the reverse. Separate those cases before you start changing configuration.

Then test both sides of the access model. Load pages as an anonymous visitor and confirm that public content still returns with the expected headers from an allowed origin. Repeat with an authenticated end user, and with any call that uses an API token, to confirm that credentials and the Authorization header are accepted only where you intended. Use your browser's developer tools to read the Access-Control-Allow-Origin response header for each call. Also send a request from an origin that is not on the list and confirm the browser blocks the response, and that your front end shows a sensible error state instead of a blank page. Confirm the exact preflight behavior with your own call patterns, because the set of methods and custom headers each front end sends varies, and the framework-specific details belong to the integration pages.

Finally, turn the most important checks into a scripted test that runs in CI on every change to the Strapi configuration. A small job can send preflight OPTIONS requests and simple GET requests with an allowed and a disallowed Origin header, then assert on the status codes and CORS headers, and fail the build if an allowed origin is refused or a disallowed origin is accepted. Keep the origin list in one place so the test and the server read the same values. Ask your team to confirm which origins, including preview and admin hostnames, must pass in each environment, and rerun the browser checks after any front-end release that adds a new API call.

Limits and what to confirm in your environment

The CORS settings in a Strapi API depend on the exact release, the network path in front of it and the platform hosting your front end. The cards below list the items to confirm before you write the configuration, so the policy you test matches what runs in production.

Exact cors middleware options

The cors middleware options in config/middlewares.ts can differ between Strapi versions, so the keys and defaults you copy from an older example may not apply to your release. Check the Strapi 5 documentation and the release notes for the version you run, then confirm which options are accepted before you rely on them. Keep the version pinned in your repository so the policy does not change silently during an upgrade.

Strapi migration docs

Reverse proxy and gateway headers

A CDN, reverse proxy or API gateway in front of Strapi may add, overwrite or strip Access-Control-* headers. If both Strapi and the proxy set these headers, browsers can receive duplicate values and reject the response. Map every layer that touches the request, record which one owns CORS, and test with the same path your users take, not only a direct call to the application port.

Node.js and database versions

The CORS settings are only as stable as the runtime under them. Strapi 5 runs only on Active or Maintenance LTS Node.js versions, and supports PostgreSQL 14 or later, MySQL 8.0 or later, MariaDB 10.3 or later and SQLite 3. Confirm that the Node.js and database versions in your environment are on the supported list for the Strapi release you choose before you schedule the change.

Self-hosted or Strapi Cloud

On self-hosted Strapi, your team controls the middleware file, the reverse proxy and the deployment pipeline, so you can change CORS and test it in each environment. On Strapi Cloud, confirm which settings the platform exposes and whether any network layer in front of your project sets headers on your behalf. Ask the vendor how to validate changes before they reach production.

Strapi Cloud plans

Front-end host preflight behavior

The platform that serves your front end, such as a static host, edge function or server-side rendering runtime, may send requests from a different origin than you expect. Check whether it sends preflight OPTIONS requests, which headers it adds, and whether it routes preview deployments through their own domains. Each preview origin you need to allow should be listed explicitly, not matched with a broad wildcard.

Common questions about CORS work on Strapi

Can our own team configure CORS for a Strapi API without a partner?

Yes, a team with Node.js and Strapi experience can change the CORS settings in the server middleware configuration, and the change itself is small. The effort sits in the surrounding work: listing every origin that calls the API, confirming which methods and headers each front end sends, and proving that preview, staging and production all still load data after the change. Teams that already have a staging front end and an owner for each environment usually find the work manageable in-house. Teams without those inputs tend to discover missing origins only after a launch breaks, so the decision depends on how much verification capacity they can spare before go-live.

What makes CORS work on a Strapi project take more or less effort?

Effort rises with the number of front ends, environments and gateways that touch the API. Each additional origin needs its own entry, its own test, and a decision about whether it should exist in production at all. The main drivers are:

  • How many front ends call the API, including admin previews and static site builds
  • How many environments (development, staging, production) need different origin lists
  • Whether a reverse proxy, CDN or API gateway also sets CORS headers, which can conflict with Strapi
  • How much automated test coverage already exists for the front-end flows that depend on the API

A project with one site and no gateway is a much smaller task than a multi-brand platform with preview environments.

What does our team need to provide before CORS work starts?

The team needs to provide an origin inventory, a named owner for each environment, and access to a staging front end that mirrors production. The origin inventory lists every domain, subdomain and preview URL that makes browser requests to the API, along with the HTTP methods and custom headers each one uses. Environment owners decide which origins are allowed in each environment and approve changes. A staging front end lets the work be tested with real browser requests rather than only with command-line tools, which do not enforce CORS. Missing any of these usually adds rework rather than adding a separate phase.

Does CORS alone secure a Strapi API?

No. CORS only tells browsers which web origins may read responses from the API in the browser. It does not stop a server, a script run from the command line or a tool such as curl from calling the API, and it does not replace authentication or permissions. Public role permissions, API tokens, admin access restrictions and rate limits still decide what any caller can read or change. Teams should treat CORS as one layer that limits browser-based access from unexpected sites and verify the other layers separately.

How do we add a new front-end domain to Strapi after launch?

Add the new origin to the environment-specific allowlist, redeploy or restart Strapi so the configuration loads, and then test from a browser on that domain. Keep the change under version control and route it through the same review as other configuration changes. Remove origins that are no longer used, because stale entries widen the attack surface without serving any purpose. Confirm that the new origin is not a wildcard or a broad pattern that matches unrelated sites. A short written procedure for this request helps the team repeat the change consistently.

What does a fixed-scope CORS engagement from DeployOpen cover?

A fixed-scope engagement covers the agreed set of Strapi environments, front ends and gateways named in the written scope. Typical deliverables include the origin inventory review, the configuration change for each environment, browser-based test results for each front end and a short handover note for the owning team. Work outside the agreed scope, such as redesigning authentication or changing front-end code, is priced separately after the scope is updated. DeployOpen does not issue audit reports or certifications for the result. Ask for the exact deliverables and test cases in writing before signing, so both sides know what is complete.

How do we test that our front ends still work after changing CORS?

Test each front end in a real browser against each environment, because only browsers enforce CORS. For each origin, check that the expected requests succeed, that preview and admin screens load, and that requests from an origin not on the list fail as intended. Include login flows, form submissions and any request with custom headers, since those trigger preflight checks. Record the results so the team can repeat them after every deployment. Ask your team to confirm which flows are business-critical before testing begins.

How DeployOpen scopes and delivers CORS hardening for Strapi

CORS hardening starts with a written scope, not a configuration file. DeployOpen agrees with the client, before work begins, which front ends, preview tools and admin origins call the Strapi API in each environment, including development, staging and production. That list becomes the allowlist for the origin setting, and it is also where the team records which methods and headers each consumer really needs. Most engagements are fixed-scope, so the deliverables, the environments covered and the test cases are written down and agreed in advance. A quote follows from that written scope, which means the origin inventory and the number of environments are the main inputs the client should expect to review before signing.

Delivery happens in the client's own infrastructure. DeployOpen edits the middleware configuration in the Strapi project, moves environment-specific origins into the environment variables or secrets source the client already uses, and deploys through the client's existing pipeline or change process. Each change is tested against every front end on the inventory, including the preview flow and any admin origin, with the browser's network and console output checked for blocked requests. The handover package usually covers the final origin table per environment, the reasoning behind each allowed method and header, the regression test results, and the steps to add a new origin safely. Teams that own the Strapi project afterwards should confirm which of these artifacts they will keep in their own repository.

Ongoing changes are where many CORS problems return, because new front ends, preview domains and marketing tools get added after launch. DeployOpen offers retainers for this work, so origin additions, test updates and upgrade checks can be handled under an agreed arrangement instead of as one-off requests. The client should ask for the retainer terms in writing, including how new origins are requested, how quickly the team responds to a broken front end, and which environments are covered. Certifications and partner status are not part of this service, so buyers should judge the work on the written scope, the test evidence and the handover material.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.