How to enable CORS in flask
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Browsers block many cross-origin requests by default, even when your Flask API is working correctly. CORS, short for Cross-Origin Resource Sharing, tells the browser which origins, methods, and headers are allowed. In Flask, the safest setup is usually a narrow allowlist rather than a global wildcard.
Enable CORS with Flask-CORS
For most projects, the Flask-CORS package is the easiest way to configure the correct headers. It handles normal responses and preflight OPTIONS requests without much boilerplate.
Install it with:
Then configure allowed origins for only the routes that need them:
This setup allows your frontend at https://app.example.com to call routes under /api/. Requests from other origins will still be blocked by the browser.
If your frontend sends cookies or authorization tied to browser credentials, you also need to opt in:
When credentials are enabled, do not use "*" as the allowed origin. Browsers reject that combination because it is too permissive.
Add CORS Headers Manually When You Need Tight Control
If you do not want an extension, you can add the headers yourself. This is useful when the allowlist depends on request data or when you want to understand exactly what the server is returning.
The Vary header matters here. Setting Vary: Origin tells caches that the response may differ by requesting origin, which prevents one origin from receiving headers intended for another.
How Preflight Requests Fit In
Browsers send a preflight OPTIONS request before some cross-origin calls, especially when the request uses custom headers or non-simple methods such as PUT or DELETE. Your server must answer that preflight with the correct CORS headers or the browser will block the real request before it is even sent.
That is why CORS issues can be confusing: your Flask route may work with curl, Postman, or a backend client, but still fail in the browser. CORS is enforced by the browser, not by Flask itself.
Common Pitfalls
The most common mistake is allowing every origin with "*" in production. That is often broader than necessary and becomes incompatible with credentialed requests.
Another mistake is forgetting about preflight handling. If the browser sends OPTIONS and your app returns a 405 response or misses the right headers, the real request never proceeds.
Many teams also treat CORS as an authentication system. It is not. CORS only tells browsers whether cross-origin JavaScript may read a response. It does not stop direct requests from non-browser clients, so you still need proper authentication and authorization.
Finally, be careful when reflecting the request origin dynamically. If you mirror any incoming Origin header without validation, you have effectively disabled origin restrictions.
Summary
- CORS controls which browser-based cross-origin requests may read your Flask responses.
- '
Flask-CORSis the quickest way to configure route-specific CORS behavior.' - Manual headers are useful when you need stricter or more dynamic control.
- Credentialed requests require an explicit origin, not
"*". - Successful API calls in Postman do not prove CORS is configured correctly for browsers.
Related reading
- How to enable HTTP response caching in Spring Boot
- How to enable POST, PUT AND DELETE methods in spring security
- How To Estimate the total time to complete the request In UDP and TCP ( Distributed Systems)
- How to explicitely define an Endpoint of an Kubernetes Service
- How to enable CORS in Spring Boot - Not working
- How to encrypt String in Java
- How to ensure task execution order per user using Celery, RabbitMQ and Django?
- How to erase the file contents of text file in Python?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.