Projects Blog

How to Fix the CORS Error in Cloudflare Workers

While developing the Octagon API project, I encountered an issue related to the API, which was deployed as a microservice on Cloudflare Workers.

From Node.js, there was no issue fetching data from multiple endpoints. However, when attempting to fetch this data from a browser, the following error occurs:

Access to fetch at 'https://api.octagon-api.com/rankings' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Let’s see how to solve it!

Fixing the CORS Error

  1. Go to you Cloudflare dashboard and select your domain.

Cloudflare dashboard

  1. Now, in the side menu, expand the “Rules” dropdown menu and click on “Transform rules”.

Rules dropdown in Cloudflare side menu

  1. In the tabs menu, click on “Modify Response Header” and then on “Create rule”.

Modify response header tab in transform rules in Cloudflare

  1. A form will appear. Fill it out as follows:

Cloudflare transform rules configuration

Now, your API will allow requests for data from any origin, and the CORS error should no longer appear!


I hope you found this article useful.

Happy coding! 🚀