Home
Support Forums

I'm unable to set CORS header on POST/GET request to API on Netlify Functions

Hi,

I have deployed https://analytics.growthfyi.com on Netlify (Ackee Analytics), now when I use it on https://www.growthfyi.com, it shows CORS error.

I don’t know why /api isn’t showing CORS header whereas I have already added that via netlify.toml

Here is my Netlify.toml file

[build]
  publish = "dist"
  command = "yarn build"
  functions = "functions/"
  environment = { NODE_VERSION = "14.9.0", NODE_ENV = "production" }
  
[[redirects]]
  from = "/api"
  to = "/.netlify/functions/api"
  status = 200
  force = true
  headers = {Access-Control-Allow-Origin= "*"}

[[headers]]
  for = "/*"
  [headers.values]
    Access-Control-Allow-Origin= "*"

[template.environment]
  ACKEE_MONGODB = "ACKEE_MONGODB"
  ACKEE_USERNAME = "ACKEE_USERNAME"
  ACKEE_PASSWORD = "ACKEE_PASSWORD"
  ACKEE_ALLOW_ORIGIN = "ACKEE_ALLOW_ORIGIN"

I can see CORS on my landing page https://analytics.growthfyi.com and https://analytics.growthfyi.com/tracker.js but not on /api which is managed by Netlify functions.

Hi @kaknut,

To resolve your issue, your function itself will need to have that header as part of the response object that your function returns. You won’t be able to inject it via a redirect or header rule. Let me know if that helps.