Skip to content

Troubleshooting · By Rivtor · Updated

Fix CORS between Next.js and FastAPI in production

CORS is enforced by browsers when a frontend requests another origin. Inspect the actual request address and OPTIONS response before changing API middleware or broadening allowed origins.

See the Rivtor deployment workflow →

Inspect the browser request

Open the network panel and verify the exact hostname, method and response status. A frontend build can retain an old API address even after runtime environment changes. A localhost URL points at the visitor’s machine. Correct build-time values and rebuild when the framework inlines them.

Check preflight independently

Some headers and methods cause an OPTIONS request. Check its response and Access-Control-Allow-Origin header. A redirect, proxy rejection or authentication error can prevent the request from reaching the API handler at all, so investigate the failing layer first.

Allow the intended origin

An origin contains scheme, hostname and port. Allow the actual production frontend and explicit previews you intend to support. For credentialed requests, configure credentials deliberately; a blanket wildcard origin is not an appropriate repair. Check middleware order and the framework’s official CORS documentation.

Preserve authorization

CORS is not authentication and does not stop a non-browser client from calling a public API. Keep server-side authorization and account ownership checks. After the browser request succeeds, test an unauthorized request as well as a successful one.

Command reference

curl -i -X OPTIONS https://example.com/health -H "Origin: https://frontend.example.com" -H "Access-Control-Request-Method: GET"

These commands illustrate the documented workflow. Replace example addresses and identifiers with your own. Deployment and migration commands affect the selected project; confirm the target before running them.

Reproduce the preflight separately

The following read-only request asks an example API whether a browser origin can submit JSON:

curl -i -X OPTIONS https://api.example.com/items \
  -H "Origin: https://app.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: content-type"

Replace both addresses with your actual deployment. Inspect the status and access-control headers. Redirects, router errors and an incorrect path can prevent the browser from receiving the intended response even when CORS middleware exists.

Use exact origins with credentials

An origin includes scheme, hostname and port, without a path. For example, https://app.example.com differs from http://app.example.com and from a local development server. When cookies or other credentials are involved, configure explicit origins and the credential policy supported by your application.

Do not use mode: no-cors to repair an API call: it restricts what JavaScript can read and does not establish a correct credentialed API integration. Do not remove application authorization because a request now passes the browser's CORS check.

Distinguish address problems from policy problems

Inspect the final network request in browser developer tools. A page requesting localhost is using the visitor's machine; an obsolete frontend bundle may still contain an earlier public API URL. Rebuild when a build-time address changes.

Return to the Next.js/FastAPI deployment guide after correcting the request path. Source: FastAPI CORS configuration.

Next steps

Read the Rivtor reference for supported configuration and limitations, and use the deployment quickstart when trying Rivtor for the first time. Validate workflows with synthetic data before connecting valuable production records.