Build Forms With API Routes
An HTML form causes the browser to refresh the page or navigate to a new one. To send form data to an API endpoint instead, you must intercept the form submission using JavaScript.
This recipe shows you how to send form data to an API endpoint and handle that data.
Create a form component using your UI framework. Each input should have a name
attribute that describes the value of that input.
Be sure to include a <button>
or <input type="submit">
element to submit the form.
export default function Form () {
return (
< form >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
</ form >
) ;
}
export default function Form () {
return (
< form >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
</ form >
) ;
}
export default function Form () {
return (
< form >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
</ form >
) ;
}
< form >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
</ form >
< template >
< form >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required / >
</ label >
< button > Send </ button >
</ form >
</ template >
Lit
Create the post
API endpoint that will receive the form data. Use request.formData()
to process it. Be sure to validate the form values before you use them.
This example sends a JSON object with a message back to the client.
import type { APIRoute } from "astro" ;
export const post : APIRoute = async ( { request } ) => {
const data = await request . formData () ;
const name = data . get ( "name" ) ;
const email = data . get ( "email" ) ;
const message = data . get ( "message" ) ;
// Validate the data - you'll probably want to do more than this
if ( ! name || ! email || ! message) {
return new Response (
JSON . stringify ( {
message : "Missing required fields" ,
} ) ,
{ status : 400 }
) ;
}
// Do something with the data, then return a success response
return new Response (
JSON . stringify ( {
message : "Success!"
} ) ,
{ status : 200 }
) ;
};
Create a function that accepts a submit event, then pass it as a submit
event handler to your form. In the function, call preventDefault
on the event to override the browser’s default submission process.
Then, create a FormData
object and send it to your endpoint using fetch
.
import { useState } from "preact/hooks" ;
export default function Form () {
const [ responseMessage , setResponseMessage ] = useState ( "" ) ;
async function submit (e : SubmitEvent ) {
e . preventDefault () ;
const formData = new FormData (e . target as HTMLFormElement ) ;
const response = await fetch ( "/api/feedback" , {
method : "POST" ,
body : formData ,
} ) ;
const data = await response . json () ;
if (data . message) {
setResponseMessage (data . message) ;
}
}
return (
< form onSubmit ={ submit } >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
{ responseMessage && < p > { responseMessage } </ p > }
</ form >
) ;
}
import { useState } from "react" ;
export default function Form () {
const [ responseMessage , setResponseMessage ] = useState ( "" ) ;
async function submit (e : SubmitEvent ) {
e . preventDefault () ;
const formData = new FormData (e . target as HTMLFormElement ) ;
const response = await fetch ( "/api/feedback" , {
method : "POST" ,
body : formData ,
} ) ;
const data = await response . json () ;
if (data . message) {
setResponseMessage (data . message) ;
}
}
return (
< form onSubmit ={ submit } >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
{ responseMessage && < p > { responseMessage } </ p > }
</ form >
) ;
}
import { createSignal , createResource , Suspense } from "solid-js" ;
async function postFormData (formData : FormData ) {
const response = await fetch ( "/api/feedback" , {
method : "POST" ,
body : formData ,
} ) ;
const data = await response . json () ;
return data ;
}
export default function Form () {
const [ formData , setFormData ] = createSignal < FormData > () ;
const [ response ] = createResource (formData , postFormData) ;
function submit (e : SubmitEvent ) {
e . preventDefault () ;
setFormData ( new FormData (e . target as HTMLFormElement )) ;
}
return (
< form onSubmit ={ submit } >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
< Suspense > { response () && < p > { response () . message } </ p > } </ Suspense >
</ form >
) ;
}
< script lang = "ts" >
let responseMessage : string ;
async function submit (e : SubmitEvent ) {
e . preventDefault () ;
const formData = new FormData (e . currentTarget as HTMLFormElement ) ;
const response = await fetch ( "/api/feedback" , {
method : "POST" ,
body : formData ,
} ) ;
const data = await response . json () ;
responseMessage = data . message ;
}
</ script >
< form on : submit = { submit } >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required />
</ label >
< button > Send </ button >
{# if responseMessage }
< p > { responseMessage } </ p >
{/ if }
</ form >
< script setup lang = "ts" >
import { ref } from "vue" ;
const responseMessage = ref < string > () ;
async function submit (e : Event ) {
e . preventDefault () ;
const formData = new FormData (e . currentTarget as HTMLFormElement ) ;
const response = await fetch ( "/api/feedback" , {
method : "POST" ,
body : formData ,
} ) ;
const data = await response . json () ;
responseMessage . value = data . message ;
}
</ script >
< template >
< form @submit = "submit" >
< label >
Name
< input type = "text" id = "name" name = "name" required />
</ label >
< label >
Email
< input type = "email" id = "email" name = "email" required />
</ label >
< label >
Message
< textarea id = "message" name = "message" required / >
</ label >
< button > Send </ button >
< p v-if = "responseMessage" > {{ responseMessage }} </ p >
</ form >
</ template >
Lit
More recipes
Learn how to share state across components — and frameworks! — with Nano Stores.
Une introduction aux flux RSS avec Astro.
Learn how you can import YAML data by adding a Rollup plugin to your project.
Learn how to use JavaScript to send form submissions to an API Route
Learn how to build HTML forms and handle submissions in your frontmatter
Learn how to use Bun with your Astro site.
Learn how to call endpoints from the server in Astro.
Learn how to create an API route and fetch it from the client.
Learn how to build your Astro site using Docker.
Learn how to install a rehype plugin to add icons to external links in your Markdown files
Use dynamic routing and content collections to add internationalization support to your Astro site.
Build a remark plugin to add the last modified time to your Markdown and MDX.
Build a remark plugin to add reading time to your Markdown or MDX files.
Learn how to share state across Astro components with Nano Stores.