Prerequisites:
- Astro 4.0 or higher (Actions are only available in v4+)
- TypeScript enabled in your Astro project
- Strapi v4 or v5 backend
Features
- Type-safe Actions — Full TypeScript support with auto-generated types
- Zod Validation — Automatic input validation using Zod schemas
- Error Handling — Built-in error handling with ActionError
- CRUD Operations — Complete create, read, update, delete functionality
- Slug Support — Automatic slug-based queries when available
- Pagination — Built-in pagination support
- Populate Relations — Easy relation population
Setup
1
Install Strapi2Front
Install the package in your Astro project:
2
Initialize Configuration
Run the init command to create a configuration file:This creates a
strapi2front.config.ts file in your project root.3
Configure for Astro
Update your
strapi2front.config.ts to generate Astro Actions:strapi2front.config.ts
4
Add Environment Variables
Create a
.env file with your Strapi credentials:.env
5
Generate Actions
Run the sync command to generate types, services, and actions:This generates the following structure:
6
Register Actions
Create an
src/actions/index.ts file to export your actions:src/actions/index.ts
Usage
Fetching Data
- Collection Type
- Single Type
- By Slug
Use actions to fetch collection data in your Astro components:
src/pages/blog/index.astro
Form Handling
Use actions in forms with automatic validation:Client-Side Actions
Call actions from client-side scripts:src/components/Newsletter.tsx
Generated Actions Reference
Collection Type Actions
For each collection type (e.g.,Article), the following actions are generated:
- getAll
- getOne
- getBySlug
- create
- update
- delete
- count
Fetch all items with pagination:Options:
pagination— Page-based or offset-based paginationsort— Sort by field(s)- Returns:
{ data: Article[], pagination: StrapiPagination }
Single Type Actions
For single types (e.g.,Homepage), these actions are generated:
get()— Fetch the single type dataupdate()— Update the single type
Advanced Features
Typed Zod Schemas
WhenuseTypedSchemas: true is enabled, Strapi2Front generates fully typed Zod schemas based on your Strapi content types:
Error Handling
All actions include comprehensive error handling:Populate Relations
Easily populate related content:Best Practices
Actions are only available in Astro 4.0+. If you’re using an earlier version, use the generated services directly instead.
Troubleshooting
Actions not found
Make sure you’ve registered your actions insrc/actions/index.ts and exported them from the server object.
TypeScript errors
Runnpx strapi2front sync to regenerate types after making changes to your Strapi schema.
Validation errors
Check that your input data matches the generated Zod schemas. You can import schemas fromcollections/[name]/schemas.ts to validate data before submitting.
Next Steps
Services
Learn about the generated service functions
Schemas
Understand Zod schema generation
Relations
Work with Strapi relations
Media
Handle media uploads