I am making a publicly accessible API. I am trying to get ahead of the need to update it in the future by introducing versioning. By default, netlify.toml contains these lines:
[[redirects]]
from = "/api/*" # simplify all calls to serverless functions
to = "/.netlify/functions/:splat" # all function calls will go to this path
At first, I started putting my function files in subdirectories of /.netlify/functions/ So for example, I made these two:
/.netlify/functions/v1/hello.js
/.netlify/functions/v2/hello.js
(corrected)
/netlify/functions/v1/hello.js
/netlify/functions/v2/hello.js
I would then try to visit them (hosted locally with the CLI app) by going to:
http://localhost:8888/api/v1/hello
http://localhost:8888/api/v2/hello
My browser displayed both of these as “Function not found…” Whereas the CLI printed:
◈ Rewrote URL to /.netlify/functions/v1/hello
Request from ::1: GET /.netlify/functions/v1/hello
Response with status 404 in 0 ms.
◈ Rewrote URL to /.netlify/functions/v1/hello.html
Request from ::1: GET /.netlify/functions/v1/hello.html
Response with status 404 in 0 ms.
◈ Rewrote URL to /.netlify/functions/v1/hello.htm
Request from ::1: GET /.netlify/functions/v1/hello.htm
Response with status 404 in 0 ms.
◈ Rewrote URL to /.netlify/functions/v1/hello/index.html
Request from ::1: GET /.netlify/functions/v1/hello/index.html
Response with status 404 in 0 ms.
◈ Rewrote URL to /.netlify/functions/v1/hello/index.htm
Request from ::1: GET /.netlify/functions/v1/hello/index.htm
Response with status 404 in 1 ms.
On the other hand, any .js or .ts files that are directly in /.netlify/functions (and not a subdirectory) still run fine.
Next thing I tried was to rewrite the redirects section of the TOML file:
[[redirects]]
from = "/api/v1/*"
to = "/.netlify/functions/v1:splat"
from = "/api/v2/*"
to = "/.netlify/functions/v2:splat"
But it seems that the server doesn’t like multiple “from” and “to” entries; I suppose these are meant to be unique identifiers. The CLI said:
When resolving config file <C:\MyProjectPath>\netlify.toml:
Could not parse configuration file
Cannot redefine existing key 'redirects.from'.
Any solution to versioning my API here?