Setting Netlify.toml functions="./api" Has No Affect on Function's URL

Setting a different functions folder name in netlify.toml or online via admin does not get reflected in final function URL. So if one sets netlify.toml -> build -> functions = "./api" the URL for the function via the web is still /.netlify/functions/hello.js.

How does one fix this so that the URL matches the functions folder name?

Hey Mark the shark!

You will want to add a redirect rule to your netlify.toml to set this up.

Try this:

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

This should map yoursite.com/.netlify/functions/hello to yoursite.com/api/hello

1 Like

Hi @markshark, it seems you cannot change the production URL of your function.
The setting in the netlify.toml only corresponds to the path of your functions folder in your code.

@DavidWells thank you for this solution, looks great. Just one concern. How do redirects on Netlify affect performance? Does one take a small hit when using them? One of the best features of functions is perf and scaleability over the same feature run serverside via a framework such as Laravel or Rails.

Also, I get this error on Netlify Dev:

ā—ˆ Warnings while parsing netlify.toml file:
  1: {"from":"/.netlify/functions/*","to":"/api/:splat","status":200} -- Invalid /.netlify path in redirect source

I think the from and to swapped in your example? This works as expected:

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200
1 Like

Whoopsie.

Have my rules backwards. Try this:

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200
1 Like

Hi @DavidWells! When I include:

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

in my netlify.toml, I receive the following error:

Warnings while parsing netlify.toml file:
TypeError: Cannot convert object to primitive value

I suspect this is related to #872.

The output of netlify --version is:

netlify-cli/2.48.0 linux-x64 node-v12.13.0

I have also tried this with versions of netlify-cli back to 2.22, and the result is the same. Which version of netlify-cli are you using that works with the [[redirects]] block you provided?

hey there, thanks for letting us know. The CLI team tells me they have just released a new version that should fix this, can you upgrade and let me know whether the issue is still occurring?

Thanks, @perry! I installed netlify-cli/2.49.0 with npm i -D netlify-cli@latest and Iā€™m happy to report that the CLI is working beautifully, with no error messages caused by the [[redirects]] block in netlify.toml.

1 Like

netlify-cli@2.50 has just been released which contains fix for this issue. Thank you all for your patience!