API to submit forms

Hi, does anyone if there is a way to submit forms with an API and get a JSON response rather than redirect url. It’s a small detail but would integrate better with a react environment and repaints etc…
Best,
Thanks

hi there, have you looked through our open API docs to see if this is possible? :thinking:

Please have a look at the docs, e.g.: https://docs.netlify.com/forms/setup/#submit-forms-via-ajax.

You can skip the navigate method from the example code, and do something else like set your React state to do what you need to do, like show your “success” message or what have you.

1 Like

Thank you both for your answers.
The API only seems to allow GET and DELETE actions.
The AJAX seems like a good approach. I am still a unclear as to how the return functions. I assume that if netflify receives the form it is always considered a success and “success is returned”. (so no validation possible/needed? or integration with neflify functions for validation). It was also slighly unclear to me how the form success message is returned (redirect/json… etc). I may just have to set up a site and play with it so see the details. Thank you for your time and answers.

Greetings :wave:t2:

I believe I understand what you’re looking for, but I don’t think it’s available at this time. The Netlify API allows you to pull down submissions or delete them, but it does not allow you to submit new submissions to that form. The Netlify (‘Open’) API is essentially made to allow you to do anything you’d do in the Netlify Amin UI (https://app.netlify.com).

On the other side, as @mlaroy noted, you can just submit the form via AJAX on a page — although this is obviously very specific to code running on a browser, but the point does stand: you can submit to a form endpoint from any code running anywhere so long as you send it as a form-post-style packet (url-form-encoded header, correct body, etc.) to the right URL. I’ve done that from a local shell many-a-time.

That said, to your question about validating that the submission was accepted, that’s not quite as straightforward because the submission endpoint is expecting and prepared for a regular (normal) form submission from a browser… so it sends back HTML and attempts to handle the HTML workflow :stuck_out_tongue: . Generally speaking, if the response is a 200 (even though it contains HTML), the submission got to Netlify okay. If you really wanted to verify, you could use the Open API after you submit the form payload to check whether the particular submission made it. Haven’t tried it myself, but it could work.


Jon

1 Like