See this message would have been better at the beginning of this thread, could have been a much better dialogue between us.
I see in your script your doing the filtering at Anubis:
request.path.startsWith("/api/")
I did the opposite approach, I filter at my proxy/nginx and then only send web traffic to Anubis.  With Lemmy since they're 2-containers for web/api it looks like this:
                set $proxpass "http://anubis:8080/"; # this was the webui, but now it handles web traffic, passing into lemmy downstream
                if ($http_accept ~ "^application/.*$") {
                  set $proxpass "http://lemmy:8536/"; #api
                }
                if ($request_method = POST) {
                  set $proxpass "http://lemmy:8536/"; #api
                }
This way everything that goes to Anubis is 100% okay for it to handle.  Then also if there are endpoints that may not work (someone called out oauth flow), you can filter those out to go directly the the UI.
For PieFed, even if you don't have a proxy in front now (which honestly would surprise me), I think it'd be better to add one then filter at that level.  Let Anubis do what it does best, let Traefik/nginx/caddy/whatever do what it does best and route traffic.
For safety you could do the reverse - allow everything and cut endpoints one by one.