Question

How do I configure CORS for DigitalOcean functions?

I created a function and I’m trying to trigger it from javascript. I have a browser application that makes a fetch request, but it fails with a CORS error:

Access to fetch at ‘https://<functionId>.doserverless.co/api/v1/namespaces/<namespaceId>/actions/post-transaction?blocking=true&result=true’ from origin ‘http://localhost:8000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: 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.

I can’t find documentation explaining how to configure CORS for functions. I found some pages that imply functions are associated with Apps in the app platform, but when I created my function namespace, and function they weren’t associated with any app. Not sure if that documentation is out of date? I’ve looked in settings of my function but there is no mention of CORS or how to set the Access-Control-Allow-Origin header.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Matt Welke
DigitalOcean Employee
DigitalOcean Employee badge
March 30, 2023

There are two ways to configure CORS for Functions.

  1. You can add CORS headers to the object you return from your function (in the headers property). This will work for functions deployed through standalone Functions and for functions deployed as components in App Platform. When you enable your function to be a web function and invoke it via the web function URL, the body, headers, and statusCode properties will be used to determine the HTTP response that is returned to the client.

  2. If you’re deploying your functions as components in App Platform, you can take advantage of App Platform’s built in CORS support. You can add CORS support via the UI and via the app spec file.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up