Next API route 404

I need to send an email on a form submission but can’t use Netlify forms. This is my first time using nextjs and netlify and I am just smashing my head on my keyboard tonight trying to get the API route working while deployed. I can hit the endpoint locally using next dev and netlify dev but it 404s when deployed.

I am using next-on-netlify v2.6.3, I have set target: 'serverless' in my next.config.js and configured my netlify.toml file like such:
[build]
command = “npm run build”
functions = “out_functions”
publish = “out_publish”

I have my function in pages/api/email.js.

The form submission is trying to hit the route /api/email with a POST request using the fetch API.
I just get a 404 and SyntaxError: Unexpected token < in JSON at position 0 that I suspect is a result of the 404.

My deploy logs seem correct Setting up redirects... /api/email /.netlify/functions/next_api_email 200

@joshzappone Can you elaborate on why you cant use netlify forms?

I agree the unexpected token is most likely coming from your 404 error.

It kind of looks like your posting to /api/email then doing a redirect to the functions?
Why dont you just post to your /functions endpoing directly?

Just a privacy thing. I cannot have access to the content submitted through the form so I need to pass it off and not have it saved anywhere.

I guess I am just not sure how to post to it directly. I was under the impression that functions would be automatically added to the functions tab in netlify but there is nothing there.

I believe from my reading that I should be able to create my endpoint in the pages/api dir and next-on-netlify would create the functions from there.

It seems like the function was created successfully… I think? At this point, I just created my own aws lambda endpoint but I would really like to keep everything within Next if possible.
image

Got it - that makes sense re / privacy.

I personally have not run the pages/api approach - in the past / current projects I just make a dir functions at the root and put everything in there.

On deploy those are available in the functions tab.

I think the problem stems from what sounds like your functions not being deployed. If they are not showing up in your functions tab, they are not there.

I would double check your site settings functions deploy settings to make sure netlify knows where to pull your built functions from.

https://app.netlify.com/sites/YOURSITENAME/settings/functions

I dont know enough about next on netlify to know if they config that for you. You could also set your functions directory in your .toml file