Astro Actions in Astro

From the Astro cheat sheet ยท Modern Astro (Server Islands, Actions, Sessions) ยท verified Jul 2026

Astro Actions

Type-safe server functions callable from the client (Astro 4.15+)

typescript
// src/actions/index.ts
import { defineAction } from 'astro:actions'
import { z } from 'astro:schema'

export const server = {
  createComment: defineAction({
    accept: 'form',
    input: z.object({
      postId: z.string(),
      body: z.string().min(1).max(2000),
    }),
    handler: async ({ postId, body }, { cookies }) => {
      const user = cookies.get('userId')?.value
      if (!user) throw new Error('Not signed in')
      return db.comment.create({ data: { postId, body, userId: user } })
    },
  }),
}

// Use from a client component
<form method="POST" action={actions.createComment}>
  <input name="postId" value={post.id} hidden />
  <textarea name="body"></textarea>
  <button>Comment</button>
</form>
๐Ÿ’ก Astro Actions = type-safe server RPC with zod validation built in
๐Ÿ“Œ accept: "form" makes the action work as a plain progressive HTML form
โšก ActionError codes map cleanly to HTTP statuses โ€” use them, don't hand-roll
๐ŸŽฏ Pair with islands for forms that work without JS but enhance with it
actionsrpcforms

Continue with Astro

Save the full cheat sheet or work through every related task.

More Astro tasks

Back to the full Astro cheat sheet