How to Build a CRUD App with Astro?
In this guide, you'll learn how to create a CRUD (create, read, update, and delete) web application using Astro.
This tutorial walks you through setting up a Back4app project—our robust backend—and designing a data structure that supports fundamental operations.
We will then build an Astro frontend that communicates with Back4app via REST/GraphQL APIs, ensuring secure and efficient data management.
By the end of this guide, you’ll have a production-ready web application complete with user authentication and secure data handling.
- How to structure a CRUD application with Astro and Back4app.
- Designing a scalable backend with custom collections.
- Using a drag-and-drop admin interface for effortless data management.
- Integrating your Astro frontend with Back4app using REST or GraphQL.
- Deploying your Astro project and containerizing it with Docker if needed.
Before you get started, please confirm you have:
- An Astro development environment set up. Use the official Astro documentation to get started. Ensure Node.js (version 14+) is installed.
- Basic familiarity with JavaScript, Astro, and REST APIs. Visit the Astro docs if you need a refresher.
- Sign in to your Back4app account.
- Click “New App” from your dashboard.
- Name your project: Basic-CRUD-App-Astro and follow the instructions to complete the setup.

After creation, your project will appear on your Back4app dashboard—your foundation for backend configuration.
For our CRUD application, you'll define several collections. Below are sample collections with fields and types to help you configure your database schema effectively. This structure supports efficient CRUD operations.
Field | Type | Purpose |
---|---|---|
_id | ObjectId | Auto-generated primary key. |
title | String | Name or title of the item. |
description | String | A brief overview of the item. |
created_at | Date | Timestamp of creation. |
updated_at | Date | Timestamp for last modification. |
Field | Type | Purpose |
---|---|---|
_id | ObjectId | Auto-generated primary key. |
username | String | Unique identifier for the user. |
String | User's email address. | |
password_hash | String | Encrypted password for authentication. |
created_at | Date | When the account was created. |
updated_at | Date | Last update time for the account. |
You can manually configure these collections in your Back4app dashboard by creating new classes and adding appropriate columns.

To add a field, select a data type, provide a field name, define a default if needed, and mark whether the field is required.

The integrated AI Agent in Back4app can auto-generate your schema. Describe your desired collections and fields in the AI Agent interface, review its recommendations, and apply them to your project.
This tool expedites setup while ensuring your schema meets application requirements.
The Back4app Admin App is a no-code tool that provides a visual interface to manage your backend data. Its drag-and-drop design lets you easily perform CRUD operations.
- Open the “More” menu in your Back4app dashboard.
- Select “Admin App” and click “Enable Admin App.”
- Set up your admin credentials by creating the initial admin user. This will also configure default roles (e.g., B4aAdminUser) and system collections.

After activation, log in to the Admin App to manage your collections.

Inside the Admin App you can:
- Add Records: Click “Add Record” in a collection (like Items) to create new entries.
- View/Edit Records: Click on a record to inspect or modify its fields.
- Remove Records: Delete obsolete entries effortlessly.
This tool simplifies CRUD operations with an intuitive interface.
With your backend configured, it’s time to integrate your Astro frontend.
Since Astro is a static site generator optimized for performance, we’ll interact with Back4app using REST or GraphQL APIs. This approach avoids the need for the Parse SDK.
Below is an example of a REST API call within an Astro component:
Integrate similar API calls for creating, updating, and deleting records within your Astro components.
Protect your data by setting ACLs on objects. For example, to create an item that only the owner can modify:
Within the Back4app dashboard, adjust the CLPs for each collection to define who can access and modify your data.
Back4app uses Parse’s User class for authentication. In your Astro project, create components for user sign-up and login by leveraging REST API calls.
You can build similar components for user login and session management.
Back4app's Web Deployment feature allows you to host your Astro project seamlessly by connecting your GitHub repository.
- Open a terminal in your project directory.
Run the build command:
- Check the Build: Ensure that the dist folder contains an index.html along with other asset directories.
Your repository should include the complete Astro source code. An example structure might be:
Initialize a Git repository (if not done):
Add all files:
Commit your changes:
Create a repository on GitHub (e.g., Basic-CRUD-App-Astro) and push:
- Access Web Deployment: Log in to your Back4app dashboard, navigate to your project (Basic-CRUD-App-Astro), and select the Web Deployment option.
- Connect Your GitHub Account: Follow the prompts to authorize Back4app to access your repository.
- Choose Your Repository and Branch: Select the repository (e.g., Basic-CRUD-App-Astro) and the branch (typically main).
- Build Command: Specify the build command (e.g., npm run build) if your repository lacks a pre-built output.
- Output Directory: Set it to dist so Back4app knows where to locate your static files.
- Environment Variables: Add any required variables, such as API keys.
If you prefer containerized deployments, include a Dockerfile like:
Integrate Docker settings in your Web Deployment configuration if desired.
- Hit the Deploy button in the Web Deployment section.
- Monitor the build process: Back4app will retrieve your code, build it, and deploy your application.
- Receive your URL: Once deployed, Back4app will supply a URL where your site is live.
Great job! You’ve now created a complete CRUD application with Astro and Back4app. Your project includes a backend with detailed collections for Items and Users, and a frontend that performs all CRUD operations through REST/GraphQL APIs.
Next Steps:
- Enhance the Frontend: Add features such as detailed views, search capabilities, and real-time notifications.
- Expand Backend Functionality: Consider integrating cloud functions or additional API endpoints for more complex logic.
- Dive Deeper: Explore further resources in the Back4app documentation and the Astro docs for advanced topics.
By following this tutorial, you now possess the knowledge to build a robust and scalable CRUD application using Astro and Back4app. Enjoy coding and exploring new possibilities!
