Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.

Introduction

Astro is a modern web framework that delivers fast, content-focused websites. It’s perfect for building headless storefronts with CoCart because of its excellent performance, flexibility, and support for multiple frameworks. This guide will walk you through setting up an Astro project configured to work with CoCart API.

Why Astro for Headless Commerce?

  • Fast by default - Ships zero JavaScript by default, loading JS only when needed
  • Framework agnostic - Use React, Vue, Svelte, or vanilla JavaScript
  • SEO friendly - Server-side rendering and static site generation
  • Island architecture - Interactive components only where needed
  • Great DX - Hot module replacement and TypeScript support

Prerequisites

  • Node.js 18.17.1 or higher
  • A WordPress site with WooCommerce installed
  • CoCart plugin installed and activated
  • Basic knowledge of JavaScript and command line

Creating a New Astro Project

Create a new Astro project using the official CLI:
When prompted, choose the following options:
  • How would you like to start? → Empty
  • Install dependencies? → Yes
  • Initialize git repository? → Yes (recommended)
  • TypeScript? → Yes (recommended) or No
Navigate to your project:

Installing Tailwind CSS

Most UI component libraries (including OxbowUI) use Tailwind CSS. Install it using Astro’s integration:
This will:
  • Install Tailwind CSS and its dependencies
  • Create a tailwind.config.mjs file
  • Update your Astro configuration
  • Add necessary imports

Project Structure

Your Astro project should have this structure:
Create the necessary folders:

Creating the CoCart API Client

Create a centralized API client to interact with CoCart. Create src/lib/cocart.js:
We are currently building out this client, so for now just make standard fetch requests to the CoCart API endpoints as needed.

Environment Configuration

Create a .env file in your project root:
Variables prefixed with PUBLIC_ are exposed to the client-side code. Be careful not to expose sensitive data.
Add .env to your .gitignore:
Create a .env.example for your team:

Creating a Base Layout

Create a base layout at src/layouts/Layout.astro:

Adding AlpineJS for Interactivity

If you plan to use interactive components (like OxbowUI), install AlpineJS:
You can initialize it globally in your layout or per-component. For global initialization, update your layout:
Alternatively, you can use AlpineJS via CDN by adding this to your <head>:

Creating API Endpoints

Astro supports API routes for server-side operations. Create API endpoints for cart operations. Create src/pages/api/cart/add.js:

Testing Your Setup

Create a test page at src/pages/index.astro:

Running Your Project

Start the development server:
Visit http://localhost:4321 to see your store.

Building for Production

Build your site for production:
Preview the production build:

Deployment Options

Astro sites can be deployed to various platforms:
  • Vercel - Zero configuration deployment
  • Netlify - Easy deployment with built-in features
  • Cloudflare Pages - Global edge network
  • GitHub Pages - Free hosting for static sites
  • Your own server - Deploy the dist folder

Next Steps

Now that your Astro project is set up with CoCart:
  1. Build product listings with OxbowUI components
  2. Add shopping cart functionality
  3. Implement checkout flow
  4. Add user authentication
  5. Optimize for performance and SEO

Troubleshooting

CORS Errors

If you encounter CORS errors, you may need to configure WordPress to allow cross-origin requests. See CORS documentation.

API Connection Issues

  1. Verify your PUBLIC_STORE_URL is correct
  2. Ensure CoCart is installed and activated
  3. Check that WooCommerce is configured properly
  4. Test API endpoints directly in your browser or Postman

Resources